// C H A T  by Vegeta
//Utiliser lors du la reherche d'un server
var lastChatId = 0;
var newChatterList = new Array();
var oldChatterList = new Array();
var newMessage = "";

function chat(action)
{
	var e_login = GetElementById("login");
	var e_text = GetElementById("chat_text");
	var content_chat = GetElementById("content_chat");
	var content_chatters = GetElementById("content_chatters");
	var table_chatters = GetElementById("table_chatters");
	
	if(action=='add_line') {
		newMessage = e_text.value;
		GetElementById("chat_text").value = "";
		return;
	}
	if(newMessage!=""&&action=='get_lines') {
		chat('add_chatline');
		return;
	}


	if (e_login!=null&&e_text!=null&&content_chat!=null&&content_chatters!=null) {
		content_chat.scrollTop=content_chat.scrollHeight;
		var data = 'Action='+action+'&lastChatId='+lastChatId;
		var page = 'ajax/ajax.chat.php';
		var servlist = "";
	
		if (window.ActiveXObject)
		{
			var XhrObj = new ActiveXObject("Microsoft.XMLHTTP") ;
		}
		else
		{
			var XhrObj = new XMLHttpRequest();
		}
	
		var chat_author = GetElementById("login").value;
		var chat_text 	= GetElementById("chat_text").value;
		
		if(action=='add_chatline') {
			data += "&Author="+chat_author;
			data += "&Text="+encodeURIComponent(newMessage);
			newMessage = "";
		} else if(action=='refresh_chatters') {
			data += "&Client="+chat_author;
		}
	
		//Ouverture du fichier en methode POST
		XhrObj.open("POST", page);
	
		//Ok pour la page cible
		XhrObj.onreadystatechange = function()
		{
			if (XhrObj.readyState == 4 && XhrObj.status == 200)
			{
				var res = XhrObj.responseText;
				if(action=='add_chatline') {
					newMessage = "";
					chat('get_lines');
				
				} else if(action=='get_lines') {
					var args = res.split("<!--separator-->");
					lastChatId = args[0];
					if(args[1].length > 27) {		
						addChatLine(args[1]);
					}
			
				} else if(action=='refresh_chatters') {
					newChatterList = res.split(",");
					//addChatLine(res);

					var t = getNewChatter();
					for(i=0;i<t.length;i++) {
						addPlayerToChattersList(t[i]);
					}
					var r = getOldChatter();
					for(i=0;i<r.length;i++) {
						var e = GetElementById("chat_player_id"+r[i]);
						table_chatters.removeChild(e);
						while(GetElementById("chat_player_id_info"+r[i]) != null) {
							var e = GetElementById("chat_player_id_info"+r[i]);
							table_chatters.removeChild(e);
						}
					}
					oldChatterList = newChatterList;
				}
			}
		}
		XhrObj.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		XhrObj.send(data);
	}
}

