/*FORMSELECT PLUGIN
 *
 * Copyright (c) 2006/2007 Sam Collett (http://www.texotela.co.uk)
 * Licensed under the MIT License:
 * http://www.opensource.org/licenses/mit-license.php
 *
 * Version 2.2.1
 * Demo: http://www.texotela.co.uk/code/jquery/select/
 *
 * $LastChangedDate$
 * $Rev$
 *
 */
 
(function($) {
 
/**
 * Adds (single/multiple) options to a select box (or series of select boxes)
 *
 * @name     addOption
 * @author   Sam Collett (http://www.texotela.co.uk)
 * @type     jQuery
 * @example  $("#myselect").addOption("Value", "Text"); // add single value (will be selected)
 * @example  $("#myselect").addOption("Value 2", "Text 2", false); // add single value (won't be selected)
 * @example  $("#myselect").addOption({"foo":"bar","bar":"baz"}, false); // add multiple values, but don't select
 *
 */
$.fn.addOption = function()
{
	var add = function(el, v, t, sO)
	{
		var option = document.createElement("option");
		option.value = v, option.text = t;
		// get options
		var o = el.options;
		// get number of options
		var oL = o.length;
		if(!el.cache)
		{
			el.cache = {};
			// loop through existing options, adding to cache
			for(var i = 0; i < oL; i++)
			{
				el.cache[o[i].value] = i;
			}
		}
		// add to cache if it isn't already
		if(typeof el.cache[v] == "undefined") el.cache[v] = oL;
		el.options[el.cache[v]] = option;
		if(sO)
		{
			option.selected = true;
		}
	};
	
	var a = arguments;
	if(a.length == 0) return this;
	// select option when added? default is true
	var sO = true;
	// multiple items
	var m = false;
	// other variables
	var items, v, t;
	if(typeof(a[0]) == "object")
	{
		m = true;
		items = a[0];
	}
	if(a.length >= 2)
	{
		if(typeof(a[1]) == "boolean") sO = a[1];
		else if(typeof(a[2]) == "boolean") sO = a[2];
		if(!m)
		{
			v = a[0];
			t = a[1];
		}
	}
	this.each(
		function()
		{
			if(this.nodeName.toLowerCase() != "select") return;
			if(m)
			{
				for(var item in items)
				{
					add(this, item, items[item], sO);
				}
			}
			else
			{
				add(this, v, t, sO);
			}
		}
	);
	return this;
};

/**
 * Add options via ajax
 *
 * @name     ajaxAddOption
 * @author   Sam Collett (http://www.texotela.co.uk)
 * @type     jQuery
 * @param    String url      Page to get options from (must be valid JSON)
 * @param    Object params   (optional) Any parameters to send with the request
 * @param    Boolean select  (optional) Select the added options, default true
 * @param    Function fn     (optional) Call this function with the select object as param after completion
 * @param    Array args      (optional) Array with params to pass to the function afterwards
 * @example  $("#myselect").ajaxAddOption("myoptions.php");
 * @example  $("#myselect").ajaxAddOption("myoptions.php", {"code" : "007"});
 * @example  $("#myselect").ajaxAddOption("myoptions.php", {"code" : "007"}, false, sortoptions, {"dir": "desc"});
 *
 */
$.fn.ajaxAddOption = function(url, params, select, fn, args)
{
	if(typeof(url) != "string") return this;
	if(typeof(params) != "object") params = {};
	if(typeof(select) != "boolean") select = true;
	this.each(
		function()
		{
			var el = this;
			$.getJSON(url,
				params,
				function(r)
				{
					$(el).addOption(r, select);
					if(typeof fn == "function")
					{
						if(typeof args == "object")
						{
							fn.apply(el, args);
						} 
						else
						{
							fn.call(el);
						}
					}
				}
			);
		}
	);
	return this;
};

/**
 * Removes an option (by value or index) from a select box (or series of select boxes)
 *
 * @name     removeOption
 * @author   Sam Collett (http://www.texotela.co.uk)
 * @type     jQuery
 * @param    String|RegExp|Number what  Option to remove
 * @param    Boolean selectedOnly       (optional) Remove only if it has been selected (default false)   
 * @example  $("#myselect").removeOption("Value"); // remove by value
 * @example  $("#myselect").removeOption(/^val/i); // remove options with a value starting with 'val'
 * @example  $("#myselect").removeOption(/./); // remove all options
 * @example  $("#myselect").removeOption(/./, true); // remove all options that have been selected
 * @example  $("#myselect").removeOption(0); // remove by index
 *
 */
$.fn.removeOption = function()
{
	var a = arguments;
	if(a.length == 0) return this;
	var ta = typeof(a[0]);
	var v, index;
	// has to be a string or regular expression (object in IE, function in Firefox)
	if(ta == "string" || ta == "object" || ta == "function" ) v = a[0];
	else if(ta == "number") index = a[0];
	else return this;
	this.each(
		function()
		{
			if(this.nodeName.toLowerCase() != "select") return;
			// clear cache
			if(this.cache) this.cache = null;
			// does the option need to be removed?
			var remove = false;
			// get options
			var o = this.options;
			if(!!v)
			{
				// get number of options
				var oL = o.length;
				for(var i=oL-1; i>=0; i--)
				{
					if(v.constructor == RegExp)
					{
						if(o[i].value.match(v))
						{
							remove = true;
						}
					}
					else if(o[i].value == v)
					{
						remove = true;
					}
					// if the option is only to be removed if selected
					if(remove && a[1] === true) remove = o[i].selected;
					if(remove)
					{
						o[i] = null;
					}
					remove = false;
				}
			}
			else
			{
				// only remove if selected?
				if(a[1] === true)
				{
					remove = o[index].selected;
				}
				else
				{
					remove = true;
				}
				if(remove)
				{
					this.remove(index);
				}
			}
		}
	);
	return this;
};

/**
 * Sort options (ascending or descending) in a select box (or series of select boxes)
 *
 * @name     sortOptions
 * @author   Sam Collett (http://www.texotela.co.uk)
 * @type     jQuery
 * @param    Boolean ascending   (optional) Sort ascending (true/undefined), or descending (false)
 * @example  // ascending
 * $("#myselect").sortOptions(); // or $("#myselect").sortOptions(true);
 * @example  // descending
 * $("#myselect").sortOptions(false);
 *
 */
$.fn.sortOptions = function(ascending)
{
	var a = typeof(ascending) == "undefined" ? true : !!ascending;
	this.each(
		function()
		{
			if(this.nodeName.toLowerCase() != "select") return;
			// get options
			var o = this.options;
			// get number of options
			var oL = o.length;
			// create an array for sorting
			var sA = [];
			// loop through options, adding to sort array
			for(var i = 0; i<oL; i++)
			{
				sA[i] = {
					v: o[i].value,
					t: o[i].text
				}
			}
			// sort items in array
			sA.sort(
				function(o1, o2)
				{
					// option text is made lowercase for case insensitive sorting
					o1t = o1.t.toLowerCase(), o2t = o2.t.toLowerCase();
					// if options are the same, no sorting is needed
					if(o1t == o2t) return 0;
					if(a)
					{
						return o1t < o2t ? -1 : 1;
					}
					else
					{
						return o1t > o2t ? -1 : 1;
					}
				}
			);
			// change the options to match the sort array
			for(var i = 0; i<oL; i++)
			{
				o[i].text = sA[i].t;
				o[i].value = sA[i].v;
			}
		}
	);
	return this;
};
/**
 * Selects an option by value
 *
 * @name     selectOptions
 * @author   Mathias Bank (http://www.mathias-bank.de), original function
 * @author   Sam Collett (http://www.texotela.co.uk), addition of regular expression matching
 * @type     jQuery
 * @param    String|RegExp value  Which options should be selected
 * can be a string or regular expression
 * @param    Boolean clear  Clear existing selected options, default false
 * @example  $("#myselect").selectOptions("val1"); // with the value 'val1'
 * @example  $("#myselect").selectOptions(/^val/i); // with the value starting with 'val', case insensitive
 *
 */
$.fn.selectOptions = function(value, clear)
{
	var v = value;
	var vT = typeof(value);
	var c = clear || false;
	// has to be a string or regular expression (object in IE, function in Firefox)
	if(vT != "string" && vT != "function" && vT != "object") return this;
	this.each(
		function()
		{
			if(this.nodeName.toLowerCase() != "select") return this;
			// get options
			var o = this.options;
			// get number of options
			var oL = o.length;
			for(var i = 0; i<oL; i++)
			{
				if(v.constructor == RegExp)
				{
					if(o[i].value.match(v))
					{
						o[i].selected = true;
					}
					else if(c)
					{
						o[i].selected = false;
					}
				}
				else
				{
					if(o[i].value == v)
					{
						o[i].selected = true;
					}
					else if(c)
					{
						o[i].selected = false;
					}
				}
			}
		}
	);
	return this;
};

/**
 * Copy options to another select
 *
 * @name     copyOptions
 * @author   Sam Collett (http://www.texotela.co.uk)
 * @type     jQuery
 * @param    String to  Element to copy to
 * @param    String which  (optional) Specifies which options should be copied - 'all' or 'selected'. Default is 'selected'
 * @example  $("#myselect").copyOptions("#myselect2"); // copy selected options from 'myselect' to 'myselect2'
 * @example  $("#myselect").copyOptions("#myselect2","selected"); // same as above
 * @example  $("#myselect").copyOptions("#myselect2","all"); // copy all options from 'myselect' to 'myselect2'
 *
 */
$.fn.copyOptions = function(to, which)
{
	var w = which || "selected";
	if($(to).size() == 0) return this;
	this.each(
		function()
		{
			if(this.nodeName.toLowerCase() != "select") return this;
			// get options
			var o = this.options;
			// get number of options
			var oL = o.length;
			for(var i = 0; i<oL; i++)
			{
				if(w == "all" ||	(w == "selected" && o[i].selected))
				{
					$(to).addOption(o[i].value, o[i].text);
				}
			}
		}
	);
	return this;
};

/**
 * Checks if a select box has an option with the supplied value
 *
 * @name     containsOption
 * @author   Sam Collett (http://www.texotela.co.uk)
 * @type     Boolean|jQuery
 * @param    String|RegExp value  Which value to check for. Can be a string or regular expression
 * @param    Function fn          (optional) Function to apply if an option with the given value is found.
 * Use this if you don't want to break the chaining
 * @example  if($("#myselect").containsOption("val1")) alert("Has an option with the value 'val1'");
 * @example  if($("#myselect").containsOption(/^val/i)) alert("Has an option with the value starting with 'val'");
 * @example  $("#myselect").containsOption("val1", copyoption).doSomethingElseWithSelect(); // calls copyoption (user defined function) for any options found, chain is continued
 *
 */
$.fn.containsOption = function(value, fn)
{
	var found = false;
	var v = value;
	var vT = typeof(v);
	var fT = typeof(fn);
	// has to be a string or regular expression (object in IE, function in Firefox)
	if(vT != "string" && vT != "function" && vT != "object") return fT == "function" ? this: found;
	this.each(
		function()
		{
			if(this.nodeName.toLowerCase() != "select") return this;
			// option already found
			if(found && fT != "function") return false;
			// get options
			var o = this.options;
			// get number of options
			var oL = o.length;
			for(var i = 0; i<oL; i++)
			{
				if(v.constructor == RegExp)
				{
					if (o[i].value.match(v))
					{
						found = true;
						if(fT == "function") fn.call(o[i]);
					}
				}
				else
				{
					if (o[i].value == v)
					{
						found = true;
						if(fT == "function") fn.call(o[i]);
					}
				}
			}
		}
	);
	return fT == "function" ? this : found;
};



/**
 * Returns values which have been selected
 *
 * @name     selectedValues
 * @author   Sam Collett (http://www.texotela.co.uk)
 * @type     Array
 * @example  $("#myselect").selectedValues();
 *
 */
$.fn.selectedValues = function()
{
	var v = [];
	this.find("option:selected").each(
		function()
		{
			v[v.length] = this.value;
		}
	);
	return v;
};

/**
 * Returns text which have been selected
 *
 * @name     selectedText
 * @author   Sam Collett (http://www.texotela.co.uk)
 * @type     Array
 * @example  $("#myselect").selectedValues();
 *
 */
$.fn.selectedText = function()
{
	var v = [];
	this.find("option:selected").each(
		function()
		{
			v[v.length] = this.text;
		}
	);
	return v;
};


})(jQuery);

					  





		
		

