// JavaScript Document
	
	function filterCBclicked (el){
		var filter = el.val();
		if (el.is(":checked")){
			setCookie("cb-"+filter, "on", 1);
			$("#ul-dico li").each(function () {
				if ($(this).hasClass("li-"+filter)) {
					$(this).show();
				}
			});
		}
		else {
			setCookie("cb-"+filter, "off", 1);
			$("#ul-dico li").each(function () {
				if ($(this).hasClass("li-"+filter)) {
					$(this).hide();
				}
			});
		}
		displayResultsCount();
		scrollWordList()
	}
	
	function scrollWordList(){
		if ($(".selected-li").is(":visible")){
			$("#word-list-container").scrollTo(".selected-li");
		}
		else {
			$("#word-list-container").scrollTo(0);
		}
	}
	
	function checkCheckboxes(){
		var all=["e","v","s","d"];
		var l = all.length;
		for(var i=0 ; i<l ; i++){
			if (getCookie("cb-"+all[i]) == "off"){
				$("#filter-"+all[i]).attr("checked", false);
				$("#ul-dico li").each(function () {
					if ($(this).hasClass("li-"+all[i])) {
						$(this).hide();
					}
				});
			}
		}
	}
	
	function setCookie(c_name,value,expiredays) {
		var exdate=new Date();
		exdate.setDate(exdate.getDate()+expiredays);
		document.cookie=c_name+ "=" +escape(value)+
		((expiredays==null) ? "" : ";expires="+exdate.toUTCString()+";path=/;");
	}
		
	function getCookie(c_name) {
		if (document.cookie.length>0)
		  {
		  c_start=document.cookie.indexOf(c_name + "=");
		  if (c_start!=-1)
			{
			c_start=c_start + c_name.length+1;
			c_end=document.cookie.indexOf(";",c_start);
			if (c_end==-1) c_end=document.cookie.length;
			return unescape(document.cookie.substring(c_start,c_end));
			}
		  }
		return "";
	}
	
	function displayResultsCount(){
		var count = 0;
		count = $("#ul-dico li:visible").length;
		var res = " trouvaille";
		if (count > 1){res = " trouvailles";}
		$("#searchCount").text(count + res);
	}
	
	function filterFieldKeyUp(el){
		var filter = string_to_slug(el.val());
		if (filter.length == 1){
			$("#ul-dico li").each(function () {
				var targ = string_to_slug($(this).text());
				if (targ.charAt(0) != filter) {
					$(this).hide();
				} else {
					$(this).show();
				}
			});
		}
		else {
			$("#ul-dico li").each(function () {
				var targ = string_to_slug($(this).text());
				if (targ.search(new RegExp(filter, "i")) < 0) {
					$(this).hide();
				} else {
					$(this).show();
				}
			});
		}
		displayResultsCount();
		scrollWordList()
	}
	
	function getMessage(){
		var mess = getUrlVars();
		if (mess["m"] == "ts"){
			$("#mess-div").text("Terme enregistré !");
			$("#mess-div").addClass("ts");
			$("#mess-div").show();
		}
		else if (mess["m"] == "er"){
			$("#mess-div").text("Une erreur est survenue...");
			$("#mess-div").addClass("er");
			$("#mess-div").show();
		}
	}
	
	function getUrlVars()
	{
		var vars = [], hash;
		var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
		for(var i = 0; i < hashes.length; i++)
		{
			hash = hashes[i].split('=');
			vars.push(hash[0]);
			vars[hash[0]] = hash[1];
		}
		return vars;
	}
	
	function doVote(terme) {
		$.ajax({
			type: "GET",
			url: "func/vote.php",
			data: "t="+terme,
			success: function(txt){
				if (txt == "vote ok"){
					$("#vote-link-cdc-"+terme).text("Merci pour votre vote !");
					
					$("#vote-count-cdc-"+terme).text(parseInt($("#vote-count-cdc-"+terme).text())+1);
					$("#voteIMG").attr("src", "assets/Wikebec-Sauvegarder.png");
					$("#mess-div").text("Merci pour votre vote !");
					$("#mess-div").addClass("ts");
					$("#mess-div").show("slow");
				}
				else if (txt == "déjà voté"){
					$("#vote-link-cdc-"+terme).text("Vous avez déjà voté pour ce terme !");
					$("#voteIMG").attr("src", "assets/Wikebec-Sauvegarder.png");
					$("#mess-div").text("Vous avez déjà voté pour ce terme !");
					$("#mess-div").addClass("er");
					$("#mess-div").show("slow");
					
				}
			}
		});
	}
	
	function string_to_slug(str) {
	  str = str.replace(/^\s+|\s+$/g, ''); // trim
	  str = str.toLowerCase();
	  
	  // remove accents, swap ñ for n, etc
	  var from = "àáäâèéëêìíïîòóöôùúüûñç·/_,:;";
	  var to   = "aaaaeeeeiiiioooouuuunc------";
	  l=from.length ;
	  for (var i=0; i<l; i++) {
		str = str.replace(from[i], to[i]);
	  }
	
	  str = str.replace(/[^a-z0-9 -]/g, '') // remove invalid chars
		.replace(/\s+/g, '-') // collapse whitespace and replace by -
		.replace(/-+/g, '-'); // collapse dashes
	
	  return str;
	}
	
	function loadReponse(q){
		$( "#faq-r" ).fadeOut("fast");
		$.ajax({
			type: "POST",
			data: "q="+q,
			url: "pages/inc/fReponses.php",
			success:function(txt){ 
				if ($("#faq-r").is(":visible")){
					setTimeout( function() {showR(txt)}, 200 );
				}
				else {
					showR(txt);
				}
		   },
		   error:function (xhr, ajaxOptions, thrownError){
				$( "#faq-r" ).text("erreur");
			}
		});
	}
	function showR(txt){
		$( "#faq-r" ).html(txt);
		$( "#faq-r" ).fadeIn("fast");
	}
	
		
	function deleteEntree() {
		$( "#dialog" ).dialog({
			height: 140,
			modal: true,
			title: "Vérification en cours..."
		});
		var dataString = $("#deleteFormu").serialize();
		$.ajax({
			type: "POST",
			data: dataString,
			url: "func/deleteEntree.php",
			success:function(txt){ 
				if (txt == "erreur3"){
					setTimeout( function() {
						$( "#dialog" ).text("Veuillez choisir une raison pour retirer ce terme !");
						$( "#dialog" ).dialog( "option", "title", 'OUPS !' );
					}, 1000 );
				}
				else if (txt == "erreur4"){
					setTimeout( function() {
						$( "#dialog" ).text("Veuillez spécifier que vous êtes bien humain !");
						$( "#dialog" ).dialog( "option", "title", 'OUPS !' );
					}, 1000 );
				}
				else if (txt == "deleted"){
					setTimeout( function() {
						$( "#dialog" ).text("L'entrée a bien été effacée !");
						$( "#dialog" ).dialog( "option", "title", 'Effacée !' );
					}, 1000 );
					 setTimeout( function() { window.location = "/";  }, 2000 );
				}
		   },
		   error:function (xhr, ajaxOptions, thrownError){
				 setTimeout( function() { window.location = "/?m=er";  }, 1000 );
			}
		});
	}
