//READY = une fois la page chargÃ©e
$(document).ready(function() {
	/*menus du header*/
	/*div avec un attribut "class" qui vaut l'id du div s'ouvrant*/
	/*ce div contient un elt ayant la classe "bt_from_hover"; au survol le div s'ouvre*/
	$('.bt_from_hover').parent().hover(
		 function(){
		 	var id_div_menu = $(this).attr('class');
		 	if(id_div_menu){
		 		$('#'+id_div_menu).stop(true,true).slideDown(250);
		 	}
		 }

		,function(){
		 	var id_div_menu = $(this).attr('class');
		 	if(id_div_menu){
		 		$('#'+id_div_menu).stop(true,true).slideUp(0);
		 	}
		}
	);
	
	init_lightbox();
	init_nice_selectbox();
});
//FIN READY

/**
 * init les lightbox
 */
function init_lightbox(){
	$('.lightbox').lightBox({
		overlayBgColor: '#ccc',
		overlayOpacity: 0.6,
		imageLoading: URL_BASE+'images/lightbox/loading.gif',
		imageBtnClose: URL_BASE+'images/lightbox/close.gif',
		imageBtnPrev: URL_BASE+'images/lightbox/prev.gif',
		imageBtnNext: URL_BASE+'images/lightbox/next.gif',
		imageBlank: URL_BASE+'images/lightbox/blank.gif',
		containerResizeSpeed: 150,
		/*xtImage: 'Imagem',*/
		txtOf: '/'//Text "of" used in the image caption.
   });
   /**
	* 	overlayBgColor - Used to define the overlay background color. #000 (black) is default.
	    overlayOpacity - Used to define the overlay opacity. 0.8 is default.
	    imageLoading - The loading gif animator. images/lightbox-ico-loading.gif is default.
	    imageBtnClose - The close image button. images/lightbox-btn-close.gif is default.

	    imageBtnPrev - The previous image button. images/lightbox-btn-prev.gif is default.
	    imageBtnNext - The next image button. images/lightbox-btn-next.gif is default.
	    containerBorderSize - The padding CSS information used in the container image box. 10 is default.
	    containerResizeSpeed - The duration of resize effect in the container image box. 400 is default.
	    txtImage - Text "Image" used in the image caption.
	    txtOf - Text "of" used in the image caption.

	    imageBlank - The image blank for trick Internet Explorer into showing hover. images/lightbox-blank.gif is default.
	    keyToClose - The key to close the lightBox. Letter c (close) is default.
	    keyToPrev - The key to show the previous image. Letter p (previous) is default.
	    keyToNext - The key to show the next image. Letter n (next) is default.

    */
}

/**
 * initialise les jolis selectbox ï¿½ la place de ceux par defaut
 * 2 types : ceux qui ont comme classe element_recherche_select ou element_recherche_select_petit
 */
function init_nice_selectbox(){
	/* jolis selectbox*/
	createDropDown();
	  $(".element_recherche_select dt a").click(function(event) {
	    event.preventDefault();
	    var dropID = $(this).closest("dl").attr("id");
	    $("#" + dropID).find("ul").toggle();
	  });

	  $(document).live('click', function(e) {
	    var $clicked = $(e.target);
	    if (! $clicked.parents().hasClass("element_recherche_select")){
	    	$(".element_recherche_select dd ul").hide();
	    	return;
	    }
	    if (! $clicked.parents().hasClass("element_recherche_select_petit")){
	    	$(".element_recherche_select_petit dd ul").hide();
	    	return;
	  	}
	  	return false;
	  });


	  $(".element_recherche_select dd ul a").live('click', function() {
	    var dl = $(this).closest("dl");
	    var dropID = dl.attr("id");
	    var text = $(this).html();
	    var source = dl.prev();
	    $("#" + dropID + " dt a").html(text);
	    $("#" + dropID + " dd ul").hide();
	    source.val($(this).find("span.value").html());
	    source.trigger("change");//sinon onchange du select non appelï¿½
	    return false;
	  }); 
	  //fin crï¿½ation select

	  //idem pour les petits selectbox
	  $(".element_recherche_select_petit dt a").live('click', function(event) {
	  	
	  	/**
	  	 * le prevent : empeche le onchange du select ...
	  	 * 
	  	 * a voir pour que la mise Ã  jour du prix soit ok
	  	 */
	  	
	  	
	    event.preventDefault();
	    var dropID = $(this).closest("dl").attr("id");
	    $("#" + dropID).find("ul").toggle();
	  });

	  $(".element_recherche_select_petit dd ul a").live('click', function() {
	    var dl = $(this).closest("dl");
	    var dropID = dl.attr("id");
	    var text = $(this).html();
	    var source = dl.prev();
	    $("#" + dropID + " dt a").html(text);
	    $("#" + dropID + " dd ul").hide();
	    source.val($(this).find("span.value").html());
	    source.trigger("change");//sinon onchange du select non appelï¿½
	    return false;
	  }); //*/
}

