/*
 zhat chat V. 1.0 
 by Sorrawut Korsuwansiri
 */

// initial function
function zhat(data) {
	zhat_init(data);	
}	
function zhat_colorbox() {
	var output='';
	output+='<select id="zhat_colorbox">';	
	output+='<option value="default" selected="selected">เลือกสีข้อความ</option>';
	for (var i=0;i<zhat_color.length;i++) {
		output+='<option value="'+zhat_color[i][0]+'" style="background-color: '+zhat_color[i][0]+'">'+zhat_color[i][1]+'</option>';
	}
	output+='</select>';
	return output;
}
function zhat_color_init() {
	zhat_color=new Array();
	zhat_color[0]=new Array('black','Black');
	zhat_color[1]=new Array('skyblue','SkyBlue');
	zhat_color[2]=new Array('royalblue','RoyalBlue');
	zhat_color[3]=new Array('blue','Blue');
	zhat_color[4]=new Array('darkblue','DarkBlue');
	zhat_color[5]=new Array('orange','Orange');
	zhat_color[6]=new Array('crimson','Crimson');
	zhat_color[7]=new Array('red','Red');
	zhat_color[8]=new Array('firebrick','Firebrick');
	zhat_color[9]=new Array('darkred','DarkRed');
	zhat_color[10]=new Array('green','Green');
	zhat_color[11]=new Array('limegreen','LimeGreen');
	zhat_color[12]=new Array('seagreen','SeaGreen');
	zhat_color[13]=new Array('deeppink','DeepPink');
	zhat_color[14]=new Array('tomato','Tomato');
	zhat_color[15]=new Array('coral','Coral');
	zhat_color[16]=new Array('purple','Purple');
	zhat_color[17]=new Array('indigo','Indigo');
	zhat_color[18]=new Array('burlywood','Burlywood');
	zhat_color[19]=new Array('sandybrown','SandyBrown');
	zhat_color[20]=new Array('sienna','Sienna');
	zhat_color[21]=new Array('chocolate','Chocolate');
	zhat_color[22]=new Array('teal','Teal');
	zhat_color[23]=new Array('silver','Silver');
}
function zhat_init(data) {   		
	zhat_last_id=0;
	zhat_running=0;
	zhat_poster='';
	zhat_poster_home='';
	for (var i=0;i<data.room.length;i++) {
		//data.room[i]=encodeURIComponent(data.room[i]);
	}
	zhat_option=data;
	zhat_cur_room=zhat_option.room[0];
	zhat_webinfo=new Array();
	zhat_color_init();	
	var c="";
	c+='<div id="zhat_wrapper">';
	c+='<div id="zhat_room_title">'+data.title+'</div>';
	c+='<div id="zhat_room_menu"  onmousemove="zhat_pause(15)">'+zhat_room_menu(data.room[0])+'</div>';
	c+='<div id="zhat_box_cover"><div id="zhat_box" onmousemove="zhat_pause(10)"></div></div>';
	c+='<div id="zhat_form">';	
	c+='<table id=\"zhat_form">';
	c+='<tr><td>ชื่อ</td><td colspan="2">ข้อความ '+zhat_colorbox()+' <a id="zhat_emobutton" href="javascript:zhat_load_emo(\'o\')">อีโม</a></td></tr>';
	c+='<tr><td><input type="text" value="" id="zhat_poster" onkeypress="chkkey_poster(event)" /></td>';
	c+='<td><input type="text" value="" id="zhat_msg" onkeypress="chkkey(event)" /></td>';
	c+='<td><input id="zhat_send" type="button" value=" ส่ง " onclick="zhat_talk()" /></td></tr>';
	c+='</table>';
	c+='<div id="zhat_popup"  onmousemove="zhat_pause(10)" onmouseout="fixOnMouseOut(this,event,\'zhat_room_popdown();\');"></div>';
	c+='</div>';
	zhat_sniffBrowsers();
	document.getElementById('zhat').innerHTML=c;
	if (data.width) {
		document.getElementById('zhat').style.width=data.width;
		var fixie=0;
		if (ie) {fixie=-2}
		document.getElementById('zhat_msg').style.width=data.width-100-30-8+fixie;
	}
  zhat_key='';
  var url = "http://nok9.paidoo.net/zhat_init.php";   
  var js = document.createElement("script");
  js.setAttribute("src",url);   
  js.setAttribute("type","text/javascript");
  js.setAttribute("charset","utf-8");
  document.body.appendChild(js);   
}