function addPlayerToChattersList(login) {
	var table_chatters = GetElementById("table_chatters");
	var content_chat = GetElementById("content_chat");
	var data = "login="+login;//+"&champ=ColoredNickName";
	var page = "./utils/getPlayerFromId.php";

	if (window.ActiveXObject)
	{
		//Internet Explorer
		var XhrObj = new ActiveXObject("Microsoft.XMLHTTP");
	}
	else
	{
		var XhrObj = new XMLHttpRequest();
	}
	//Ouverture du fichier en methode POST
	XhrObj.open("POST", page);
	
	//Ok pour la page cible
	XhrObj.onreadystatechange = function()
	{
		if (XhrObj.readyState == 4 && XhrObj.status == 200)
		{
			var res = XhrObj.responseText;
			var resT = res.split('<');
			var playerid = resT[0];
			resT[0] = "";
			var playerNick = resT.join('<');
			//for(i=0;i<5;i++) {
			var newChatter = document.createElement("TR");
			newChatter.id = "chat_player_id"+login;
			newChatter.onmouseover = function() {this.style.backgroundColor = '#fdfefe'}
			newChatter.onmouseout = function() {this.style.backgroundColor = '#f7f7f7'}
			newChatter.style.backgroundColor = "#f7f7f7";
			newChatter.style.cursor = "pointer";
			var cell = document.createElement("TD");
			cell.style.borderBottom = "1px solid #ccc";
			cell.innerHTML = "<div style=\"width:100px;height:20px;padding:4px 0px 0px 0px;overflow:hidden\">&nbsp;&nbsp;<font size='1'>"+playerNick+"</font></div>";
   			newChatter.appendChild(cell);
			newChatter.onclick = function() {
				if(GetElementById("chat_player_id_info"+login)==null) {
					cell.style.backgroundColor = "#ececfe";
					cell.style.borderBottom = "1px dashed #ccc";

					var newChatterInfo = document.createElement("TR");
					newChatterInfo.id = "chat_player_id_info"+login;
					var cellinfo = document.createElement("TD");
					cellinfo.innerHTML = "&nbsp;&nbsp;<img align='absmiddle' src='images/profile.gif'/>&nbsp;<font size='1'>View profile</font>";
					cellinfo.style.height = "20px";
					cellinfo.style.borderBottom = "1px solid #ccc";
					cellinfo.style.backgroundColor = "#fdfefe";
					cellinfo.style.cursor = "pointer";
					cellinfo.onclick = function() {
						internalLink("player:"+playerid);
					}
	   				newChatterInfo.appendChild(cellinfo);
					insertAfter(table_chatters, newChatterInfo, newChatter);
					/*var newChatterInfo = document.createElement("TR");
					newChatterInfo.id = "chat_player_id_info"+login;
					var cellinfo = document.createElement("TD");
					cellinfo.innerHTML = "&nbsp;&nbsp;<font size='1'>Say 'Hello'</font>";
					cellinfo.style.borderBottom = "";
					cellinfo.style.backgroundColor = "#fdfefe";
					cellinfo.style.cursor = "pointer";
					cellinfo.onclick = function() {
						newMessage="Hi player:"+playerid;
					}
	   				newChatterInfo.appendChild(cellinfo);
					insertAfter(table_chatters, newChatterInfo, newChatter);

					var newChatterInfo = document.createElement("TR");
					newChatterInfo.id = "chat_player_id_info"+login;
					var cellinfo = document.createElement("TD");
					cellinfo.innerHTML = "&nbsp;&nbsp;<font size='1'>Say 'Cya'</font>";
					cellinfo.style.borderBottom = "";
					cellinfo.style.backgroundColor = "#fdfefe";
					cellinfo.style.cursor = "pointer";
					cellinfo.onclick = function() {
						newMessage="Cya player:"+playerid;
					}
	   				newChatterInfo.appendChild(cellinfo);
					insertAfter(table_chatters, newChatterInfo, newChatter);
					*/
				} else {
					cell.style.backgroundColor = "";
					cell.style.borderBottom = "1px solid #ccc";
					while(GetElementById("chat_player_id_info"+login) != null) {
						var e = GetElementById("chat_player_id_info"+login);
						table_chatters.removeChild(e);
					}
				}
			}
			table_chatters.appendChild(newChatter);
			//alert(table_chatters.innerHTML);
			//}
		}
	}
	XhrObj.setRequestHeader('Content-Type','application/x-www-form-urlencoded');;
	XhrObj.send(data);
}

function insertAfter(parent, newElement, referenceElement){
  parent.insertBefore(newElement, referenceElement.nextSibling);
} 

function getNewChatter() {
	var res = new Array();
	var k = 0;
	for(i=0;i<newChatterList.length;i++) {
		$appartient = false;
		for(j=0;j<oldChatterList.length;j++) {
			if(!$appartient&&(oldChatterList[j]==newChatterList[i]))
				$appartient = true;
		}
		if(!$appartient) {
			res[k] = newChatterList[i];
			k++;
		}
	}
	return res;
}

function addChatLine(text) {
	var content_chat = GetElementById("content_chat");
	var noeud_element = document.createElement("DIV");
	noeud_element.innerHTML=text;
	content_chat.appendChild(noeud_element);
	content_chat.scrollTop=content_chat.scrollHeight;
	//content_chat.insertBefore(noeud_element,content_chat.firstChild);
}

