var ltie7 = ($.browser.msie && $.browser.version < 7);
var ltie8 = ($.browser.msie && $.browser.version < 8);
var ltie9 = ($.browser.msie && $.browser.version < 9);
// Menu //

$(function(){

	menu.init();
	menu2.init();
	gamme.init();
	newsletter.init();
	recherche.init();
	login.init();
	zoom.init();
	questioReponse.init();
	popupcommande.init();
	checkQte.init();
	same_adr.init();
	

	// Fix png
	if (ltie7) {
		$.ifixpng('_media/img/pixel.gif');
		//Etre plus subtil sur ce site, il n'y en a pas beaucoup de vraiment transparents $('img[src$=.png], input[type=image][src$=.png]').ifixpng();
		$('.site_info img[src$=.png]').ifixpng();
		$('li.rubrique:first-child').css({background:'none'});
		//$('.rubrique:first-child .rubriqueLink').css({background: 'none'});
		$('input[type="radio"], input[type="checkbox"], input[type="file"], input[type="image"]').css({border: 'none'});
		$('.site_info:first-child').css({paddingLeft: 0});
		$('#footer li:first-child a').css({background:'none'});
	}
	
	
	$('.sous_sous_item').hover(function(){
			$(this).parents('.sous_rubrique').find('.sous_rubriqueLink').parent('li').addClass('actived');
		},
		function(){
			$(this).parents('.sous_rubrique').find('.sous_rubriqueLink').parent('li').removeClass('actived');
		});
});

/***  same adress Start***/
var same_adr = function(){
	function _init(){
		$("input[name=same_adress]").click(function(){
			
			if ($(this).val() == "oui")
			{
				if ($("input[name=cli_civilite]:checked").val()=='Mlle')
					$("#liv_civilite").attr("checked","checked");
				if ($("input[name=cli_civilite]:checked").val()=='Mme')
					$("#liv_civilite1").attr("checked","checked");
				if ($("input[name=cli_civilite]:checked").val()=='Mr')
					$("#liv_civilite2").attr("checked","checked");
				$("#adr").val("yes");
				$("#liv_nom").val($("#cli_nom").val());
				$("#liv_prenom").val($("#cli_prenom").val());
				$("#liv_mail").val($("#cli_mail").val());
				$("#liv_societe").val($("#cli_societe").val());
				$("#liv_num_appart").val($("#cli_num_appart").val());
				$("#liv_batiment").val($("#cli_batiment").val());
				$("#liv_residence").val($("#cli_residence").val());
				$("#liv_rue").val($("#cli_rue").val());
				$("#liv_ville").val($("#cli_ville").val());
				$("#liv_codepostal").val($("#cli_codepostal").val());
				$("#liv_telephoneF").val($("#cli_telephoneF").val());
				$("#liv_telephoneM").val($("#cli_telephoneM").val());				
			}			
		});
	}
	return {init:_init}
}();
/***same adress End***/

/****Search ****/
	$('.recherche_mot').click(function(){
		$("#mots").submit();
	});

/** End Search **/

var menu = function(){
	var Items, Links;
	var ItemVisibleClass = 'on ssMenuVisible';
	function _init(){
		Items = $('#menuListe li.menuItem');
		Links = $('a',Items);
		Items.hover(function(){
				show($(this))
		}
		,function(){
				hide($(this))
		});
		Links.focus(function(){show($(this))}).blur(function(){hide($(this))});
	}

	function getItem(elmt) {
		if(!elmt.is('.menuItem')) return elmt.parents('.menuItem');
		else return elmt;
	}

	function show(elmt){
		elmt = getItem(elmt);
		Items.removeClass(ItemVisibleClass);
		elmt.addClass(ItemVisibleClass);
	}

	function hide(elmt){
		elmt = getItem(elmt);
		elmt.removeClass(ItemVisibleClass);
		$('#menuListe').find('li.actif').addClass('on');
	}

	return {init:_init}
}();



/**
	function d'ajout des favoris
**/
function favoris(url, title)
{
	if ( navigator.appName != 'Microsoft Internet Explorer' ) {
		window.sidebar.addPanel(title , url, '');
	} else {
		window.external.AddFavorite(url, title);
	}
}