/*GESTION DU MOTEUR DE RECHERCHE*/
function affiche(id) {
var d = document.getElementById(id);
	for (var i = 1; i<=10; i++) {
		if (document.getElementById('smenu'+i)) {document.getElementById('smenu'+i).style.display='none';}
	}
	if (d) {d.style.display='block';}
}
function cacher(id) {
	var d = document.getElementById(id);
	for (var i = 1; i<=10; i++) {
		if (document.getElementById('smenu'+i)) {document.getElementById('smenu'+i).style.display='none';}
	}
	if (d) {d.style.display='none';}
}

/* Courtesy of Maxime Lafontaine */
function changeTitre(idTitreMenu, texte){

            eltitre = document.getElementById(idTitreMenu);
            eltitre = eltitre.getElementsByTagName("a");
            eltitre[0].innerHTML = texte ;
            return false;

}
function changeValeurChamp(valeur, champ, form){

	$("#fff select[@name='"+champ+"'] option[@selected='selected']").removeAttr("selected");//remove any selected items
	$("#fff select[@name='"+champ+"'] option[@value="+valeur+"]").attr("selected","selected");//select 
		
            return false;
}

function changerSousMenu(form, idMenu, idTitreMenu, texte, valeur, champ){
    changeTitre(idTitreMenu, texte);
    changeValeurChamp(valeur, champ, form);
    cacher(idMenu);
    return false;
}


