var aoOption = new Hash();
var strOutil = "";
var aoTypePari;
var oTimeoutId;

function icone_event(event) {
	event.preventDefault();

	strName = $(this).attr('name') + "_" + $(this).attr('matchid');

	$("form :checkbox").each(function () {
		if ($(this).is(':checked')) {
			strPari = "OT" + $(this).attr('value');

			oLignes = $("tr[name='" + strPari + "_" + strName + "']");

			//Si la ligne existe
			if (oLignes.length > 0) {
		        if (oLignes.is(":hidden")){
		        	oLignes.fadeIn();
		        	aoOption.setItem(strName,"true");
		        }
		        else {
		        	oLignes.fadeOut();
		        	aoOption.removeItem(strName);
		        }
			}
		}
	});
}

function affiche_pari(event) {
		
	strPari = "OT" + $(this).attr('value');

	if ($(this).is(':checked')) {
		aoTypePari.setItem($(this).attr('value'),true);
	    $("tr[name='" + strPari + "_cote']").show();
	    $("tr[name^='" + strPari + "_redistribution']").show();

	    for (i in aoOption.items) {
	    	$("tr[name='" + strPari + "_" + i + "']").show(); 
	    }
	    
	    if (strOutil != "") {
	    	$("tr[name^='" + strPari + "_" + strOutil + "']").show(); 
	    }
	 }
	else {
		aoTypePari.removeItem($(this).attr('value'));
	    $("tr[name='" + strPari + "_cote']").hide();
	    $("tr[name^='" + strPari + "_redistribution']").hide();

	    for (i in aoOption.items) {
	    	$("tr[name='" + strPari + "_" + i + "']").hide(); 
	    }
	    
	    if (strOutil != "") {
	    	$("tr[name^='" + strPari + "_" + strOutil + "']").hide(); 
	    }
	}
}

function update(blnLoad) {
	var leagueid = $("#leagueid").val();
	var matchid = $("#matchid").val();
	var datemin = $("#datemin").val();
	var datemax = $("#datemax").val();
	var sport = $("#sport").val();
	var ids = $("#ids").val();
	var valeur = $("#valeur").val();
	var bookmaker = $("#bookmaker").val();
	var ordretri = $("#ordretri").val();
	strOutil = $("#strOutil").val();

	strData = "";
	if (datemax != "") {
		strData += "datemax="+datemax+"&";
	}
	if (datemin != "") {
		strData += "datemin="+datemin+"&";
	}
	if (matchid != "") {
		strData += "matchid="+matchid+"&";
	}
	if (leagueid != "") {
		strData += "leagueid="+leagueid+"&";
	}
	if (sport != "") {
		strData += "sport="+sport+"&";
	}
	if (ids != "") {
		strData += "ids="+ids+"&";
	}
	if (valeur != "") {
		strData += "valeur="+valeur+"&";
	}
	if (bookmaker != "") {
		strData += "bookmaker="+bookmaker+"&";
	}
	if (ordretri != "") {
		strData += "ordretri="+ordretri+"&";
	}
	if (strOutil != "") {
		strData += "strOutil="+strOutil+"&";
	}

	strData = strData.substring(0,strData.length-1);
	
	if (!blnLoad) {
		//Mise à jour de l'entête des sports
		$.ajax({
			   type: "GET",
			   url: "/comparateur/includes/header_sport.php",
			   data: strData,
			   cache: false,
			   dataType: "html",
			   success: function(data){	     
			     $("#header_sport").html(data);
			   },
		 	   error: function (XMLHttpRequest, textStatus, errorThrown) {
				   //window.setTimeout(update, 60000);
			   }
						   
			 });
	
		//Mise à jour des types de pari
		$.ajax({
			   type: "GET",
			   async: false,
			   url: "/comparateur/includes/type_pari.php",
			   data: strData,
			   cache: false,
			   dataType: "html",
			   success: function(data){	 
			     $("#type_pari").html(data);

			     if (aoTypePari != null) {
			    	 //Pour tous les checkbox, si dans liste, check, sinon, uncheck
			    	 $("form :checkbox").each(function () {
			    		if (aoTypePari.hasItem($(this).attr('value'))) {
			    			$(this).attr('checked',true);
			    		}
			    		else {
			    			$(this).attr('checked',false);
			    		}
			    	 }); 
			     }
			     
			     //Evenement click des checkbox
			     $("form :checkbox").click(affiche_pari); 
			   },
		 	   error: function (XMLHttpRequest, textStatus, errorThrown) {
				   //window.setTimeout(update, 60000);
			   }
						   
			 });
	}
	
	if (aoTypePari == null) {
   	    aoTypePari = new Hash();

   	    //Remplir avec tous les checked
   	    $("form :checkbox").each(function () {
   		    if ($(this).attr('checked')) {
   			    aoTypePari.setItem($(this).attr('value'),true);
   		    }
   	    }); 
    }
	
	//Mise à jour des cotes
	$.ajax({
		   type: "GET",
		   url: "/comparateur/cote_match_display.php",
		   data: strData,
		   cache: false,
		   dataType: "html",
		   success: function(data){	 
		     $("#cotes_matchs").html(data);
		     //Affiche les cotes pour tous les paris selectionnes
		     $("form :checkbox").each(affiche_pari);
    
		     $("a[name='probabilite']").click(icone_event);	

		     $("a[name='valuebet']").click(icone_event);	   

		     $("a[name='surebet']").click(icone_event);
		     
		     //Si changement de cote, jouer un son
		     if (isUpdate()) {
		    	 jouerSon("/comparateur/audio/butcanal02.mp3");
		     }
		   },
	 	   error: function (XMLHttpRequest, textStatus, errorThrown) {
			   //window.setTimeout(update, 60000);
			   //alert("erreur");
		   }
					   
		 });
	
	oTimeoutId = window.setTimeout(update, 60000);
}

