function ChangerWidget(idChronokif ,afterFinish, choix, visibilite, doRefresh){
	$.ajax({
		  type: "POST",
		  url: "index.php?module=front&cont=chronokif&action=changerwidget",
		  data : { chx: choix , idc: idChronokif },
		  success : function(data) {  
		  	afterFinish(data, idChronokif, choix, visibilite);	
			
			if (choix == 1) {
				var message = '<div title="' + data + '">Le Chronokif <span style="color:#fea31a; font-style:italic">' + data + '</span> a bien été ajouté à tes CKs !</div>';
				$(message).dialog({
					width: 430,
					close: function(event, ui) { 
								if(doRefresh == 1){
									$refreshPage = window.location.reload(true);
								} 
						   }
				});
			}
			else {
				var message = '<div title="' + data + '">Le Chronokif <span style="color:#fea31a; font-style:italic">' + data +  '</span> a bien été retiré de tes CKs !</div>';
				$(message).dialog({
					width: 430,
					close: function(event, ui) { 
								if(doRefresh == 1){
									$refreshPage = window.location.reload(true);
								} 
						   }
				});
			}	
			
		 }

	});	
}

function popUpInfosConnexion(){
	var message = '<div title="Connecte-toi">Tu dois être connecté pour pouvoir Chronokifer ce CK !!!</div>';
	$(message).dialog({width: 360});
}

var nbCKTotal;
var pf = 0;
var page = 1;

function refreshTableau(tri, trica){
	
	//Récupérer le nombre total de CK retournés en ajoutant une action dans le controleur (faire un $.get)
	
	$.get ("index.php?module=front&cont=chronokif&action=nbcktotal", { 'tri' : tri, 'trica' : trica, 'pf' : pf },
		function(data) { 
			nbCKTotal = data;
			
			EffectuerRefreshTableau(tri, trica)
		}
	);

}

function EffectuerRefreshTableau(tri, trica){
	
	$.post (
		"index.php?module=front&cont=chronokif&action=refreshTableau&tri="+tri+"&trica="+trica+"&pf="+pf,
		function(data) { 
		
			$('#blocContenuTableauDeBord').html(data); 
			
			//Faire le code JS pour générer les options de la liste ...
			
			/*for(var i = 1; i <= Math.ceil(nbCKTotal/18); i++) {
					document.getElementById('chgpage').options[i - 1] = null;
			}*/
			
			for(var i = 1; i <= Math.ceil(nbCKTotal/18); i++) {
				
				document.getElementById('chgpage').options[i - 1] = null;
				
				if(i == page)  {
					
					document.getElementById('chgpage').options[i - 1] = new Option(i,i,true,true);
				}
				else {
					
					document.getElementById('chgpage').options[i - 1] = new Option(i,i);
				}
			}
			
			// ...et pour l'affichage des liens
			
			if(nbCKTotal > 18) {
				
				document.getElementById('horizonChoixPage').style.visibility="visible";
				
				if(document.getElementById('chgpage').options[document.getElementById('chgpage').length - 1].selected == true){
					document.getElementById('horizonPageSuivante').style.display="none";
				}
				else {
					document.getElementById('horizonPageSuivante').style.display="";
				}
				
				if(document.getElementById('chgpage').options[0].selected == true){
					document.getElementById('horizonPagePrecedente').style.display="none";
				}
				else {
					document.getElementById('horizonPagePrecedente').style.display="";
				}
			}
			else {
				document.getElementById('horizonChoixPage').style.visibility="hidden";
			}
		}
	);

}
function GetNext()
{
	pf += nbCkPage;
	page = parseInt(document.getElementById('chgpage').options[document.getElementById('chgpage').selectedIndex].value) + parseInt(1);
	var tri = document.getElementById('typoListeTabBord_1').options[document.getElementById('typoListeTabBord_1').selectedIndex].value;
	var trica = document.getElementById('typoListeTabBord_2').options[document.getElementById('typoListeTabBord_2').selectedIndex].value;
	refreshTableau(tri, trica);
}