function changerPrix(form, idMenu, idTitreMenu, texte, valeur, champ){
    changeTitre(idTitreMenu, texte);
    changeValeurChamp(valeur, champ, form);
	changevaleur();
    cacher(idMenu);
    return false;
}



function changevaleur(){

	

	
		
				if( $("#fff select[@name='pricerange']").selectedValues() == ''){
					$("#fff input[@name='data[Produit][pdt_prixsaq-gte]'] ").attr("value", ""); 
					
					$("#fff input[@name='data[Produit][pdt_prixsaq-lte]'] ").attr("value", "");
				}
			
				if( $("#fff select[@name='pricerange']").selectedValues() == '1'){
					$("#fff input[@name='data[Produit][pdt_prixsaq-gte]'] ").attr("value", "10"); 
					
					$("#fff input[@name='data[Produit][pdt_prixsaq-lte]'] ").attr("value", "19");
				}
				
				if( $("#fff select[@name='pricerange']").selectedValues() == '2'){
					$("#fff input[@name='data[Produit][pdt_prixsaq-gte]'] ").attr("value", "20"); 
					
					$("#fff input[@name='data[Produit][pdt_prixsaq-lte]'] ").attr("value", "39");
				}
				
				if( $("#fff select[@name='pricerange']").selectedValues() == '3'){
					$("#fff input[@name='data[Produit][pdt_prixsaq-gte]'] ").attr("value", "40"); 
					$("#fff input[@name='data[Produit][pdt_prixsaq-lte]'] ").attr("value", "99");
					
				}
				
				if( $("#fff select[@name='pricerange']").selectedValues() == '4'){
					
					$("#fff input[@name='data[Produit][pdt_prixsaq-gte]'] ").attr("value", "100"); 
					$("#fff input[@name='data[Produit][pdt_prixsaq-lte]'] ").attr("value", "");
				}
			}