function zhat_listen() {   
  var url = "http://nok9.paidoo.net/zhat_listen.php?room="+encodeURIComponent(zhat_cur_room)+"&key="+zhat_key+"&last_id="+zhat_last_id+"&poster="+zhat_poster+"&nocache="+Math.random();   
  //alert(url);
  var js = document.createElement("script");
  js.setAttribute("src",url);   
  js.setAttribute("type","text/javascript");                   
  js.setAttribute("charset","utf-8");
  document.body.appendChild(js);   
  clearTimeout(zhat_running);  
  zhat_running=setTimeout(zhat_listen,3000);  
}   
  
function zhat_listen_Callback(data) {   
var box=document.getElementById("zhat_box");
var cover=document.getElementById("zhat_box_cover");
  for (var i =0; i < data.length; i++) {
 	 box.innerHTML += data[i].msg; 	  	 
	}   			
	zhat_room_popdown();	// close opoup
	cover.scrollTop=getHeight("zhat_box")-getHeight("zhat_box_cover");
}
 
function zhat_pause(seconds) {
	// if pause , listen will stop for 10 second
	clearTimeout(zhat_running);  
	zhat_running=setTimeout(zhat_listen,seconds*1000);  
}
function zhat_talk() {
	var poster=document.getElementById("zhat_poster");
	var msg=document.getElementById("zhat_msg");
	var color=document.getElementById("zhat_colorbox");
	if (msg.value!=''&&poster.value!='') {
			msg.blur();
			msg.disabled=true;
			poster.disabled=true;			
			var url = "http://nok9.paidoo.net/zhat_talk.php?room="+encodeURIComponent(zhat_cur_room)+"&key="+zhat_key+"&msg="+ encodeURIComponent(msg.value)+"&poster="+encodeURIComponent(poster.value)+"&color="+color.value;   			
  			var js = document.createElement("script");
  			js.setAttribute("src",url);   
  			js.setAttribute("type","text/javascript");                   
			js.setAttribute("charset","utf-8");  			
  			document.body.appendChild(js);   		
		}			
	if (poster.value=='')	{
		alert('กรุณาใส่ชื่อของคุณก่อนส่งคำถาม');
	}
}
 
function zhat_talk_Callback(data){		
	var msg=document.getElementById("zhat_msg");	
	var box=document.getElementById("zhat_box");
	var cover=document.getElementById("zhat_box_cover");
 	box.innerHTML += data.msg; 	  	 	
	zhat_room_popdown();	// close opoup	
	cover.scrollTop=getHeight("zhat_box")-getHeight("zhat_box_cover");	
	
	msg.disabled=false;
	msg.value="";
	msg.focus();	
}
function zhat_room_popdown() {	
	var popup=document.getElementById('zhat_popup');	
	popup.style.display="none";
}
function zhat_room_popup() {
	 zhat_pause(10);
	var popup=document.getElementById('zhat_popup');
	var menu="<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\">";
	menu+="<tr><td class=\"zhat_popup_menu_choice\" onmousemove=\"zhat_pause(10)\"><a href=\"javascript:zhat_room('"+zhat_cur_room+"');\">"+zhat_cur_room+"</a></td></tr>";				
	menu+="<tr><td onmousemove=\"zhat_pause(10)\">เว็บมาสเตอร์จัดให้</td></tr>";
	for (var i=0;i<zhat_option.room.length;i++) {
			menu+="<tr><td class=\"zhat_popup_menu_choice\" onmousemove=\"zhat_pause(10)\"><a href=\"javascript:zhat_room('"+zhat_option.room[i]+"');\">"+zhat_option.room[i]+"</a></td></tr>";			
	}
	if (zhat_poster_home!='') {
	menu+="<tr><td onmousemove=\"zhat_pause(10)\">ห้องส่วนตัว</td></tr>";
	menu+="<tr><td class=\"zhat_popup_menu_choice\" onmousemove=\"zhat_pause(10)\"><a href=\"javascript:zhat_room('"+zhat_poster_home+"');\">"+zhat_poster_home+"</a></td></tr>";			
	}
	menu+="<tr><td onmousemove=\"zhat_pause(10)\">ห้องอื่นๆ พิมพ์ /room [ชื่อห้อง]</td></tr></table>";

	popup.style.display="block";
	popup.style.zIndex = "999";
	var fixiex=fixiey=0;
	if (ie) {fixiex=1;fixiey=2;}
	popup.style.top=getY('zhat_room')+fixiey;
	popup.style.left=getX('zhat_room')+fixiex;
	popup.className='zhat_popup_menu';
	popup.innerHTML=menu;		

}
function zhat_web_popup(id) {
	var popup=document.getElementById('zhat_popup');	
	var webinfo="";
	if (zhat_webinfo[id]!='') {		
		popup.innerHTML=zhat_webinfo[id];
		popup.style.zIndex = "999";
		popup.style.display="block";		
		popup.className='zhat_popup_banner';
		var fixiey=0;
		var fixiex=0;
		if (ie) {fixiey=1;fixiex=-1;}
		var boxscroll=getHeight("zhat_box")-getHeight("zhat_box_cover");
		var vscroll = document.getElementById("zhat_box_cover").scrollTop;		
		popup.style.top=getY('web'+id)-3+fixiey-vscroll;
		popup.style.left=getX('web'+id)-67+fixiex;	
	}
}
function zhat_room_menu(room) {
	return "<div class='zhat_left'>ห้อง : <a href='javascript:zhat_room_popup()' id='zhat_room' onmouseover='zhat_room_popup()'>"+room+"<b>&darr;</b></a></div><div class='zhat_right'><a href='http://nok9.paidoo.net/manual.html' target=_top>วิธีใช้</a></div><div style='clear:both'></div>";
}
function zhat_room(room) {	
	zhat_last_id=0;
	zhat_cur_room=room;
	document.getElementById("zhat_room_menu").innerHTML = zhat_room_menu(room);
	zhat_listen();
	document.getElementById("zhat_popup").style.display="none";
}

