var req;
var do_type;
var url_target;
var timeStart = new Date();
var timeLast = timeStart.getTime();
var floattime = 0;
var topposition = document.body.scrollTop;
var display_text;

var intval=""
var timealert = 25000;

var cursor_x;var cursor_y;
var IE = false;
if (navigator.appName == "Microsoft Internet Explorer"){IE = true}
if (!IE){document.captureEvents(Event.MOUSEMOVE);
	document.onmousemove =  getPosition;}
function getCursor(xy) {
	if (window.Event) {
	}else{
		getPosition();		
	}
	if (xy =="x"){
		return cursor_x;
	}else{
		return cursor_y;
	}
}
function getPosition(e) {
	e = e || window.event;
	var cursor = {x:0, y:0};
	if (e.pageX || e.pageY) {
		cursor_x = e.pageX;
		cursor_y = e.pageY;
	} 
	else {
		cursor_x = e.clientX + 
			(document.documentElement.scrollLeft || 
			document.body.scrollLeft) - 
			document.documentElement.clientLeft;
		cursor_y = e.clientY + 
			(document.documentElement.scrollTop || 
			document.body.scrollTop) - 
			document.documentElement.clientTop;
	}
}

function show_mem_menu(div_type,mem_name_encode,mem_name,mem_sec){
  var div_menu = document.getElementById("div_menu");
  var div_text="";
  div_menu.style.top = getCursor("y") -60;
  div_menu.style.left = getCursor("x") -150;
  if(getCursor("x")-150 < 0){div_menu.style.left=10;}
  
	if (div_menu.className == 'menuOFF'){
		div_menu.className='menuOn';
	}else{
		div_menu.className='menuOFF';
	}
	switch (div_type) {
		case "recent_come" : div_text = '<strong>' + mini_decode(mem_name) + '</strong><br /><br /><img src="images/talk.gif" align="absmiddle"> <a style="font-family:tahoma;font-size=12;text-decoration: none" class="handcursor" onclick="JavaScript:setTimeout(' + "'showMSNPopup(true)'" + ',1000);opensmallMini(\'sendsms.aspx?mem_sec=' + mem_sec + '&mem_name=' + mem_name_encode + "'" + ",'" + mem_sec + "'" +')"><strong><font color=#666666>ส่งข้อความสด</font></strong></a> ' ;break;
	}
	div_menu.innerHTML = "<p align=right style='margin-top:0px'><a  class=handcursor><img src='images/closemenu.gif'></a></p><p align=center>" + div_text + " <strong>|</strong> <img src='images/useronline.gif' align='absmiddle'> <a href='memprofile.aspx?q_sec=" + mem_sec + "' target=_blank><strong>ดูข้อมูล Profile</strong></a> </p>";
}

function mini_decode(str){
	decode_str = str.replace('%2B','+');
	decode_str = decode_str.replace('%26','&');
	return decode_str
}

function make_querystring(){
	var str = document.URL;
	var slashlast = str.lastIndexOf("/");
	var file_call_name = str.substring(slashlast+1,(str.length));
	var show_offline_value;
	
	if(file_call_name.indexOf("social_network.asp") != -1){
		show_offline_value = "all";
	}else if(file_call_name.indexOf(".aspx") == -1 || file_call_name ==""){
		show_offline_value = "no";
	}else{
		show_offline_value = "yes";
	}

	return "show_offline=" + show_offline_value + "&";
}

function opensmall(pname) {
	win=open(pname,"","toolbar=0,directories=0,status=0,scrollbars=0,menubar=1,resizable=1,width=500,height=250");
}

function opensmallMini(pname,mem_sec) {
	win=open(pname,mem_sec,"toolbar=0,directories=0,status=0,scrollbars=0,menubar=0,resizable=0,width=450,height=150");
} 

function start_Int(){
		stop_Int();
		if(intval==""){
	        intval=window.setInterval('loadXMLDoc("smsalert.aspx?ran=" + Math.floor(Math.random()*100000),"sms")',timealert);
			if(timealert >= 20000){
				loadXMLDoc("smsalert.aspx?ran=" + Math.floor(Math.random()*100000),"sms")
			}
	    }else{
	        stop_Int();
    	}
}

function stop_Int(){
      if(intval!=""){
          window.clearInterval(intval);
          intval="";
      }
}

function EditInterval(editTime){
		timealert = editTime;
		stop_Int();
		start_Int();	
}
	

//***********slde