function createDropDown() {
	var selects = $("select.element_recherche_select");//dropdown_value");
	  var idCounter = 1;
	  selects.each(function() {
	    var dropID = "dropdown_" + idCounter;
	    var source = $(this);
	    if(source.siblings('dl').size()==0){//pas ceux déjà  faits
		    var selected = source.find("option[selected]");
		    var options = $("option", source);
		    source.after('<dl id="' + dropID + '" class="element_recherche_select"></dl>');
		    $("#" + dropID).append('<dt><a href="#">' + selected.text() + '<span class="value">' + selected.val() + '</span></a></dt>');
		    $("#" + dropID).append('<dd><ul></ul></dd>');
		    options.each(function() {
		      $("#" + dropID + " dd ul").append('<li><a href="#">' + $(this).text() + '<span class="value">' + $(this).val() + '</span></a></li>');
		    });
		    $(this).hide();
	    }
		idCounter++;
	  });
	
	  //petits select
	  var selects = $("select.element_recherche_select_petit");//dropdown_value");
	  //var idCounter = 1;
	  selects.each(function() {
	    var dropID = "dropdown_" + idCounter;
	    var source = $(this);
	    if(source.siblings('dl').size()==0){//pas ceux dÃ©jÃ  faits
		    var selected = source.find("option[selected]");
		    var options = $("option", source);
		    source.after('<dl id="' + dropID + '" class="element_recherche_select_petit"></dl>');
		    $("#" + dropID).append('<dt><a href="#">' + selected.text() + '<span class="value">' + selected.val() + '</span></a></dt>');
		    $("#" + dropID).append('<dd><ul></ul></dd>');
		    options.each(function() {
		      $("#" + dropID + " dd ul").append('<li><a href="#">' + $(this).text() + '<span class="value">' + $(this).val() + '</span></a></li>');
		    });
		    $(this).hide();
	    }
		idCounter++;
	  });
}

function transparaitre(p,sens,id_div){
	var div_trans=document.getElementById("div_de_trans");
	var div_affiche=document.getElementById(id_div);
	if(sens=="moins"){
		div_trans.className="div_trans_transparence_"+p;
		if(p!=0){
			window.setTimeout("transparaitre("+(p-1)+",\"moins\",'"+id_div+"')",10);
		}else{
			div_trans.style.display="none";
		}
	}
	if(sens=="plus"){
		div_trans.className="div_trans_transparence_"+p;
		if(p!=9){
			window.setTimeout("transparaitre("+(p+1)+",\"plus\",'"+id_div+"')",10);
		}else{
			div_affiche.style.display="block";
		}
	}
}
function affiche_div_trans(id_div){
	var div_trans=document.getElementById("div_de_trans");
	var div_affiche=document.getElementById(id_div);
	if(div_trans.style.display=="none"){
		div_trans.style.display="block";
		transparaitre(3,"plus",id_div);
	}else{
		div_affiche.style.display="none";
		transparaitre(9,"moins",id_div);
	}
}
function affiche_div(id_div){
	affiche_div_trans(id_div);
}


function replaceAll(str, search, repl) {
	while (str.indexOf(search) != -1)
	str = str.replace(search, repl);
	return str;
}
