var chatId=0;
var private=false;
var privateId=0;
var userId;
var font=13;
$(window).unload(endChat);


//-----------RUNS ONLOAD, SETS USERNAME, CALLS getText('0'), CALLS getUser(), INITIALIZER---------------------//
function startChat() { 
	$.ajax({
		url: 'include/startChat.php',
		type: 'GET',
		dataType: 'html',
		data: "id="+idNum+"&name="+userName+"&gender="+gender, 
		success: function(html){
			userId=html;
			sendChat("<em><b>entered the chat.</b></em>");
			getText();
			getUser();
		//	endChat();
			callExpire();
		},
		error: function(request,status,error){
			//alert('Error! status: '+status+" error: "+error+" request: "+request);
		}
		
	})
}

//----------------------REMOVES THE USER FROM THE DB----------------------------------------------------------------------------------//
function endChat() { 
	if(typeof(userName)!='undefined'){
		sendChat("<em><b>left the chat.</b></em>");
		var anticache=new Date().getTime();
		//alert(userName+' '+idNum+' '+anticache)
		$.ajax({
			async: false,
			url: 'include/endChat.php',
			type: 'GET',
			dataType: 'html',
			data: 'userName='+userName+"&id="+idNum+"&c="+anticache,
			success: function(html){
				delete userName;
				//alert("YOU HAVE LOGGED OUT");
			},
			error: function(request,status,error){
			//	alert('Error! status: '+status+" error: "+error+" request: "+request);
				endChat();
			}	
		})
	}
}
//---------------RUNS WHEN TEXT IS ENTERED, ENTERS TEXT INTO THE DB, CALLS getText()-----------------------------------//
function sendChat(text) {
//	alert("sending Chat!");
	var anticache=new Date().getTime();
	if(text!=undefined){
		$('#textField').attr("value","");
		//alert("Sending... "+ text + " done");
		$.ajax({
			url: '/include/handleText.php',
			type: 'POST',
			dataType: 'html',
			data: 'id='+idNum+'&message='+text+"&name="+userName+"&gender="+gender+"&private="+private+"&privateId="+privateId+"&userId="+userId+"&cache="+anticache,
			success: function(html){
			//	alert("success on the sending!");
				clearInterval(tTimer);
				getText();
				clearInterval(eTimer);
				callExpire();
				if(private==1){
					$("#chatField").append("<p><i>Private Message to "+privateName+": "+text+"</i></p>");
				}
				private=false;
				privateId=0;
				$(".message").html("");
				return false;
			},
			error: function(request,status,error){
				//alert('Error! status: '+status+" error: "+error+" request: "+request);
			}		
		})
	}
}

//------------------Sets up a variables for a private chat, they are cleared out everytime a new message is sent---------------//
function privateChat(privId, privName){
	if(private){
		private=false;
		privateId=0;
		privateName='';
		$(".message").html("");
	}
	else{
		private=true;
		privateId=privId;
		privateName=privName;
		$(".message").html("Enter your private message to "+privName+":");
		$('#textField').focus();
	}
}
//-----------------RETURNS THE CHAT MESSAGES, IF FIRST TIME, RETURNS FIRST 10 MESSAGES, ELSE RETURNS ALL NEW MESSAGES---------//
function getText() {
	//alert(idNum);
	//alert('GETTEXT!');
	var anticache=new Date().getTime();
	var chat_div = document.getElementById('chatField');
	$.ajax({
		url: 'include/handleText.php',
		type: 'GET',
		dataType: 'xml',
		data: 'chatId='+chatId+'&id='+idNum+"&userId="+userId+"&cache="+anticache,
		async: false,
		success: function(xml){
		//	alert(xml);
		//	alert($(this).attr("id"));
			
		//	alert("success one the getText()");
			$(xml).find("message").each(function(){
					if($(this).attr("id")!=undefined){
					chatId= $(this).attr("id");
				//	alert("got new stuff: "+$(this).attr("text"));
					if($(this).attr('private')=='1'){
						var p='<i>Private message from</i> ';
					}
					else{
						var p='';
					}
					$("#chatField").append("<p>" + $(this).attr("time") +"-"+p+"<strong class=\""+$(this).attr("gender")+"\">" + $(this).attr("user") + ":</strong> " + $(this).attr("text")+"</p>");
					//$("#chatField").scrollTop = $("#chatField").scrollHeight;
					chat_div.scrollTop = chat_div.scrollHeight;
				}
			});
		},
		error: function(request,status,error){
			//alert('Error! status: '+status+" error: "+error+" request: "+request);
		}
	});
	callText();
}


//---------------------RETURNS A LIST OF ONLINE USERS------------------------------------------------------------------------------//
function getUser() {
	var anticache=new Date().getTime();
	$.ajax({
		url: 'include/getUser.php',
		type: 'GET',
		dataType: 'html',
		data: 'id='+idNum+"&chache="+anticache,
		success: function(html){
			//alert(html);
			$("#userList").html(html);
		},
		error: function(request,status,error){
			//alert('Error! status: '+status+" error: "+error+" request: "+request);
		}
	});
	callUser();
}

//----------------------CALLS getUser() AFTER X SECONDS----------------------------------------------------------------------------------//
function callUser() {
	uTimer=setTimeout('getUser();', 4000);
}

//----------------------CALLS getText() AFTER X SECONDS----------------------------------------------------------------------------------//
function callText() {
	tTimer=setTimeout('getText();', 4000);
}

function callExpire(){
	eTimer=setTimeout('window.location="secret.php"',  900000)
}

//Runs When the user submits the form
function hitEnter() {
	var text=$('#textField').attr("value");
	sendChat(text);
	return false;
}

//clears the value of an element
function clearVal(el){
	$(el).attr("value","");
	$(el).attr("onfocus","");
}

function fontSmall(){
	if(font>8 && font<20){
		font--;
		$("#chatField").css("font-size",font);
	//	$(".chat-input").css("font-size",font);
	}
	return false;
	
}
function fontBig(){
	if(font>7 && font<19){
		font++;
		$("#chatField").css("font-size",font);
	//	$(".chat-input").css("font-size",font);
	}
	return false;
}
function fontDefault(){
	font=13;
	$("#chatField").css("font-size",font);
//	$(".chat-input").css("font-size",font);	
	return false;
}

function logOut(){
	endChat();
	window.location='index.php';
}