// JavaScript Document
soundManager.url = '/includes/SoundManager2/swf/';
soundManager.onload = function()
{
	// SM2 has loaded - now you can create and play sounds!
	soundManager.createSound('chatMessage','/includes/SoundManager2/sounds/newChatMessage.mp3');
};

var action = 0;
var lastTime = 0;
function initialize()
{
	window.setInterval(chatUpdate, 2000);
	document.getElementById("output").scrollTop = document.getElementById("output").scrollHeight;
	document.chatForm.message.focus();
}
function initializeHttp()
{
	//initializes the HTTP object
	var xmlHttp;
	try
	{
		// Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
	}
	catch (e)
	{
		// Internet Explorer
		try
		{
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e)
		{
			try
			{
				xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e)
			{
				alert("Your browser does not support AJAX!");
				return false;
			}
		}
	}
	return xmlHttp;
}
//key typed event checking for Enter = 13
function keyPress(event)
{
	if(event.keyCode=='13')
	{
		chatFunction(document.chatForm.message.value);
		document.chatForm.message.value='';
		document.chatForm.message.focus();
		return false;
	}
	return true;
}
function checkTyping()
{
	if(action!=2)
	{
		if(document.chatForm.message.value.length!=0)
		{
			action = 1;
		}
		else
		{
			action = 0;
		}
	}
}
function chatFunction(message)
{
	var xmlHttp = initializeHttp();
	
	//checks for the state of the packet
	xmlHttp.onreadystatechange=function()
	{
		if(xmlHttp.readyState==4)
		{
			var text = xmlHttp.responseText;
			var mid = text.indexOf('|');
			var last = text.indexOf('|',mid+1);
			if(mid!=0)
				document.getElementById("output").innerHTML=text.substring(0,mid);
			if(last!=-1)
			{
				document.getElementById("users").innerHTML=text.substring(mid+1,last);
				lastTime = text.substring(last+1,text.length);
			}
			
			document.getElementById("output").scrollTop = document.getElementById("output").scrollHeight;
			if(message.length==0)
			{
				document.chatForm.message.value='';
			}
		}
	}
	//send request
	var username = document.chatForm.username.value;
	if(message.length>0)
	{
		if(message.toLowerCase()=="/afk".toLowerCase())
		{
			message = "";
			if(action!=2)
				action = 2;
			else
				action = 0;
			var url = "conversation.php?user="+username+"&action="+action+"&lasttime="+lastTime;
		}
		else
		{
			action = 0;
			var url = "conversation.php?user="+username+"&action="+action+"&lasttime="+lastTime+"&message="+message;
			document.chatForm.message.value='';
		}
	}
	xmlHttp.open("POST",url,true);
	xmlHttp.send(null);
}
function chatUpdate()
{
	var xmlHttp = initializeHttp();
	
	//checks for the state of the packet
	xmlHttp.onreadystatechange=function()
	{
		if(xmlHttp.readyState==4)
		{
			var text = xmlHttp.responseText;
			var mid = text.indexOf('|');
			var last = text.indexOf('|',mid+1);
			var isBottom = false;
			if(document.getElementById("output").scrollTop == document.getElementById("output").scrollHeight-340)
			{
				isBottom = true;
			}
			if(mid!=0)
			{
				str = text.substring(0,mid);
				str = str.replace(' ','');
				if(str.length>0)
				{
					document.getElementById("output").innerHTML=text.substring(0,mid);
					soundManager.play('chatMessage');
				}
			}
			if(last!=-1)
			{
				document.getElementById("users").innerHTML=text.substring(mid+1,last);
				lastTime = text.substring(last+1,text.length);
			}
			
			if(isBottom)
			{
				document.getElementById("output").scrollTop = document.getElementById("output").scrollHeight;
			}
		}
	}
	//send request
	var username = document.chatForm.username.value;
	var url = "conversation.php?user="+username+"&action="+action+"&lasttime="+lastTime;
	xmlHttp.open("POST",url,true);
	xmlHttp.send(null);
}