function browserName() { 
var browserName = ""; 
var ua = navigator.userAgent.toLowerCase(); 

	if ( ua.indexOf( "opera" ) != -1 ) { 
		browserName = "opera"; 
	} else if ( ua.indexOf( "msie" ) != -1 ) { 
		browserName = "msie"; 
	} else if ( ua.indexOf( "safari" ) != -1 ) { 
		browserName = "safari"; 
	} else if ( ua.indexOf( "mozilla" ) != -1 ) { 
		if ( ua.indexOf( "firefox" ) != -1 ) { 
			browserName = "firefox"; 
		} else { 
			browserName = "mozilla"; 
		} 
	} 

	if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)){ //test for MSIE x.x;
		var ieversion=new Number(RegExp.$1) // capture x.x portion and store as a number
		if (ieversion>=7)
	 	browserName = "msie8";
	}

	return browserName; 
} 

var step = 10;
var time = 500;
function showMSNPopup(visible){
	var objPopup = document.getElementById('smsalert');
	if(visible){
	
		if(browserName() != "msie"){
			if(objPopup.style.display != ""){	
				objPopup.style.bottom = 0-(parseInt(document.body.scrollTop))+"px";	
				//objPopup.style.bottom = 0;//-document.body.scrollTop-parseInt(objPopup.style.height);
			}
		}else{
			objPopup.style.bottom = 0;
		}
	
		objPopup.style.display = "";	
		//slide(true);
		
	}else{
		//slide(false);
		objPopup.style.display = "none";	
	}
}

function slide(isUp){

	var mystep = (!isUp)? -step: step;
	var objPopup = document.getElementById('smsalert');
	var baseFloat;

	if(browserName() == "msie"){
		baseFloat = 0;
	}else{
		baseFloat = 0-document.body.scrollTop;
	}

	
	if(isUp && parseInt(objPopup.style.bottom) >= baseFloat) {
		objPopup.style.bottom = baseFloat+"px";
		return;
	}else if(!isUp && parseInt(objPopup.style.bottom) <= baseFloat-parseInt(objPopup.style.height)){
		objPopup.style.display = "none";
		objPopup.style.bottom = "-"+objPopup.style.height;
		return;
	}
	objPopup.style.bottom = (parseInt(objPopup.style.bottom) + mystep)+"px";
	setTimeout("slide("+isUp+")", time);
}


function loadXMLDoc(url,do_type_val) {
    req = false;
	do_type = do_type_val;
	url_target = url;
    // branch for native XMLHttpRequest object
    if(window.XMLHttpRequest && !(window.ActiveXObject)) {
    	try {
			req = new XMLHttpRequest();
        } catch(e) {
			req = false;
        }
    // branch for IE/Windows ActiveX version
    } else if(window.ActiveXObject) {
       	try {
        	req = new ActiveXObject("Msxml2.XMLHTTP");
      	} catch(e) {
        	try {
          		req = new ActiveXObject("Microsoft.XMLHTTP");
        	} catch(e) {
          		req = false;
        	}
		}
    }
	if(req) {
		req.onreadystatechange = processReqChange;
		req.open("GET", url, true);
		req.send("");
	}
}

function checkFloatOpen(nTime){
	if (timealert != nTime) {
		showMSNPopup(true);
		EditInterval(nTime);
	}
}

function floatmove(){
			if (topposition != document.body.scrollTop){
				var objFloat = document.getElementById('smsalert');		

				if(browserName() == "msie"){
					objFloat.style.bottom = (parseInt(objFloat.style.bottom)+1)+"px";
					objFloat.style.bottom = (parseInt(objFloat.style.bottom)-1)+"px";		

				}else{
					objFloat.style.bottom = 0-(parseInt(document.body.scrollTop))+"px";	
				}

				topposition = document.body.scrollTop;	
			}	
}

//function autohide(){
//	showMSNPopup(false);
//	EditInterval(20000);	
//}