function GetPrev()
{
	pf -= nbCkPage;
	page = parseInt(document.getElementById('chgpage').options[document.getElementById('chgpage').selectedIndex].value) - parseInt(1);
	var tri = document.getElementById('typoListeTabBord_1').options[document.getElementById('typoListeTabBord_1').selectedIndex].value;
	var trica = document.getElementById('typoListeTabBord_2').options[document.getElementById('typoListeTabBord_2').selectedIndex].value;
	refreshTableau(tri, trica);
}

function ChoixPage()
{
	page = document.getElementById('chgpage').options[document.getElementById('chgpage').selectedIndex].value;
	pf = (page - 1) * parseInt(nbCkPage);
	//(parseInt(page) - 1) * parseInt(nbCkPage);
	var tri = document.getElementById('typoListeTabBord_1').options[document.getElementById('typoListeTabBord_1').selectedIndex].value;
	var trica = document.getElementById('typoListeTabBord_2').options[document.getElementById('typoListeTabBord_2').selectedIndex].value;
	refreshTableau(tri, trica);
}

function PermuterWidget(data, idChronokif, choix, visibilite) {
	if(choix == 1){
		
		if(visibilite == 0){
			$("#bloc_chronokif_" + idChronokif + " .texteAjoutWidget").text('Retirer du widget');
		} else {
			$("#bloc_chronokif_" + idChronokif + " .texteAjoutWidget").text('Retirer de mes CKs');
		}
		
		$("#bloc_chronokif_" + idChronokif + " .texteAjoutWidget").addClass('texteSupprimeWidget');
		$("#bloc_chronokif_" + idChronokif + " .texteAjoutWidget").removeClass('texteAjoutWidget');
		
		$("#bloc_chronokif_" + idChronokif + " .cadreAjoutWidget").addClass('cadreSupprimeWidget');
		$("#bloc_chronokif_" + idChronokif + " .cadreAjoutWidget").removeClass('cadreAjoutWidget');
		
		$("#bloc_chronokif_" + idChronokif + " .ajoutWidgetLogo").addClass('supprimeWidgetLogo');
		$("#bloc_chronokif_" + idChronokif + " .ajoutWidgetLogo").removeClass('ajoutWidgetLogo');
		
		document.getElementById('link_chg_wgt_' + idChronokif).onclick = function() { ChangerWidget(idChronokif, PermuterWidget, 0, visibilite);
		 
		}
		
		
	}
	else if(choix == 0){
		
		// mis en commentaire car posé des problème lorsqu'on retirer un CK de l'horizon
		if(document.getElementById('typoListeTabBord_1'))
		{
			var tri = document.getElementById('typoListeTabBord_1').options[document.getElementById('typoListeTabBord_1').selectedIndex].value;
			var categorie = document.getElementById('typoListeTabBord_2').options[document.getElementById('typoListeTabBord_2').selectedIndex].value;
			var trica = "categorie," + categorie;
			refreshTableau(tri, trica);
			
		}	
		
		if(visibilite == 0){
			$("#bloc_chronokif_" + idChronokif + " .texteSupprimeWidget").text('Ajouter au widget');
		} else {
			$("#bloc_chronokif_" + idChronokif + " .texteSupprimeWidget").text('Ajouter à mes CKs');
		}
		
		
		$("#bloc_chronokif_" + idChronokif + " .texteSupprimeWidget").addClass('texteAjoutWidget');
		$("#bloc_chronokif_" + idChronokif + " .texteSupprimeWidget").removeClass('texteSupprimeWidget');
		
		$("#bloc_chronokif_" + idChronokif + " .cadreSupprimeWidget").addClass('cadreAjoutWidget');
		$("#bloc_chronokif_" + idChronokif + " .cadreSupprimeWidget").removeClass('cadreSupprimeWidget');
		
		$("#bloc_chronokif_" + idChronokif + " .supprimeWidgetLogo").addClass('ajoutWidgetLogo');
		$("#bloc_chronokif_" + idChronokif + " .supprimeWidgetLogo").removeClass('supprimeWidgetLogo ');
		
		document.getElementById('link_chg_wgt_' + idChronokif).onclick = function() { ChangerWidget(idChronokif, PermuterWidget, 1, visibilite);
		
		}
		
	}
	
	else{
		//alert("la modification n'a pu être effectuée!");
	}
}