/** function pour la quantite des articles produits **/
var checkQte = function(){
	function _init(){
		$("#nbr_produit").text($("#valueQte").val());
		$("#nbr_produit").css("color","#000");
	}
	return {init:_init}
}();
/**
	Fonction pour les questions reponses
**/
var questioReponse = function() {

	function _init()
	{
		var qr = document.getElementById('questionsreponses');
		if (qr) {
			var liens = qr.getElementsByTagName('A');
			for (var i=0; i<liens.length; i++) {
				document.getElementById(('cible'+liens[i].id)).style.display = 'none';
				liens[i].onclick = function() {document.getElementById(('cible'+this.id)).style.display == 'none' ? document.getElementById(('cible'+this.id)).style.display = 'block' : document.getElementById(('cible'+this.id)).style.display = 'none'; return false;};
			}
		}
	}
	return {init:_init}
}();

/** End question reponse **/

/**********************************************************/
/* Gestion du div pour l'ajout de produits dans le panier */
/**********************************************************/

var popupcommande = function() {
	function _init()
	{
		var form_fermer_popup = document.getElementById('form_fermer_popup');
		if (form_fermer_popup)
			form_fermer_popup.onsubmit = function() { if (document.getElementById('popup')) {document.getElementById('popup').style.display = "none"}; return false;}
		if (document.getElementById('dispo'))
			document.getElementById('dispo').onclick = function() { if (document.getElementById('popup')) {document.getElementById('popup').style.display = "block"}; return true; };
	}
	return {init:_init}
}();

function showPopup(ID) {
	if (document.getElementById(ID)) {
		document.getElementById(ID).style.display = "block";
		document.getElementById(ID).style.background= "#ffffff";
		document.getElementById(ID).style.border = "1px solid #000";
		document.getElementById(ID).style.left = "50%";
		document.getElementById(ID).style.marginLeft = "-20em";
		document.getElementById(ID).style.marginTop = "-5.5em";
		document.getElementById(ID).style.paddingBottom = "1em";
		document.getElementById(ID).style.position = "absolute";
		document.getElementById(ID).style.textAlign = "center";
		document.getElementById(ID).style.top = "50%";
		document.getElementById(ID).style.width = "40em";
		document.getElementById(ID).style.zIndex = "10";
	}

	if (document.getElementsByTagName) {
		var arrayForm = document.getElementsByTagName('form');
		if (arrayForm.length) {
			for (cnt = 0; cnt < arrayForm.length; cnt++) {arrayForm[cnt].style.margin = "1em";}
		}
		var arrayA = document.getElementsByTagName('a', 'dispo');
		if (arrayA.length) {

			for (cnt = 0; cnt < arrayA.length; cnt++) {arrayA[cnt].style.cursor = "pointer";}
		}
	}
	if (document.getElementsByClassName) {
		var arrayButton = getElementsByClassName('button', '*', 'popup');
		if (arrayButton.length) {
			for (cnt = 0; cnt < arrayButton.length; cnt++) {
				arrayButton[cnt].style.float = "left";
				arrayButton[cnt].style.margin = "10px 15px";
			}
		}
		var arrayInput = getElementsByClassName('input_email', '*', 'popup');
		if (arrayInput.length) {
			for (cnt = 0; cnt < arrayInput.length; cnt++) {
				arrayInput[cnt].style.border = "1px solid #666";
				arrayInput[cnt].style.height = "15px";
				arrayInput[cnt].style.marginBottom = "1em";
			}
		}
	}
}

function hidePopup(ID) {
	if (document.getElementById(ID)) {
		document.getElementById(ID).style.display = "none";
	}
	return false;
}


/*** End gestion Pop up***/


var menu2 = function(){
	var Items, Links;
	var ItemVisibleClass = 'on ssMenuVisible';
	function _init(){
		Items = $('.rubrique, .rubrique li, .menuGuideItem, .menuGuideItem li');
		Links = $('a',Items);

		Items.hover(function(){show($(this))},function(){hide($(this))});
		Links.focus(function(){show($(this))}).blur(function(){hide($(this))});

	}

	function getItem(elmt) {
		if (!elmt.is('li')) return elmt.parents('li');
		return elmt;
	}

	function show(elmt){
		elmt = getItem(elmt);
		elmt.siblings('li').removeClass(ItemVisibleClass).end().find('li').removeClass(ItemVisibleClass);
		elmt.addClass(ItemVisibleClass);
	}

	function hide(elmt){
		elmt = getItem(elmt);
		elmt.removeClass(ItemVisibleClass);
		$('#menuListe').find('li.actif').addClass('on');
	}

	return {init:_init}
}();

