var $j = jQuery.noConflict();
var liveChatIntervalID;
var techChatIntervalID;
var clientTypingTimeout;
var renewTechXHR = $j.ajax();
var deliverChatXHR = $j.ajax();
var chatCreatorXHR = $j.ajax();
var renewChatsXHR = $j.ajax();
var doneTypingXHR = $j.ajax();
var sayIsTypingXHR = $j.ajax();
var chatRenewalIntervalTime = 3000;

$j(document).ready(function () {
	if($j("#chat_id").val() != "") {
		chatID = $j("#chat_id").val();
		renewChat(chatID);
		openUserChat("team");
	}
	if($j("#chat_tech_id").val() != "") {
		chatID = $j("#chat_tech_id").val();
		renewChat(chatID);
		openUserChat("tech");
	}
	
	$j("#open_new_team_chat, .open_new_team_chat").click(function () {
		if($j("#chat_id").val() == "") {
			createNewChat("team");
		}else{
			alert("Live chat session is already running");
		}
		return false;
	});
	$j("#open_new_tech_chat, .open_new_tech_chat").click(function () {
		if($j("#chat_tech_id").val() == "") {
			createNewChat("tech");
		}else{
			alert("Tech Support chat session is already running");
		}
		return false;
	});
	
	$j("#minamize_chat").click(function () {
		$j("#live_chat_message_container").slideToggle("slow");
		return false;
	});
	$j("#minamize_tech_chat").click(function () {
		$j("#chat_tech_message_container").slideToggle("slow");
		return false;
	});
	$j("#close_chat").click(function () {
		var chatID = $j("#chat_id").val();
		if($j(".client_chat_box").hasClass("chat_ended")){
			if(confirm("Are you sure you have finished with these messages?")) {
				closeChat(chatID, "team", "chat_has_ended");
			}
		}else{
			if(confirm("Are you sure you want to end this chat?")) {
				closeChat(chatID, "team", "end_the_chat");
			}
		}
		return false;
	});
	$j("#close_tech_chat").click(function () {
		var chatID = $j('#chat_tech_id').val();
		if($j(".client_tech_chat_box").hasClass("chat_ended")){
			if(confirm("Are you sure you have finished with these messages?")) {
				closeChat(chatID, "tech", "chat_has_ended");
			}
		}else{
			if(confirm("Are you sure you want to end this chat?")) {
				closeChat(chatID, "tech", "end_the_chat");
			}
		}
		return false;
	});
	$j("#client_chat").keyup(function(eventObj){
		if(eventObj.keyCode == 13){
			sendChat("live"); 
		}else{
			var chatID = $j("#chat_id").val();
			sayClientIsTyping(chatID);
		}
	});
	$j("#client_tech_chat").keyup(function(eventObj){
		if(eventObj.keyCode == 13){
			sendChat("tech");
		}else{
			var chatID = $j('#chat_tech_id').val();
			sayClientIsTyping(chatID);
		}
	});
	$j("#send_client_chat").click(function () {
		sendChat("live");
		return false;
	});
	$j("#send_client_tech_chat").click(function () {
		sendChat("tech");
		return false;
	});
	
	
	
	
	
});
/* !___end document.ready___ */