function processReqChange() {

	var timenow = new Date();

    // only if req shows "loaded"
    if (req.readyState == 4) {
        // only if "OK"
        if (req.status == 200) {
            // ...processing statements go here...		
			if (req.responseText != 'x' && req.responseText != ''){
		
				if (do_type =='sms') {
 					if (req.responseText == '**stop_im**'){
						stop_Int();
					}else{
						if (req.responseText != display_text){
							display_text = req.responseText;							
							//if (display_text.indexOf("เพิ่งเข้ามา") != -1){
								//PlayIt('music',"zoom.mid");
								//if (display_text.indexOf("#cccccc") == -1){ //แสดงว่าไม่มีข้อความติดมาด้วย
									//setTimeout('autohide()', 15000);
								//}
							//}else{
								PlayIt('music',"zoom.mid");	
							//}
	
							if (timealert > 10000) {
								if (timenow.getTime() - timeLast > 10000){
									timeLast = timenow.getTime();
									setTimeout("loadXMLDoc('useronline.asp?' + make_querystring() + 'ran=' + Math.floor(Math.random()*100000),'refresh')",1000);
								}			
							}		
		
							floattime = 0;						
							checkFloatOpen(10000);
							
							var objSMS = document.getElementById("divSMS");
							objSMS.innerHTML = objSMS.innerHTML+req.responseText;			
							objSMS.scrollTop = objSMS.scrollHeight;
	
	
						}
					}

				} else if (do_type =='refresh') {
					var checkText = req.responseText;		
					//if(checkText.length > 100){	
						var objUseronline = document.getElementById("useronlinezone");
						objUseronline.innerHTML = req.responseText;
						var checkFull = checkText.indexOf('FullUserOnline');
						if (checkFull!=-1){ 
							if (timealert == 25000) {	
								EditInterval(60000);
							}
						}
					//} else {
					//	setTimeout("loadXMLDoc('" + url_target + "','" + do_type + "')",5000);
					//}
				} else if (req.responseText == '**stop_im**'){
					stop_Int();
				}
			} else if (timenow.getTime() - timeLast > 300000){
				timeLast = timenow.getTime();	
				loadXMLDoc('useronline.asp?' + make_querystring() + 'ran=' + Math.floor(Math.random()*100000),'refresh');

			}			

			if (timealert == 10000) {	
				floattime += 1;
				if(floattime > 6){				
					//showMSNPopup(false);				
					EditInterval(25000);
					floattime = 0;
				}				
			
			}			
	
        } else {

			//setTimeout("loadXMLDoc('" + url_target + "','" + do_type + "')",3000);

            //alert("There was a problem retrieving the XML data:\n" +
             //   req.statusText);
        }
    }	
}

function PlayIt(where,what){
document.getElementById(where).innerHTML='<object width="1" height="1" '
      +'classid="CLSID:6BF52A52-394A-11d3-B153-00C04F79FAA6" '
      +'codebase="http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=5,1,52,701" '
      +'standby="Loading Microsoft Windows Media Player components..." type="application/x-oleobject">'
      +'<param name="url" value="'+what+'">'
      +'<param name="uiMode" value="invisible">'
      +'<param name="autoStart" value="true">'
      +'<param name="loop" value="true">'
      +'<embed type="application/x-mplayer2" '
      +'pluginspage="http://microsoft.com/windows/mediaplayer/en/download/" '
      +'showcontrols="0" ShowDisplay="0" uimode="invisible" width="1" height="1" '
      +'src="'+what+'" autostart="true" loop="true">'
+'<\/object>';
}

function StopIt(where){
	document.getElementById(where).innerHTML='';
}

var call_again_time = 0;

function CheckUserOnline(){
	var objUseronline = document.getElementById("useronlinezone");
	var checkText = objUseronline.innerHTML;
	checkText = checkText.toLowerCase();
	if (checkText.indexOf('table') == -1) {
		if (call_again_time < 2){
			call_again();
		}else{
			call_again_time = 0;
			objUseronline.innerHTML = '<p align=center class=smallfont>ขออภัย โหลดข้อมูล User Online ไ่ม่สำเร็จ<br /><a class="handcursor" onclick="call_again()"><img src=images/reload_small.gif border=0></a></center>';
		}
	}
}

/*function CheckUserOnline(){
	var objUseronline = document.getElementById("useronlinezone");
	var checkText = objUseronline.innerHTML;
	if (checkText.length <110) {
		call_again();
	}
}*/

function call_again(){
	call_again_time += 1;
	setTimeout("CheckUserOnline()",10000);		
	var objUseronline = document.getElementById("useronlinezone");		
	objUseronline.innerHTML = '<center><img src="images/loading_circle_pixel.gif"><br />' + call_again_time + '</center>';
	setTimeout("loadXMLDoc('useronline.asp?' + make_querystring() + 'ran=' + Math.floor(Math.random()*100000),'refresh')",1000);
}

document.onload=setInterval('floatmove()',1000);