function reinitform(){

				if ($("#fff input[@name='langue']").attr('value')=='fr')
				{
					eltitre = $("#smenu1Titre > a");

					eltitre[0].innerHTML = "Producteurs";

					eltitre = $("#smenu2Titre > a");

					eltitre[0].innerHTML = "Pays";

					eltitre = $("#smenu3Titre > a");

					eltitre[0].innerHTML = "R&eacute;gions";
	
					eltitre = $("#smenu4Titre > a");

					eltitre[0].innerHTML = "Appellation";

					eltitre = $("#smenu5Titre > a");

					eltitre[0].innerHTML = "Type de produit";

					eltitre = $("#smenu6Titre > a");

					eltitre[0].innerHTML = "Plages de prix";

					$("#fff input[@name='data[Produit][pdt_nom-cont]']").attr("value","Nom");
					$("#fff input[@name='data[Produit][pdt_codesaq-cont]']").attr("value","Code SAQ");
				

				}

				if ($("#fff input[@name='langue']").attr('value')=='en')
				{
					eltitre = $("#smenu1Titre > a");

					eltitre[0].innerHTML = "Producers";

					eltitre = $("#smenu2Titre > a");

					eltitre[0].innerHTML = "Countries";

					eltitre = $("#smenu3Titre > a");

					eltitre[0].innerHTML = "Regions";
	
					eltitre = $("#smenu4Titre > a");

					eltitre[0].innerHTML = "Origin";

					eltitre = $("#smenu5Titre > a");

					eltitre[0].innerHTML = "Product type";

					eltitre = $("#smenu6Titre > a");

					eltitre[0].innerHTML = "Price ranges";

					$("#fff input[@name='data[Produit][pdt_nom-cont]']").attr("value","Name");
					$("#fff input[@name='data[Produit][pdt_codesaq-cont]']").attr("value","SAQ code");

				}

		
					$("#fff input[@name='data[Produit][pdt_prixsaq-gte]'] ").attr("value", ""); 
					
					$("#fff input[@name='data[Produit][pdt_prixsaq-lte]'] ").attr("value", "");

					$("#fff select[@name='data[Producteur][country_id]'] option[@selected='selected']").removeAttr("selected");//remove any selected items
					$("#fff select[@name='data[Producteur][country_id]'] option[@value='']").attr("selected","selected");//select 

					$("#fff select[@name='data[Produit][producteur_id]'] option[@selected='selected']").removeAttr("selected");//remove any selected items
					$("#fff select[@name='data[Produit][producteur_id]'] option[@value='']").attr("selected","selected");//select 

					$("#fff select[@name='data[Producteur][region_id]'] option[@selected='selected']").removeAttr("selected");//remove any selected items
					$("#fff select[@name='data[Producteur][region_id]'] option[@value='']").attr("selected","selected");//select 


					$("#fff select[@name='data[Produit][app_origi_id]'] option[@selected='selected']").removeAttr("selected");//remove any selected items
					$("#fff select[@name='data[Produit][app_origi_id]'] option[@value='']").attr("selected","selected");//select 

					$("#fff select[@name='data[Produit][couleur_id]'] option[@selected='selected']").removeAttr("selected");//remove any selected items
					$("#fff select[@name='data[Produit][couleur_id]'] option[@value='']").attr("selected","selected");//select 


			}