function zhat_load_emo(group) {
	zhat_pause(15);
	var popup=document.getElementById('zhat_popup');
	var emo="<span class='zhat_emomenu'>";
		
	for (var i=1;i<=30;i++) {
			emo+="<a href=\"javascript:zhat_insert_emo('"+group+i+"')\">";			
			emo+="<img src='http://nok9.paidoo.net/emo/"+group+i+".gif'>";			
			emo+="</a>";			
			if (i%6==0) {
				emo+="<br />\n";			
			}
	}
	emo+="<a href=\"javascript:zhat_load_emo('o')\">หัวหอม</a> - <a href=\"javascript:zhat_load_emo('m')\">ลิงน้อย</a> - <a href=\"javascript:zhat_load_emo('p')\">หมีแพนด้า</a>";
	emo+="</span>";
	popup.style.display="block";
	var fixiex=fixiey=0;
	if (ie) {fixiex=1;fixiey=2;}
	popup.style.top=getY('zhat_emobutton')-290+fixiey;
	var popupleft=getX('zhat_emobutton')-300+fixiex;
	if (popupleft<10) {popup.style.left=10;} else {popup.style.left=popupleft;}
	popup.className='zhat_popup_emomenu';
	popup.innerHTML=emo;		
	
}

function zhat_insert_emo(emo) {
	var msg=document.getElementById("zhat_msg");	
	zhat_room_popdown();	// close opoup
	msg.value+=' e'+emo+' ';
	msg.focus();		
}

function chunkui(name,web) {	
	var poster=document.getElementById("zhat_poster");		
	var url = "http://nok9.paidoo.net/zhat_invite.php?name="+name+"&web="+web+"&from="+encodeURIComponent(poster.value)+'&room='+zhat_cur_room;   			
  	var js = document.createElement("script");
	zhat_room(name+'@'+web);
  	js.setAttribute("src",url);   
  	js.setAttribute("type","text/javascript");                   
  	js.setAttribute("charset","utf-8");
  	document.body.appendChild(js);   			
}

function zhat_Callback(data) {
	// jump to new room
	
}
function chkkey_poster(e) {
	var keynum;
	if(window.event) // IE
  	{
  		keynum = e.keyCode;
  	}
	else if(e.which) // Netscape/Firefox/Opera
  	{
  		keynum = e.which;
  	}
  	if (keynum==13) {document.getElementById('zhat_msg').focus();}
}
function chkkey(e) {
var keynum;
if(window.event) // IE
  {
  keynum = e.keyCode;
  }
else if(e.which) // Netscape/Firefox/Opera
  {
  keynum = e.which;
  }
  if (keynum==13) {zhat_talk();}
}