function sendChat(chatType){
	var targetChat = (chatType == "tech") ? $j(".client_tech_chat_box") : $j(".client_chat_box");
	var chatMessage = (chatType == "tech") ? $j("#client_tech_chat").val() : $j("#client_chat").val();
	deliverChatXHR.abort();
	deliverChatXHR = $j.ajax({
		type: "POST",
		url: "/base_ajax_php/client_chat_functions.php?function=send_message",
		data: "function=send_message&chat_type="+chatType+"&message="+ encodeURI(chatMessage),
		success: function(resp){
			//alert(resp);
			targetChat.find(".chat_textarea").val("");
			targetChat.find(".chat_message_list_container ul").html(resp);
			scrollDownChats(targetChat);
		}
	});
}
function createNewChat(chatType){
	chatCreatorXHR.abort();
	chatCreatorXHR = $j.ajax({
		type: "POST",
		url: "/base_ajax_php/client_chat_functions.php?function=create_new_chat", 
		data: "function=create_new_chat&chatType="+chatType+"&siteID="+$j("#host_id").val(),
		success: function(resp){
			switch(chatType){
				case "team": 
					$j("#chat_id").val(resp);
					break;
				case "tech":
					$j("#chat_tech_id").val(resp);
					break;
			}	
			renewChat(resp);
			openUserChat(chatType);
		}		
	});
}
function openUserChat(chatType) {
	switch(chatType) {
		case "team": 
			var chatID = $j("#chat_id").val();
			var targetChat = $j(".client_chat_box");
			var sisterChat = $j(".client_tech_chat_box");
			var messagesCount = parseInt($j("#chat_count").val());
			liveChatIntervalID = window.setInterval("renewChat("+chatID+")", chatRenewalIntervalTime);
			break;
		case "tech": 
			var chatID = $j("#chat_tech_id").val();
			var targetChat = $j(".client_tech_chat_box");
			var sisterChat = $j(".client_chat_box");
			var messagesCount = parseInt($j("#chat_tech_count").val());
			techChatIntervalID = window.setInterval("renewTechChat("+chatID+")", chatRenewalIntervalTime);
			break;
		default : 
			return false;
			break;
	}
	targetChat.css("right", "0px");
	//if(sisterChat.css("display") == "block" && sisterChat.css("right") == "0px") {
		//targetChat.css("right", "296px");
	//}
	if($j("#chat_id").val() != "") {
		$j(".client_tech_chat_box").css("right", "296px");
	}
	targetChat.css("display","block");
	targetChat.find('.chat_disabler_overlay').css("display", "block");
	if(messagesCount > 0){
		targetChat.find('.chat_disabler_overlay').css("display", "none");
	}
	scrollDownChats(targetChat);
	$j(".video_floater").css("display", "none");
	$j("#floater").css("display","block");
}
function closeChat(chatID, chatType, affirmClose) {
	switch(chatType) {
		case "team": 
			var targetChat = $j(".client_chat_box");
			clearInterval(liveChatIntervalID);
			//chatID = "";
			$j("#chat_id").val("");
			$j("#chat_count").val("0");
			if($j("#chat_tech_id").val() != "") {
				$j(".client_tech_chat_box").css("right", "0px");
			}
			break;
		case "tech": 
			var targetChat = $j(".client_tech_chat_box");
			clearInterval(techChatIntervalID);
			//chatTechSession = "";
			$j('#chat_tech_id').val("");
			$j("#chat_tech_count").val("0");
			break;
	}	
	targetChat.css("display","none");
	targetChat.find(".chat_textarea").val("");
	targetChat.find(".chat_message_list_container ul").html("");
	targetChat.find(".chat_disabler_overlay").css("display", "block");
	
	if(affirmClose == "chat_has_ended"){
		$j.ajax({
			type: "POST",
			url: "/base_ajax_php/client_chat_functions.php?function=affirm_close_chat",
			data: "function=affirm_close_chat&chatID="+chatID,
			success: function(resp){
				//alert("Bye");
			}
		});
	}else{
		$j.ajax({
			type: "POST",
			url: "/base_ajax_php/client_chat_functions.php?function=mark_close_chat", 
			data: "function=mark_close_chat&chatID="+chatID,
			success: function(resp){
				alert('You have ended this chat. Our support team will be notified.');
			}
		});
	}
}
function sayClientIsTyping(chatID){
	clearTimeout(clientTypingTimeout);
	sayIsTypingXHR.abort();
	sayIsTypingXHR = $j.ajax({
		type: "POST",
		url: "/base_ajax_php/client_chat_functions.php?mark_client_typing",
		data: "function=mark_client_typing&chatID="+chatID,
		success: function(resp){
			//Nothing to change here
		}
	});
	clientTypingTimeout = setTimeout('sayClientIsDoneTyping('+chatID+')', 4000);
}
function sayClientIsDoneTyping(chatID){
	doneTypingXHR.abort();
	doneTypingXHR = $j.ajax({
		type: "POST",
		url: "/base_ajax_php/client_chat_functions.php?mark_end_client_typing",
		data: "function=mark_end_client_typing&chatID="+chatID,
		success: function(resp){
			//Nothing to change here
		}
	}); 
}
function renewChat(chatID) {
	renewChatsXHR.abort();
	renewChatsXHR = $j.ajax({
		type: "POST",
		url: "/base_ajax_php/client_chat_functions.php?get_chat_history",
		data: "function=get_chat_history&chatID="+chatID,
		success: function(resp){
			//alert(resp);
			$j(".client_chat_box").find(".chat_message_list_container ul").html(resp);
			$j("#chat_count").val($j(".chat_message").length);
			if($j(".chat_message.message_untaken").length > 0){
				scrollDownChats($j(".client_chat_box"));
			}
			if(parseInt($j("#chat_count").val()) > 1){
				$j(".client_chat_box").find('.chat_disabler_overlay').css("display", "none");
			}
			if($j(".client_chat_box li.fyi_chat_ended").length > 0){
				$j(".client_chat_box").addClass("chat_ended");
				$j(".client_chat_box .header_text").html("Live Chat (Chat has ended)");
				$j(".client_chat_box").find(".chat_disabler_overlay").css("display", "block");
			}else{
				$j(".client_chat_box").removeClass("chat_ended");
				$j(".client_chat_box .header_text").html("Live Chat");
			}
		}
	}); 
}
function renewTechChat(chatID) {
	renewTechXHR.abort();
	renewTechXHR = $j.ajax({
		type: "POST",
		url: "/base_ajax_php/client_chat_functions.php?get_chat_history",
		data: "function=get_chat_history&chatID="+chatID,
		success: function(resp){
			//alert(resp);
			$j(".client_tech_chat_box").find(".chat_message_list_container ul").html(resp);
			$j("#chat_tech_count").val($j(".chat_message").length);
			if($j(".chat_message.message_untaken").length > 0){
				scrollDownChats($j(".client_tech_chat_box"));
			}
			if(parseInt($j("#chat_tech_count").val()) > 1){
				$j(".client_tech_chat_box").find('.chat_disabler_overlay').css("display", "none");
			}
			if($j(".client_tech_chat_box li.fyi_chat_ended").length > 0){
				$j(".client_tech_chat_box").addClass("chat_ended");
				$j(".client_tech_chat_box .header_text").html("Tech Support Chat (Chat has ended)");
				$j(".client_tech_chat_box").find(".chat_disabler_overlay").css("display", "block");
			}else{
				$j(".client_tech_chat_box").removeClass("chat_ended");
				$j(".client_tech_chat_box .header_text").html("Tech Support Chat");
			}
		}
	}); 
}
function scrollDownChats(chatBoxObj){
	var chatScrollHeight = 0;
	chatBoxObj.find('.chat_message_list_container').find("li").each(function(){
		chatScrollHeight = chatScrollHeight + parseInt($j(this).height());
	});
	chatBoxObj.find('.chat_message_list_container').animate({
		scrollTop: chatScrollHeight
	}, 300);
}