function LoadCk(choix) {
	if(choix == 1) {
		refreshTableau($("#typoListeTabBord_1").val(), $("#typoListeTabBord_2").val());
		
	} 
	else {
		refreshTableau($("#typoListeTabBord_1").val(), $("#typoListeTabBord_2").val());
	}
}
	
	
function AjouterWidget(data) {
	
	if(data == 'off'){
		$('#flecheAjoutCk').hide();
	}
	else{
		//alert("la modification n'a pu être effectuée!");
	}
}

function AjoutTribuPopUp(idChronokif){
	//alert(idChronokif);
	$.post(
		"index.php?module=front&cont=tribus&action=formpopajout", { 'idc' : idChronokif },
		function(data) { 
				
			$(data).dialog({
				autoOpen : true,
				height : 'auto',
				width : 430,
				modal : false,
				resizable : false,
				zIndex : 9999
			});
			
		},
		"html"
	)
}

function AnnulerAjoutTribuPopUp() 
{ 
	$("#ajoutPopUp").dialog("close"); 
	$("#ajoutPopUp").dialog("destroy"); 
	$("#ajoutPopUp").remove();
}

function AjoutTribuPopUpSave()
{
	var idTribus = new Array();
	
	$("input[name^='tribu']:checked").each(function() {
		idTribus[idTribus.length] = $(this).val();
	});
	
	$.post(
		"index.php?module=front&cont=tribus&action=ajoutsave", { 'idc' : $("#idc").val(), 'idTribus[]' : idTribus  },
		function(data) { 
			$("#ajoutPopUp").dialog("close"); 
			$("#ajoutPopUp").dialog("destroy"); 
			$("#ajoutPopUp").remove();
			
			var message = '<div title="' + data + '">Les modifications sur le ChronoKif  <span style="color:#fea31a; font-style:italic"> ' + data + ' </span>, ont bien &eacute;t&eacute; enregistr&eacute;es pour tes tribus</div>';
			$(message).dialog({width: 430});
			
		}
	)
	
}

function SelectOutlook(){
	
	var outlook = $('#fichier').val();
	alert(outlook );
	$.post(
		"index.php?module=front&cont=tribus&action=selectoutlook", { 'outlook' : outlook },
		function(data) { 
				
			$('#tabSelectOutlook').html;
		}
	)
}

function RedirectRecherche(idSearch)
{
	var url = "comptes-a-rebours-chronokifs-";
	
	// -- Mots Cles -- //
	if(idSearch == 'motCle')
	{
		if($("#motCle").size() != 0)
		{
			motcle = $("#motCle").val();
			motcle = FormatUrlRW(motcle);
			
			url = url + motcle;
			
		}
	}
	else if(idSearch == 'zoneRecherche')
	{
		if($("#zoneRecherche").size() != 0)
		{
			motcle = $("#zoneRecherche").val();
			motcle = FormatUrlRW(motcle);

			url = url + motcle;
		}
	}

	// -- Categories -- //
	if($("#categorie").size() != 0 && $("#categorie").val() != "")
	{
		idCategorie = $("#categorie").val();
		
		libelleCategorie = FormatUrlRW($("#categorie option:selected").html());
		
		url = url +  "_dans-" + libelleCategorie + "-" + idCategorie;
	}
	
	if(url != "")
	{
		url = url + ".html";
		document.location.href = url;
	}
}

function FormatUrlRW(chaine)
{
	chaine = chaine.toLowerCase();
	
	chaine = chaine.replace(/[àâä]/gi,"a")
  chaine = chaine.replace(/[éèêë]/gi,"e")
  chaine = chaine.replace(/[îï]/gi,"i")
  chaine = chaine.replace(/[ôö]/gi,"o")
  chaine = chaine.replace(/[ùûü]/gi,"u")
	
	chaine = chaine.replace(/(&nbsp;)?(&gt;)?(&amp;)?/g, "");
	chaine = chaine.replace(/[^a-zA-Z0-9 ]/g, "");
	chaine = chaine.replace(/^\s+/g,'').replace(/\s+$/g,'');
	chaine = chaine.replace(/\s+/g,'-'); 
	
	return chaine;
}



