jQuery(document).ready(function() {
    //Cette fonction, quand elle activée ( decommentée ) permet de tester le fonctionnement de jQuery
	jQuery("a").click(function() {
        //alert("Hello world!");
    });
	
	//---------------
	//Gestion du chat
	//---------------
	
	//Variable globales du chat
	var url="ajax/chat.deal.php";
	var timerChat = "";
	var timerConn = "";
	var pseudo = "";
	var id = "";
	var msg = "";
	var msg2 = "";
	var val = "";
	var val2 = "";
	
	//Lancement du chat
	if(jQuery("#chatContainer").get() != "") { 
		//Lancement timer
		timerChat = setInterval(getMessages, 5000);
		//Chargement options
		val2 = jQuery("#optionContainer form select :selected").val();
		jQuery("#chatViz").css("font-size", ""+val2+"px");
		jQuery("#chat").css("font-size", ""+val2+"px");
		//Connexion
		pseudo = jQuery("#chatSender form input:hidden").val();
		id = jQuery("#id").val();
		if (pseudo != "")
		{
			jQuery.post(url,{action:"setConnected", id:id, pseudo:pseudo},function(data)
			{
			if(data.erreur=="Ok")
				{
					verif_conn();
				}
				else
				{
					//alert(data.erreur);
				}
			}
			, "json");
		}
	}
	var timeError = "";
	//Fonction d'appel Ajax sur Submit
	jQuery("#chatSender form").keypress(function(event) {
		if(event.keyCode == '13')
		{
			event.preventDefault();
			jQuery("#chatSender form").submit();
		}
	});
		
	//Send Message
	jQuery("#chatSender form").submit(function() {
		clearInterval(timerChat);
		//showLoader("#chatSender");
		var message = jQuery("#chatSender form textarea").val();
		pseudo = jQuery("#chatSender form input:hidden").val();
		id = jQuery("#id").val();
		id_chat = jQuery("#activeStream").val();
		jQuery.post(url,{action:"addMessage", message:message, pseudo:pseudo, id:id, id_chat:id_chat},function(data)
		{
		if(data.erreur=="Ok")
			{
				getMessages();
				jQuery("#chatSender form textarea").val("");
			}
			else
			{
				//alert(data.erreur);
			}
			timerChat = setInterval(getMessages, 5000);
			//hideLoader();
		}
		, "json");
		return false;
	});
	
	//Gestion Erreur 403
	jQuery("#chatError").ajaxError(function(e, xhr, settings, exception) {
		if (xhr.status == 403)
		{
			jQuery("#chatError").fadeIn("slow");
			jQuery("#chat").scrollTop(jQuery("#chat").scrollTop() + 80);
			jQuery("#chatSender form textarea").attr("disabled", "disabled");
			timerError = setTimeout(callError, 5000);
		}
	}); 
	
	function callError()
	{
		jQuery("#chatError").fadeOut("slow");
		jQuery("#chatSender form textarea").removeAttr("disabled");
		clearTimeout(timerError);
		getMessages();
		clearInterval(timerChat);
		timerChat = setInterval(getMessages, 5000);
	}
	
	//Fonction de recup des messages
	function getMessages()
	{
		id_chat = jQuery("#activeStream").val();
		id = jQuery("#id").val();
		pseudo = jQuery("#chatSender form input:hidden").val();
		jQuery.post(url,{action:"getMessage", lastid:lastid, id:id, id_chat:id_chat},function(data)
		{
		if(data.erreur=="Ok")
			{
				jQuery("#rchat").append(data.result);
				lastid=data.lastid;
				if(data.result != "")
				{
					jQuery("#chat").scrollTop(jQuery("#chat").scrollTop() + 80);
					bindChatHover2();
				}
				if(data.mute != "Ok")
				{
					jQuery("#chatEnter").fadeOut("slow", function() {
						jQuery("#chatMute").empty();
						jQuery("#chatMute").append(data.mute);
						jQuery("#chatMute").fadeIn("slow", function() { 
						});
					});
				}
				else
				{
					jQuery("#chatMute").fadeOut("slow", function() {
						jQuery("#chatEnter").fadeIn("slow");
					});
				}
			}
			else
			{
				//alert(data.erreur);
			}
			//hideLoader();
		}
		, "json");
	}
	
	function verif_conn() {
			clearInterval(timerConn);
			clearInterval(timerChat);
			pseudo = jQuery("#chatSender form input:hidden").val();
			id = jQuery("#id").val();
		jQuery.post(url,{action:"getConnected", id:id, pseudo:pseudo},function(data)
			{
			if(data.erreur=="Ok")
				{
					jQuery("#connected").empty();
					jQuery("#connected").append(data.result);
					timerConn = setInterval(verif_conn, 60000);
					timerChat = setInterval(getMessages, 5000);
					bind_connected();
				}
				else
				{
					//alert(data.erreur);
				}
			}
			, "json");
	}
	
	//Fonctions du loader
	function showLoader(div) {
		jQuery(div).append("<div id='loader'>Chargement... </div>");
	}
	
	function hideLoader() {
		jQuery("#loader").fadeOut();
	}
	
	jQuery("#chatSender form textarea").removeAttr("disabled");
	
	//Positionner le scroll
	jQuery("#chat").scrollTop(7000);
	
	var nick
	function bind_connected()
	{
		jQuery("#connected > p").click(function(event) {
			jQuery(this).next().show("slow");
		});
		jQuery("#connected form").submit(function() {
			pseudo = jQuery("input:hidden", (this)).val();
			id = jQuery("input:hidden", (this)).next().val();
			raison = jQuery("input:text", (this)).val();
			duree = jQuery("input:text", (this)).next().val();
			jQuery.post(url,{action:"setMute", id:id, pseudo:pseudo, raison:raison, duree:duree},function(data)
			{
			if(data.erreur=="Ok")
				{
					msg = "<span class='red'> a rendu muet "+pseudo+" pour "+duree+" secondes, pour la raison : "+raison+".</span>";
					set_announce(msg);
				}
				else
				{
					alert(data.erreur);
				}
			}
			, "json");
			return false;
		});
	}
	
	function set_announce(msg)
	{
		clearInterval(timerChat);
		pseudo = jQuery("#chatSender form input:hidden").val();
		id = jQuery("#id").val();
		jQuery.post(url,{action:"setAnnouce", message:msg, pseudo:pseudo, id:id},function(data)
		{
		if(data.erreur=="Ok")
			{
				getMessages();
				jQuery("#chatSender form textarea").val("");
			}
			else
			{
				//alert(data.erreur);
			}
			timerChat = setInterval(getMessages, 5000);
			//hideLoader();
		}
		, "json");
	}
	
	jQuery("#announce form").submit(function() {
		msg2 = jQuery("#announce form input:text").val();
		msg2 = "<span class='vrai'>Annonce : "+msg2+" </span>";
		set_announce(msg2);
		return false;
	});
	
	bindClick();
	
	function bindClick() {
	jQuery("#optionsChat a").click(function(event) {
        jQuery("#optionContainer").css("display", "block");
		jQuery("#chat").css("display", "none");
		jQuery("#chatSender form textarea").attr("disabled", "disabled");
		jQuery("#optionsChat").css("padding", "0px 3px 2px 5px");
		event.preventDefault();
		bindClickBack();
    });
	}
	
	function bindClickBack() {
	jQuery("#optionsChat a").click(function(event) {
		jQuery("#optionContainer").css("display", "none");
		jQuery("#chat").css("display", "block");
		jQuery("#chat").scrollTop(7000);
		jQuery("#chatSender form textarea").removeAttr("disabled");
		jQuery("#optionsChat").css("padding", "0px 3px 0px 5px");
		event.preventDefault();
		bindClick();
	});
	}
	
	jQuery("#optionContainer form select").change(function() {
		jQuery("#optionContainer form").submit();
	});
	
	jQuery("#optionContainer form").submit(function() {
		val = jQuery("#optionContainer form select :selected").val();
		id = jQuery("#id").val();
		jQuery("#chatViz").css("font-size", ""+val+"px");
		jQuery("#chat").css("font-size", ""+val+"px");
		jQuery.post(url,{action:"setOption", id:id, taille:val},function(data)
			{
			if(data.erreur=="Ok")
				{
				}
				else
				{
					alert(data.erreur);
				}
			}
			, "json");
		return false;
	});
	
	//Options - Popup
	bindClickPop();
	
	function bindClickPop() {
	jQuery("#optionContainer a").click(function(event) {
		bindClickOff();
		clearInterval(timerChat);
		jQuery("#optionContainer a").text("Re-activer le Chat sur cette page");
		jQuery("#optionContainer ul").next().next().next().hide();
		jQuery("#optionContainer a").removeAttr("onClick");
		bindClickBackPop();
		event.preventDefault();
    });
	}
	
	function bindClickBackPop() {
	jQuery("#optionContainer a").click(function(event) {
		jQuery("#optionsChat").empty();
		jQuery("#optionsChat").append("<a href=''><span class='titreOption'>// Options </span><img src='images/icons/design/rouage.png' border='0' /></a>");
		jQuery("#optionContainer a").text("Détacher le Chat");
		jQuery("#optionContainer ul").next().next().next().show();
		jQuery("#optionContainer a").attr("onClick", "wc = window.open('engine/popup.php','HiScoreTvChat','toolbar=0, location=0, directories=0, status=1, scrollbars=0, copyhistory=0, menuBar=0, width=500, height=450, left=100, top=200')");
		bindClickPop();
		bindClickBack();
	});
	}
	
	function bindClickOff() {
	jQuery("#optionsChat").empty();
	jQuery("#optionsChat").append("<span class='titreOption'>// Options </span><img src='images/icons/design/rouage.png' border='0' />");
	}
	
	bindChatHover();
	function bindChatHover() 
	{
		$("#chat > p").hover(function() {
			$(this).children(".msgTime").show();
		},
		function() {
			$(this).children(".msgTime").hide();
		});
	}
	
	function bindChatHover2() 
	{
		$("#rchat > p").hover(function() {
			$(this).children(".msgTime").show();
		},
		function() {
			$(this).children(".msgTime").hide();
		});
	}
});