$(document).ready(function() {
						   
	//preremplir le formulaire
		if($("#sel_prod").selectedValues() != ''){


			eltitre = $("#smenu1Titre > a");

			eltitre[0].innerHTML = $("#fff select[@name='data[Produit][producteur_id]']").selectedText();

		}

		if($("#sel_country").selectedValues() != ''){


			eltitre = $("#smenu2Titre > a");

			eltitre[0].innerHTML = $("#fff select[@name='data[Producteur][country_id]']").selectedText();

		}

		if($("#sel_region").selectedValues() != ''){


			eltitre = $("#smenu3Titre > a");

			eltitre[0].innerHTML = $("#fff select[@name='data[Producteur][region_id]']").selectedText();

		}


		if($("#sel_appel").selectedValues() != ''){


			eltitre = $("#smenu4Titre > a");

			eltitre[0].innerHTML = $("#fff select[@name='data[Produit][app_origi_id]']").selectedText();;

		}

		if($("#sel_couleur").selectedValues() != ''){


			eltitre = $("#smenu5Titre > a");

			eltitre[0].innerHTML = $("#fff select[@name='data[Produit][couleur_id]']").selectedText();;

		}
		
		//recherche sur le nom  le code SAQ et le prix
		var stringurl=location.pathname; 
		var getit= new Array();
		getit=stringurl.split("/");

		for(i=1;i<getit.length-1;i++){
			if(getit[i].indexOf('PdtNom:cont-')==0){			
				var reqnom = getit[i].substr(12,30);
				$("#fff input[@name='data[Produit][pdt_nom-cont]']").attr("value",reqnom);
				}
			if(getit[i].indexOf('PdtCodesaq:cont-')==0){
				var reqsaq = getit[i].substr(16,30);
				$("#fff input[@name='data[Produit][pdt_codesaq-cont]']").attr("value",reqsaq);
				}

			if(getit[i].indexOf('PdtPrixsaq:gte-')==0){
				var reqsaq = getit[i].substr(15,30);
				//alert(reqsaq);
				if (reqsaq=="10")
				{
					return changerPrix('document.fff', 'smenu6', 'smenu6Titre','10 - 19','1', 'pricerange');

				}
				if (reqsaq=="20")
				{
					return changerPrix('document.fff', 'smenu6', 'smenu6Titre','20 - 39','2', 'pricerange');

				}
				if (reqsaq=="40")
				{
					return changerPrix('document.fff', 'smenu6', 'smenu6Titre','40 - 99','3', 'pricerange');

				}
				if (reqsaq=="100")
				{
					return changerPrix('document.fff', 'smenu6', 'smenu6Titre','100++','4', 'pricerange');

				}

		
				
				}
		
		}

		
		


});



//Navigation revue de presse bas de page
$(document).ready(function() {

  $('table.paginated').each(function() {

    var currentPage = 0;

    var numPerPage = 6;



    var $table = $(this);

  

    var repaginate = function() {
		  $table.find('tbody tr').show();
	    $table.find('tbody tr:lt('+(numPerPage*currentPage)+')').hide();
		$table.find('tbody tr:gt('+((currentPage + 1) * numPerPage - 1)+')').hide();

     };

	 var numRows = $table.find('tbody tr').length;

    var numPages = Math.ceil(numRows / numPerPage);

 

    var $pager = $('<div class="pager"></div>');

   for (var page = 0; page < numPages; page++) {

      $('<span class="page-number">' + (page + 1) + '</span>')

         .bind('click', {'newPage': page}, function(event) {

    currentPage = event.data['newPage'];

    repaginate();
	$(this).addClass('triactif').siblings().removeClass('triactif');

  })

        .appendTo($pager).addClass('clickable');

    }

	$pager.find('span.page-number:first').addClass('triactif');

    $pager.insertAfter($table);

    repaginate();

  });

});

//-->
