// 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;
var username = "Guest";
var dividerKey = "`";
var playSound = 1;
function initChat()
{
	username = document.chatForm.username.value;
	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.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();
		
	if(message=="/sound")
	{
		if(playSound==1)
		{
			playSound = 0;
			document.chatForm.message.value="Sound is Off";
		}
		else if(playSound==0)
		{
			playSound = 1;
			document.chatForm.message.value="Sound is On";
		}
	}
	else
	{
		//checks for the state of the packet
		xmlHttp.onreadystatechange=function()
		{
			if(xmlHttp.readyState==4)
			{
				var text = xmlHttp.responseText;
				var mid = text.indexOf(dividerKey);
				var last = text.indexOf(dividerKey,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);
				}
				
				if(playSound==1)
				{
					soundManager.play('chatMessage');
				}
				document.getElementById("output").scrollTop = document.getElementById("output").scrollHeight;
				if(message.length==0)
				{
					document.chatForm.message.value='';
				}
			}
		}
		//send request
		//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("GET",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(dividerKey);
			var last = text.indexOf(dividerKey,mid+1);
			var isBottom = false;
			var thisTime = text.substring(last+1,text.length);
			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)
				{
					if(document.getElementById("output").innerHTML.length!=0 && lastTime!=thisTime)
					{
						if(playSound==1)
						{
							soundManager.play('chatMessage');
						}
					}
					lastTime = thisTime;
					
					document.getElementById("output").innerHTML=text.substring(0,mid);
				}
			}
			if(last!=-1)
			{
				document.getElementById("users").innerHTML=text.substring(mid+1,last);
			}
			
			if(isBottom)
			{
				document.getElementById("output").scrollTop = document.getElementById("output").scrollHeight;
			}
		}
	}
	//send request
	//username = document.chatForm.username.value;
	var url = "conversation.php?user="+username+"&action="+action+"&lasttime="+lastTime;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}