$(document).ready(function() {
	update(true);
	
	//Evenement click des checkbox
    $("form :checkbox").click(affiche_pari); 
    
    $("#conditionutilisation").click(displayHide);
    
	//menu bannière
	var name = "#divbanners";
		var menuYloc = null;

	menuYloc = 10; //parseInt($(name).css("top").substring(0,$(name).css("top").indexOf("px")));
	
	$(window).scroll(function () {
		 if ($(document).scrollTop() > 285) {
			 
		     offset = menuYloc+$(document).scrollTop()+"px";

		     $(name).show();
		     $(name).animate({top:offset},{duration:300,queue:false});
		 }
		 else {
			 $(name).hide();
		 }
 	});

});

function isUpdate() {
	aoUpdate = $(".update1");	
	
    if (aoUpdate.length > 0) {
    	if (aoUpdate.is(":visible")){
    		return true;
    	}
    	else {
    		return false;
    	}
    }
    
    return false;
}

function displayHide(event) {
	event.preventDefault();
	
	strName = "divconditionutilisation";
	
	oDiv = $("#"+strName);
	
	if (oDiv.is(":visible")) {
		oDiv.fadeOut("slow");
	}
	else {
		oDiv.fadeIn("slow");
	}
}

function controleSon(objImg) {
	blnSon = getCookie("blnSon");
	strPopup = "";
	
	//Si le son est deja actif
	if (blnSon == "true") {
	    setCookie("blnSon","false");
	    
		objImg.src = "/comparateur/images/son_off.gif";
		
		strPopup = "Activer le son lors d\'un changement de cote";
	}
	else {
		setCookie("blnSon","true");
		
		objImg.src = "/comparateur/images/son_on.gif";
		strPopup = "Désactiver le son lors d\'un changement de cote";
	}
	
	cache();
	
	//Si IE
	if (document.all) {
		objImg.onmouseover = new Function("montre(\"" + strPopup + "\");");
	}
	else {		
		objImg.setAttribute("onmouseover","montre(\"" + strPopup + "\")");
	}
}

function jouerSon(strFichierSon) {

	if (strFichierSon != "") {
		blnSon = getCookie("blnSon");
	
		//Si le son actif
		if (blnSon == "true") {
			setUrl(strFichierSon);
			play();
		}
	}
}

function rechercher() {
	$("#bookmaker").attr('value', $("#filtre_book").val()); 
	$("#ids").attr('value', $("#filtre_sport").val());
	$("#valeur").attr('value',$("#filtre_valeur").val());
	$("#ordretri").attr('value', $("#filtre_ordretri").val());
	
	//afficher image attente
	$("#cotes_matchs").html("<center><img border='0' width='50' height='50' src='/comparateur/images/ajax-loader.gif' /></center>");
	$("#type_pari").html("");
	
	//Arreter le timeout en cours
	window.clearTimeout(oTimeoutId);

	//Initialiser le tableau des types de pari
	aoTypePari = null;

	//updater zone typepari et listematch
	update();
	
	return false;
}

function getCookieVal(offset) {
	var endstr=document.cookie.indexOf (";", offset);
	if (endstr==-1) endstr=document.cookie.length;
	return unescape(document.cookie.substring(offset, endstr));
}

function setCookie(strName, strValue) {
	var argv=setCookie.arguments;
	var argc=setCookie.arguments.length;
	var expires=(argc > 2) ? argv[2] : null;
	var path=(argc > 3) ? argv[3] : "/";
	var domain=(argc > 4) ? argv[4] : null;
	var secure=(argc > 5) ? argv[5] : false;
	document.cookie=strName+"="+escape(strValue)+
	((expires==null) ? "" : ("; expires="+expires.toGMTString()))+
	((path==null) ? "" : ("; path="+path))+
	((domain==null) ? "" : ("; domain="+domain))+
	((secure==true) ? "; secure" : "");
}

function getCookie(strName) {
	var arg=strName+"=";
	var alen=arg.length;
	var clen=document.cookie.length;
	var i=0;
	while (i<clen) {
		var j=i+alen;
		if (document.cookie.substring(i, j)==arg) return getCookieVal(j);
		i=document.cookie.indexOf(" ",i)+1;
		if (i==0) break;
	}
	return null;
}