var gamme = function(){
	function _init(){
		$('.produit h2').adjustSize({periode: 5});
		$('.produitPresentation').adjustSize({periode: 5});
		$('.produitImage').adjustSize({periode: 5});
		$('.produitDetails').adjustSize({periode: 5});
	}
	return {init:_init}
}();

var newsletter = function(){
	var input, label
	function _init(){
		input = $("#header_nwsltr_email");
		label = $("#header_newsletter label");

		insertLabelIntoInput(label, input);
	}
	return {init:_init}
}();

var recherche = function(){
	var input, label
	function _init(){
		input = $("#menuGuide_rech_input");
		label = $("#menuGuide_recherche label");

		insertLabelIntoInput(label, input);
	}
	return {init:_init}
}();

var login = function(){
	var inputEmail, labelEmail, inputPassword, labelPassword;
	function _init(){
		inputEmail = $("#header_login_email");
		labelEmail = $("#header_login_email").siblings('label');
		insertLabelIntoInput(labelEmail, inputEmail);

		inputPassword = $("#header_login_password");
		labelPassword = $("#header_login_password").siblings('label');
		insertLabelIntoInput(labelPassword, inputPassword);
	}
	return {init:_init}
}();

var zoom = function(){
	var zoom;
	function _init(){
		zoom = $('.zoom');
		zoom.click(function(){
			var e = $(this);
			if (!e.is('a')) e = e.find('a:first');
			if (!e.is('a')) return;
			if(window.open("zoom_popup.php?path=" + e.attr('href'), "ZOOM", "directories=no,location=no,menubar=no,resizable=no,scrollbars=no,status=no,toolbar=no,width=0,height=0")) return false;
		})
	}
	return {init:_init}
}();

$.fn.adjustSize=function(options) {
	var settings = {
		periode: 2,
		mode: 'height',
		minHeight: 0,
		minWidth: 0
	};
	var ltie7 = $.browser.msie && /MSIE\s(5\.5|6\.)/.test(navigator.userAgent);
	if (options) {
		jQuery.extend(settings, options);
	}
	var totalSize = $(this).size();
	var nbPeriodes = Math.ceil(totalSize/settings.periode);
	var index = 0;
	if (settings.mode == 'height' || settings.mode == 'both') {
		var maxTotalHeightArray = new Array();
		$(this).each(function(){
			var blockId = Math.floor(index/settings.periode);
			if (!maxTotalHeightArray[blockId]) maxTotalHeightArray[blockId]=0;
			var blockTotalHeight = $(this).height();
			if (blockTotalHeight > maxTotalHeightArray[blockId]) {
				if (settings.minHeight && settings.minHeight > blockTotalHeight) blockTotalHeight = settings.minHeight;
				maxTotalHeightArray[blockId] = blockTotalHeight;
			}
			index++;
		});
		var index = 0;
		$(this).each(function(){
			var blockId = Math.floor(index/settings.periode);
			var blockComputedHeight = maxTotalHeightArray[blockId]+1;
			if (ltie7) $(this).height(blockComputedHeight+'px');
			else $(this).css('minHeight',blockComputedHeight+'px');
			index++;
		});
	}
	if (settings.mode == 'width' || settings.mode == 'both') {
		var maxTotalWidthArray = new Array();
		$(this).each(function(){
			var blockId = Math.floor(index/settings.periode);
			if (!maxTotalWidthArray[blockId]) maxTotalWidthArray[blockId]=0;
			var blockTotalWidth = $(this).width() ? $(this).width() : $(this).attr('offsetWidth') ;
			if (blockTotalWidth > maxTotalWidthArray[blockId]) {
				if (settings.minWidth && settings.minWidth > blockTotalHeight) blockTotalWidth = settings.minWidth;
				maxTotalWidthArray[blockId] = blockTotalWidth;
			}
			index++;
		});
		var index = 0;
		$(this).each(function(){
			var blockId = Math.floor(index/settings.periode);
			var blockComputedWidth = maxTotalWidthArray[blockId];
			if (ltie7) $(this).width(blockComputedWidth+'px');
			else $(this).css('minWidth',blockComputedWidth+'px');
			index++;
		});
	}

	return this;
};

function insertLabelIntoInput (label,input) {
	if (!input.val() && label.text()) {
		setDefaultValue(label,input);
		input.blur(function(){
			if (input.val()=='') setDefaultValue(label,input);
		});
	}
	function setDefaultValue(label, input){input.val(label.text()).addClass('defaultValue' ).one('click', function(){$(this).val('').removeClass('defaultValue');})}
}