function getOldChatter() {
	var res = new Array();
	var k = 0;
	for(i=0;i<oldChatterList.length;i++) {
		$appartient = false;
		for(j=0;j<newChatterList.length;j++) {
			if(!$appartient&&(newChatterList[j]==oldChatterList[i]))
				$appartient = true;
		}
		if(!$appartient) {
			res[k] = oldChatterList[i];
			k++;
		}
	}
	return res;
}

function start_chat(time,intervalle) {
	setTimeout("autorefresh_chat("+intervalle+")",time);
}
function autorefresh_chat(duration) {
	if(pageCourante=='pageHome')
		chat('get_lines');
	setTimeout("autorefresh_chat("+duration+")",duration);
}

function autorefresh_chatters(duration) {
	if(pageCourante=='pageHome')
		chat('refresh_chatters');
	setTimeout("autorefresh_chatters("+duration+")",duration);
}

function EvalSound(soundobj) {
 /* var thissound=GetElementById(soundobj);
  thissound.Play();*/
}

function soundSmiley(t){
	/*var res = false;
	if(t.search("images/smiley/grand_sourire.gif") != -1) {
		EvalSound('rire');
		res = true;
		return res;
	}
	if(t.search("bonjour") != -1) {
		EvalSound('bonjour_2');
		res = true;
		return res;
	}
	if(t.search("yo") != -1) {
		EvalSound('bonjour_1');
		res = true;
		return res;
	}
	if(t.search("doh") != -1) {
		EvalSound('doh');
		res = true;
		return res;
	}
	if(t.search("afk") != -1) {
		EvalSound('afk');
		res = true;
		return res;
	}
	if(t.search("wahoo") != -1) {
		EvalSound('wahoo');
		res = true;
		return res;
	}
	return res;*/
}

function sendMessageToChat(nick,message) {
	var data = 'Action=add_chatline&lastChatId='+lastChatId+"&Author="+nick+"&Text="+message;
	var page = 'ajax/ajax.chat.php';

	if (window.ActiveXObject)
	{
		var XhrObj = new ActiveXObject("Microsoft.XMLHTTP") ;
	}
	else
	{
		var XhrObj = new XMLHttpRequest();
	}
	//définition de l'endroit d'affichage:
	content_chat 		= GetElementById("content_chat");

	//Ouverture du fichier en methode POST
	XhrObj.open("POST", page);

	//Ok pour la page cible
	XhrObj.onreadystatechange = function()
	{
		if (XhrObj.readyState == 4 && XhrObj.status == 200)
		{
		}
	}
	XhrObj.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	XhrObj.send(data);
}


function getLastMessages(n) {
	var data = 'Action='+action+'&lastChatId='+1*lastChatId-1*n;
	var page = 'ajax/ajax.chat.php';

	if (window.ActiveXObject)
	{
		var XhrObj = new ActiveXObject("Microsoft.XMLHTTP") ;
	}
	else
	{
		var XhrObj = new XMLHttpRequest();
	}

	data += "&Client="+chat_author;

	//définition de l'endroit d'affichage:
	content_chat 		= GetElementById("content_chat");

	//Ouverture du fichier en methode POST
	XhrObj.open("POST", page);

	//Ok pour la page cible
	XhrObj.onreadystatechange = function()
	{
		if (XhrObj.readyState == 4 && XhrObj.status == 200)
		{
			var res = XhrObj.responseText;
			var args = res.split("<!--separator-->");
			lastChatId = args[0];
			if(args[1].length > 27) {		
				var type = "div";
				var noeud_element = document.createElement(type);
				noeud_element.innerHTML=args[1];
				//content_chat.insertBefore(noeud_element,content_chat.firstChild);
				content_chat.appendChild(noeud_element);	
				content_chat.scrollTop=content_chat.scrollHeight;
			}
		}
	}
	XhrObj.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	XhrObj.send(data);
}


