chkTyping = "";
chkAlive = "";
chkMessages = "";

function laOpen()
{
	if(!$("#live_assist_container").attr('id'))
	{
		$.post("/jquery/chat.php?action=open",
		function(data){
			$("#live_assist").html(data);
		});	
	}
}

function laClose()
{
	$.post("/jquery/chat.php?action=close",
	function(data){
		$("#live_assist_container").remove();
		clearTimeout(chkTyping);
		clearTimeout(chkAlive);
	});		
}
function laMin()
{
	if($("#live_assist_messages").css('display') == 'none')
	{
		$("#live_assist_messages").css('display','block');
		$("#live_assist_txt").css('display','block');
		$("#live_assist_container").css('height','285px');
		$("#live_assist_container").css('width','260px');
		$("#live_assist_header").css('width','250px');
		$("#live_assist_header").css('background-color','#0099CB');
		$("#live_assist_header").css('color','#FFF');
		$("#live_assist_messages").animate({scrollTop: $("#live_assist_messages").prop("scrollHeight") - $('#live_assist_messages').height() }, 100);
	}
	else
	{
		$("#live_assist_messages").css('display','none');
		$("#live_assist_txt").css('display','none');
		$("#live_assist_container").css('height','25px');
		$("#live_assist_container").css('width','158px');
		$("#live_assist_header").css('width','148px');
		$("#live_assist_header").css('background-color','#EDEDED');
		$("#live_assist_header").css('color','#666666');
		$("#live_assist_close").css('display','none');
	}
}

function laOver()
{
	$("#live_assist_close").css('display','block');
}

function laOut()
{
	if($("#live_assist_messages").css('display') == 'none')
	{
		$("#live_assist_close").css('display','none');
	}
}

function addMessage(field)
{
	var message = jQuery.trim($(field).val());
	if(message != "")
	{
		$.post("/jquery/chat.php?action=addmessage",
		{
			message: message
		},
		function(data){
			
		});
		$(field).val('');
	}
}

function eventChatHandle (field, event) {
	var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
	
	if (keyCode == 13) {
	
		if(event.shiftKey == false)
		{
			if(event.preventDefault) 
				event.preventDefault(); 
			else
				event.returnValue = false; 
			
			addMessage(field);
			
			$.post("/jquery/chat.php?action=showMessages",
			function(data){
				$("#live_assist_messages").html(data);
				$("#live_assist_messages").animate({scrollTop: $("#live_assist_messages").prop("scrollHeight") - $('#live_assist_messages').height() }, 100);
			});
		}
	} 
	
}

function checkMessages()
{
	var aantal = $(".CHmessage").length;
	$.post("/jquery/chat.php?action=checkMessages&aantal=" + aantal,
	function(data){
		if(data == 'true')
		{
			$.post("/jquery/chat.php?action=showMessages",
			function(data){
				$("#live_assist_messages").html(data);
				$("#live_assist_messages").animate({scrollTop: $("#live_assist_messages").prop("scrollHeight") - $('#live_assist_messages').height() }, 100);
				if($("#live_assist_messages").css('display') == 'none')
				{
					$("#live_assist_header").css('background-color','#64FF64');	
				}
				
				if(!$("#live_assist_container").attr('id'))
				{
					$.post("/jquery/chat.php?action=lastMessagesShop",
					function(data){
						if(data == 'true')
						{
							laOpen();
						}
					});
				}
			});
		}
	});	

	chkMessages = setTimeout(checkMessages, 3000);
}


function online()
{
	$.post("/jquery/chat.php?action=online");
	
	setTimeout(online, 59000);		
}

function showTyping()
{
	$.post("/jquery/chat.php?action=showTyping",
	function(data){
		if(data == 'true')
		{
			$("#CHEmes").css('display', "block");
		}
		else
		{
			$("#CHEmes").css('display', "none");
		}
	});
	
	chkTyping = setTimeout(showTyping, 3000);
}

function setAlive()
{
	$.post("/jquery/chat.php?action=setAlive");
	chkAlive = setTimeout(setAlive, 40000);
}

online();
checkMessages();