// utility
function zhat_sniffBrowsers() {
	ns4 = document.layers;
	op5 = (navigator.userAgent.indexOf("Opera 5")!=-1) 
		||(navigator.userAgent.indexOf("Opera/5")!=-1);
	op6 = (navigator.userAgent.indexOf("Opera 6")!=-1) 
		||(navigator.userAgent.indexOf("Opera/6")!=-1);
	var agt=navigator.userAgent.toLowerCase();
	mac = (agt.indexOf("mac")!=-1);
	ie = (agt.indexOf("msie") != -1); 
	mac_ie = mac && ie;
}

function getX(elemID){
var offsetTrail = document.getElementById(elemID);
var offsetLeft = 0;
while (offsetTrail&&offsetTrail.style.position!='absolute'){
offsetLeft += offsetTrail.offsetLeft;
offsetTrail = offsetTrail.offsetParent;
}
if (navigator.userAgent.indexOf('Mac') != -1 && typeof document.body.leftMargin != 'undefined'){
offsetLeft += document.body.leftMargin;
}

return offsetLeft;
}	

function getY(elemID){
var offsetTrail = document.getElementById(elemID);
var offsetTop = 0;
while (offsetTrail&&offsetTrail.style.position!='absolute'){
offsetTop += offsetTrail.offsetTop;
offsetTrail = offsetTrail.offsetParent;
}
if (navigator.userAgent.indexOf('Mac') != -1 && typeof document.body.leftMargin != 'undefined'){
offsetTop += document.body.topMargin;
}
return offsetTop;
}	

function getHeight(Elem) {
	if (ns4) {
		var elem = getObjNN4(document, Elem);
		return elem.clip.height;
	} else {
		if(document.getElementById) {
			var elem = document.getElementById(Elem);
		} else if (document.all){
			var elem = document.all[Elem];
		}
		if (op5) { 
			xPos = elem.style.pixelHeight;
		} else {
			xPos = elem.offsetHeight;
		}
		return xPos;
	} 
}

function getWidth(Elem) {
	if (ns4) {
		var elem = getObjNN4(document, Elem);
		return elem.clip.width;
	} else {
		if(document.getElementById) {
			var elem = document.getElementById(Elem);
		} else if (document.all){
			var elem = document.all[Elem];
		}
		if (op5) {
			xPos = elem.style.pixelWidth;
		} else {
			xPos = elem.offsetWidth;
		}
		return xPos;
	}
}

function changecss(theClass,element,value) {
	 var cssRules;
	 if (document.all) {
	  cssRules = 'rules';
	 }
	 else if (document.getElementById) {
	  cssRules = 'cssRules';
	 }
	 var added = false;
	 for (var S = 0; S < document.styleSheets.length; S++){
	  for (var R = 0; R < document.styleSheets[S][cssRules].length; R++) {
	   if (document.styleSheets[S][cssRules][R].selectorText == theClass) {
	    if(document.styleSheets[S][cssRules][R].style[element]){
	    document.styleSheets[S][cssRules][R].style[element] = value;
	    added=true;
		break;
	    }
	   }
	  }

	  if(!added){
	  if(document.styleSheets[S].insertRule){
			  document.styleSheets[S].insertRule(theClass+' { '+element+': '+value+'; }',document.styleSheets[S][cssRules].length);
			} else if (document.styleSheets[S].addRule) {
				document.styleSheets[S].addRule(theClass,element+': '+value+';');
			}
	  }
	 }
}

	function get_object(id) {
		var object = null;
		if( document.layers )	{			
			object = document.layers[id];
		} else if( document.all ) {
			object = document.all[id];
		} else if( document.getElementById ) {
			object = document.getElementById(id);
		}
		return object;
	}

		function is_child_of(parent, child) {			
			var pn;
			if( child != null ) {							
			try {
				pn=child.parentNode;
			}
			catch(err) {return false;} 							
				while(pn) {					
					if( (child = child.parentNode) == parent ) {
						return true;
					}
					try {
						pn=child.parentNode;
					}
						catch(err) {return false;} 								
					}
			}
			return false;
		}
		function fixOnMouseOut(element, event, JavaScript_code) {
			var current_mouse_target = null;
			if( event.toElement ) {				
				current_mouse_target 			 = event.toElement;
			} else if( event.relatedTarget ) {				
				current_mouse_target 			 = event.relatedTarget;
			}			
			if( !is_child_of(element, current_mouse_target) && element != current_mouse_target ) {
				eval(JavaScript_code);
			}
			
		}
		
