jQuery(
	function() {
		
		//MINI LOGIN / MINI COMPTE CLIENT
		jQuery('#encart-espace-client').hover(function(){
			jQuery('#link-me-connecter').attr('class', 'actif');
			jQuery('#rollover-no-loggue').show();
			jQuery('#rollover-loggue').show();			
		}, function(){
			jQuery('#link-me-connecter').attr('class', '');
			jQuery('#rollover-no-loggue').hide();
			jQuery('#rollover-loggue').hide();
		});
		
		jQuery('#form-login input').click( function() {
			if (jQuery(this).val() == 'Adresse e-mail' || jQuery(this).val() == 'Mot de passe') {					
				jQuery(this).val('');
			} 
		});		
		
		//PAGE DE LOGIN
		jQuery('#input-email-create').click(function() {
			jQuery(this).attr('value', '');
		});
		
		//COMPTE CLIENT: MODIFICATIONS
		var select = jQuery('#child_number');
		showChildDateList(select.attr('value'));

		select.change(function() {
			showChildDateList(jQuery(this).attr('value'));
		});
		
		function showChildDateList(nbr) {
			jQuery('#child_date > li').hide();
			if(nbr > 0) {
				for(i=1; i<=10; i++){
					if(i<=nbr) {
						jQuery('#child_date_' + i).show();
					}
					else {
						jQuery('#child_date_'+ i +' > select').each(function(){jQuery(this).attr('value', null);});
						jQuery('#child_date_'+ i +' > select').attr('value', '0');
					}
				}
			}			
		}
		
		//COMPTE CLIENT: COMMANDES
		hideOrderLine();
		jQuery('#view-all').append(jQuery('<a href="#" id="showLineButton">Voir toutes mes commandes</a>').click(function(){
			jQuery('.order-line').show();
			jQuery(this).hide();
			jQuery('#hideLineButton').show();
		}));
		jQuery('#view-all').append(jQuery('<a href="#" id="hideLineButton">Voir mes dernières commandes</a>').hide().click(function(){
			hideOrderLine()
			jQuery(this).hide();
			jQuery('#showLineButton').show();
		}));

		function hideOrderLine() {
			var count_line = 0;
			jQuery('.order-line').hide().each(function(){
				if(count_line < 2) {
					jQuery(this).show();
					count_line++;
				}
			});			
		}
		//jQuery('.order-line')
		
		
		
		//MINI PANIER HEADER
		/*jQuery('.panier-plein').hover(function(){
			jQuery('#rollover-panier').show();
			jQuery(this).addClass("over-mini-cart");
		}, function(){
			jQuery('#rollover-panier').hide();
			jQuery(this).removeClass("over-mini-cart");
		});
		
		jQuery("#mini-cart-link").mouseover(function()
        {
            jQuery("#rollover-panier").toggle();
        }
        ).mouseout(function()
        {
            jQuery("#rollover-panier").toggle();
        });
        jQuery("#rollover-panier").mouseover(function()
        {
            jQuery(this).toggle();
        }
        ).mouseout(function()
        {
            jQuery(this).toggle();
        });*/
		
		jQuery('#voir-articles').click(function () {
			jQuery('#products-panier-cache').slideDown();
		});
	
		// RECHERCHE MAGASIN
		jQuery('#submitButtonId').hide();
		jQuery('#shopId').change(function() {
			jQuery('#formSelectCity').submit();
		});
		
		// masque l'ensemble des infos magasins sauf curShop
		// curShop definie dans default/template/shops/list.phtml
		jQuery('.shopDetail').each(function() {
			if (this.id != 'shopDetailId' + curShop) {
				jQuery(this).hide();
				jQuery('#' + this.id.replace('shopDetail', 'shopNoDetail')).show();
			}
		});

		// affiche le detail d'un magasin et masque les autres
		jQuery('.savoir-plus').click(function() {
			var id	= jQuery(this).parent('div').attr('id').replace('shopNoDetailId', '');
			jQuery(this).parent('div').hide();
			jQuery('.shopDetail').hide().each(function() {
				if ('shopDetailId' + id == jQuery(this).attr('id')) {
					jQuery(this).show();
				} else {
					jQuery(this).hide();
					jQuery('#shopNoDetailId' + this.id.replace('shopDetailId', '')).show();
				}
			});
		});
		
		// permet d'enregistrer un magasin en favori
		jQuery('.submitButtonFavorites').hide();
		jQuery('.inputFavorites').each(function() {
			jQuery(this).replaceWith('<input type="checkbox" name="' + jQuery(this).attr('name') + '" class="' + jQuery(this).attr('class') + '" value="' + jQuery(this).attr('value') + '" />');
		});
		jQuery('.inputFavorites').click(function() {
			if (jQuery(this).attr('checked') == true) {
				jQuery(this).parent('p').parent('form').submit();
			}
		});

		//checkout/process/register
		jQuery('#add-adresse-livraison').click(function () {
			jQuery('#adresse-livraison').slideDown();
		});
		
		//CHECKOUT
		jQuery('.add_qty_cart').click(function() {
			qty = parseInt(jQuery("#" + jQuery(this).parents('td').attr("id") + " input").val());
			jQuery("#" + jQuery(this).parents('td').attr("id") + " input").val(qty+1);
			jQuery(".form_cart").submit();
		});
		
		jQuery('.sup_qty_cart').click(function() {
			qty = parseInt(jQuery("#" + jQuery(this).parents('td').attr("id") + " input").val());
			if (qty > 1)
			{
				jQuery("#" + jQuery(this).parents('td').attr("id") + " input").val(qty-1);
				jQuery(".form_cart").submit();
			}
		});
		
		jQuery('#estimate_method').change(function () {
			jQuery('#co-shipping-method-form').submit();
		});
		
		// MENU
		
		// Le hover posant des problèmes sur les éléments option des select sous IE 7,
		// on redéfinit le hover du megamenu en javascript
		// Variables globales gérant les différents hover sur le megamenu (titre, onglet, select)
		clicked=hover1=hover2=hover3=false;
		bgMenuImage='';		var hoverTimer=null;		
		
		// Survol d'un titre du menu
		jQuery('.level0').hover(function(){
			hover1=true;						if (hoverTimer) {								clearTimeout(hoverTimer);								hoverTimer=null;							}
			if (bgMenuImage=='')
				bgMenuImage=jQuery(this).css('background-image');
			if (!hover2 && !hover3){								elt=jQuery(this);
				elt.css(
					{
						'background-color':elt.find('.wrapper-smenu').css('color'),
						'background-image':'none'
					}
				);				hoverTimer=setTimeout(function(){					elt.find('.level1').css('display','block');										},300);			
			}
		},function(){
			hover1=false;						if (hoverTimer) {								clearTimeout(hoverTimer);								hoverTimer=null;							}
			if (!hover2 && !hover3){
				jQuery(this).css(
					{
						'background-color':'transparent',
						'background-image':bgMenuImage
					}
				);
				jQuery(this).find('.level1').css('display','none');
			}
		});
		
		// Survol d'un onglet du menu
		jQuery('.level1').hover(function(){
			hover2=true;
			var displayMode=jQuery(this).css('diplay');
			if (!hover1 && !hover3 && typeof(displayMode)!=undefined && displayMode!='' && displayMode!='none'){
				jQuery(this).parent('.level0').css(
					{
						'background-color':jQuery(this).find('.wrapper-smenu').css('color'),
						'background-image':'none'
					}
				);
				jQuery(this).css('display','block');
			}
		},function(){
			hover2=false;
			var displayMode=jQuery(this).css('diplay');
			if (!hover1 && !hover3 && typeof(displayMode)!=undefined && displayMode!='' && displayMode!='none'){
				jQuery(this).css(
					{
						'background-color':'transparent',
						'background-image':bgMenuImage
					}
				);
				jQuery(this).css('display','none');
			}
		});

		// Clic sur un select du megamenu
		jQuery('.level0 select').click(function() {
			clicked = clicked ? false : true;
			if (clicked)
				hover3=true;
		}).blur(function(){
			hover3=false;
		}).change(function() {
			setTimeout(function(){
				hover3=false;
			},100);
		});

		jQuery('#menu_cartridge_brand').change(function() {
			hover3=false;
			jQuery('#menu_cartridge_model').get(0).length=1;
			if (jQuery(this).val() != '0') {
				var models	= eval('('+modelOptions+')');
				var options=models[jQuery(this).val()];
				var optionsHTML='';
				jQuery.each(options, function(i, o) {
					var label=o.label;
					var value=o.value;
					optionsHTML+='<option value="'+value+'">'+label+'</option>';
				});
				if (optionsHTML=='')
					jQuery('#menu_cartridge_model').attr('disabled','disabled');
				else
					jQuery('#menu_cartridge_model').removeAttr('disabled');
				jQuery('#menu_cartridge_model').html('<option value="">Sélectionnez votre modèle</option>'+optionsHTML);
			} else {
				jQuery('#menu_cartridge_model').attr('disabled','disabled');
			}
		});
		
		// Mise en couleur du titre de menu (permet de corriger le "color:inherit" non supporté par ie7)
		var menuColor;		
		jQuery('.wrapper-smenu').each(function(){
			if (typeof(jQuery(this).css("background"))!=undefined){
				menuColor=jQuery(this).css("background");
				jQuery(this).find('.titre-retrouvez a,.rubrique a,.decouvrez-gamme a').css('color',menuColor);
			}
		});
		
		
		// AJOUT PANIER AJAX
		jQuery("#formsubmit").click(function(){
			jQuery.ajax({  
				type: "POST",  
				url: jQuery('#addtocart').attr('action'),  
				data: "product="+jQuery('#id-product-0').attr('value')+"&qty="+jQuery('#qty-0').attr('value'), 
				async: false, 
				success: function() { 
					jQuery("#encart-panier").load("/checkout/cart/minicart #content-panier"); 
					jQuery("#popup-confirm-panier").css('display', 'block');
				//display message back to user here  
				}  
			});  
			return false;
		});
		
		// AJOUT PANIER AJAX - UPSELL
		jQuery(".upsell-formsubmit").click(function(){
			_id = jQuery(this).parent().parent().find('.product_id').attr('value');
			jQuery.ajax({  
				type: "POST",  
				url: jQuery('#addtocart-'+_id).attr('action'),  
				data: "product="+_id+"&qty=1", 
				async: false, 
				success: function() { 
					jQuery("#encart-panier").load("/checkout/cart/minicart #content-panier"); 
					jQuery("#popup-confirm-panier").css('display', 'block');
				}  
			});  
			return false;
		});
		
		// AJOUT PANIER AJAX - GROUPED
		jQuery(".grouped-formsubmit").click(function(){
			_id = jQuery(this).parent().parent().find('.product_id').attr('value');
			jQuery.ajax({  
				type: "POST",  
				url: jQuery('#addtocart-'+_id).attr('action'),  
				data: "product="+_id+"&qty=1", 
				async: false, 
				success: function() { 
					jQuery("#encart-panier").load("/checkout/cart/minicart #content-panier"); 
					jQuery("#popup-confirm-panier").css('display', 'block');
				}  
			});  
			return false;
		});
		
		
		// FERMER POPUP AJOUT PANIER CONFIRME
		jQuery("#popup-ajout-panier-close").click(function(){
			jQuery("#popup-confirm-panier").css('display','none');
		});
		
		//jQuery('#popup-ajout-panier-continuer-achat').click(function() {
		//	parent.top.$('#fancy_close').trigger('click');
		//});
		//jQuery()
		//jQuery("#popup-ajout-panier-continuer-achat").click(function(){
			//parent.jQuery.fn.fancybox.close();
			//jQuery.fn.fancybox.close();
			//jQuery("#fancy_overlay, #fancy_close, #popup-ajout-panier-continuer-achat").bind("click", ); 
			//alert('coucou');
			//jQuery(this).fancybox.close();
			//jQuery("#fancy_close").css('display','none');
		//});
		
		// COMMERCIAL FLYER
		
			jQuery('.clickable-mea').click(function() {
				jQuery('.clickable-mea').removeClass('actif');
				jQuery('.switchable-mea').removeClass('actif');
				jQuery('.displayable-mea').css('display', 'none');
				jQuery(this).addClass('actif');
				jQuery('#visu-encart-'+jQuery(this).attr('id').replace("link-visu-encart-","")).css('display', 'block');
			});
			
			try {
                jQuery('.onglets-mea').children(":first-child").children('a').addClass('actif');
                jQuery("#visu-encart-"+jQuery('.onglets-mea').children(":first-child").children('a').attr('id').replace("onglet-","")).css('display','block');
            } catch (e) {}
		
		// PRODUCT PAGE
		jQuery(".clickable").click(function() {
    		jQuery('.switchable').css('display', 'none');
    		jQuery('.clickable').removeClass('actif');
    		jQuery(this).addClass('actif');
    		jQuery("#"+jQuery(this).attr("id").replace("onglet-","")).css('display','block');
    		jQuery("#bottom-"+jQuery(this).attr("id").replace("onglet-","")).css('display','block');
    		
		});
		jQuery(".infotab").click(function() {
			jQuery('.tab').css('display', 'none');
			jQuery("#"+jQuery(this).attr('id').replace('li-','')).css('display', 'block');
			jQuery('.infotab').removeClass('actif');
			jQuery(this).addClass('actif');
		});
		
		// si les cross existent, on les initialise
		try {
			jQuery('.onglets').children(":first-child").addClass('first'); 
			jQuery('.onglets').children(":first-child").children('a').addClass('actif');
			jQuery("#"+jQuery('.onglets').children(":first-child").children('a').attr('id').replace("onglet-","")).css('display','block');
			jQuery("#bottom-"+jQuery('.onglets').children(":first-child").children('a').attr('id').replace("onglet-","")).css('display','block');
		}
		catch (e) {}
		try {
			jQuery('#onglets-fiche-produit').children(":first-child").children('a').addClass('actif'); 
			jQuery('#'+jQuery('#onglets-fiche-produit').children(":first-child").children('a').attr('id').replace('li-','')).css('display','block') ;    
		}
		catch (e) {}
		
		// PRODUCT LIST
		// COMPARATEUR
		jQuery("a.comp1").fancybox({
			'frameWidth' : (screen.width < 1100 ? 750 : 1100),
			'frameHeight' : (screen.height < 520 ? 350 : 520),
			'zoomSpeedIn': 400, 
			'zoomSpeedOut': 400, 
			'overlayShow': true,
			'hideOnContentClick' : false
		});
		
		jQuery("a.comp2").fancybox({
			'frameWidth' : 250,
			'frameHeight' : 100,
			'zoomSpeedIn': 400, 
			'zoomSpeedOut': 400, 
			'overlayShow': true,
			'hideOnContentClick' : false
		}); 
		jQuery("a.iframe").fancybox();	
		
		// BOUTON QTE +
		jQuery('.increase').click(function() {
			if (jQuery('#qty-'+jQuery(this).attr('id').replace("inc-","")).val()) {
				if (isNaN(jQuery('#qty-'+jQuery(this).attr('id').replace("inc-","")).val()) || jQuery('#qty-'+jQuery(this).attr('id').replace("inc-","")).val() < 1) {
					jQuery('#qty-'+jQuery(this).attr('id').replace("inc-","")).val(1);
				} else {
					jQuery('#qty-'+jQuery(this).attr('id').replace("inc-","")).val(parseInt(jQuery('#qty-'+jQuery(this).attr('id').replace("inc-","")).val())+1);
				}		
			} else {
				jQuery('#qty-'+jQuery(this).attr('id').replace("inc-","")).val(1);
			}
			try {
				jQuery("#addajax-"+jQuery(this).attr('id').replace("inc-","")).attr('href',"/checkout/cart/addajax/product/"+jQuery('#id-product-'+jQuery(this).attr('id').replace("inc-","")).attr('value')+"/univers/"+jQuery('#univers-'+jQuery(this).attr('id').replace("inc-","")).attr('value')+"/qty/"+jQuery('#qty-'+jQuery(this).attr('id').replace("inc-","")).attr('value'));
			} catch(e) {
			}
		});
		// BOUTON QTE -
		jQuery('.decrease').click(function() {
			if (jQuery('#qty-'+jQuery(this).attr('id').replace("dec-","")).val()) {
				if (isNaN(jQuery('#qty-'+jQuery(this).attr('id').replace("dec-","")).val()) || jQuery('#qty-'+jQuery(this).attr('id').replace("dec-","")).val() <= 1) {
					jQuery('#qty-'+jQuery(this).attr('id').replace("dec-","")).val(1);
				} else {
					jQuery('#qty-'+jQuery(this).attr('id').replace("dec-","")).val(parseInt(jQuery('#qty-'+jQuery(this).attr('id').replace("dec-","")).val())-1);
				}		
			} else {
				jQuery('#qty-'+jQuery(this).attr('id').replace("dec-","")).val(1);
			}
			try {
				jQuery("#addajax-"+jQuery(this).attr('id').replace("dec-","")).attr('href',"/checkout/cart/addajax/product/"+jQuery('#id-product-'+jQuery(this).attr('id').replace("dec-","")).attr('value')+"/univers/"+jQuery('#univers-'+jQuery(this).attr('id').replace("dec-","")).attr('value')+"/qty/"+jQuery('#qty-'+jQuery(this).attr('id').replace("dec-","")).attr('value'));
			} catch(e) {
			}
		});
		
		// Champs qte
		jQuery('#input-quantite input[name="qty"]').blur(function(){
			var qty=jQuery(this).val();
			var btn_elt=jQuery('.addajax');
			var btn_href=btn_elt.attr('href');
			var btn_qty='/qty/';
			var qty_id=parseInt(btn_elt.attr('id').replace('addajax-',''));
			if (btn_href.indexOf(btn_qty)!=-1)
				btn_href=btn_href.substr(0,btn_href.indexOf(btn_qty));
			btn_elt.attr('href',btn_href+btn_qty+qty);
		});
		
        jQuery('.super-attribute-select').change(function () {
            try {
                var endRequest = '';
                jQuery('.super-attribute-select').each(function() {
                    endRequest += '/'+jQuery(this).attr('name')+'/'+jQuery(this).val();
                });
                jQuery(".addajax").attr('href',"/checkout/cart/addajaxConfigurable/product/"+jQuery('.addajax').attr('id').replace("addajax-","")+"/univers/"+jQuery('#univers-0').attr('value')+"/qty/"+jQuery('#qty-0').attr('value')+endRequest);
            } catch(e) {}
        }); 
		
		
		// COMPARATEUR
		jQuery('.compare').click(function() {
			var param = '';
			var i = 0;
			jQuery(".comparer :checked").each(function() {  
				if (i++<3) 
				{
					param +=jQuery(this).val() +',';
				} 
				else
				{
					param	+= '/trop/1';
				}
			}); 
			jQuery('.comp1').attr('href','/catalog/product_compare/adds/products/'+param);
		});
	
		if (jQuery('.comparer :checked').size() > 0) {
			jQuery('.comp-1').css('display', 'block');
			jQuery('.comp-2').css('display', 'none');
		} else {
			jQuery('.comp-1').css('display', 'none');
			jQuery('.comp-2').css('display', 'block');
		}
		
		jQuery('.comparer').click(function() {
			if (jQuery('.comparer :checked').size() > 0) {
				jQuery('.comp-1').css('display', 'block');
				jQuery('.comp-2').css('display', 'none');
			} else {
				jQuery('.comp-1').css('display', 'none');
				jQuery('.comp-2').css('display', 'block');
			}
		}); 
		
		// AJOUT PANIER AJAX SANS POPUP
		jQuery('.list-product-valid').click(function() {
			jQuery.ajax({  
				type: "POST",  
				url : '/checkout/cart/addajax/',  
				data: "product="+jQuery('#id-product-'+jQuery(this).attr('id')).attr('value')+"&qty="+jQuery('#qty-'+jQuery(this).attr('id')).attr('value')+"&univers="+jQuery('#univers-'+jQuery(this).attr('id')).attr('value'), 
				success: function() { 
					jQuery("#encart-panier").load("/checkout/cart/minicart #content-panier"); 
				}  
			});
		return false;
		});
		
		// AJOUT PANIER AJAX POPUP
		jQuery("a.addajax").fancybox({
			'frameWidth' : 512,
			'frameHeight' : 322,
			'callbackOnShow' : function() {jQuery("#encart-panier").load("/checkout/cart/minicart #content-panier");},
			'overlayShow': true,
			'overlayOpacity': 0.7,
			'titleShow': false,
			'hideOnContentClick' : false
		});
		
		jQuery("a.addajaxpack").fancybox({
			'frameWidth' : 440,
			'frameHeight' : 210,
			'callbackOnShow' : function() {jQuery("#encart-panier").load("/checkout/cart/minicart #content-panier");},
			'overlayShow': true,
			'overlayOpacity': 0.7,
			'titleShow': false,
			'hideOnContentClick' : false
		});
		/*jQuery("a.addajax").fancybox({
			'frameWidth' : 440,
			'frameHeight' : 210,
			'callbackOnShow' : function() {jQuery("#encart-panier").load("/checkout/cart/minicart #content-panier");},
			'zoomSpeedIn': 400, 
			'zoomSpeedOut': 400, 
			'overlayShow': true,
			'hideOnContentClick' : false
		});*/
		
		// remplacement du formulaire par la fancy box pour ajout ajax
		try {
			jQuery('.ajouter-panier').css('display','none');
			jQuery('.new-ajout-panier').css('display','block');
		} catch(e) {
			//alert('erreur');
		}
		
		/*try {
			jQuery('.packajout').css('display','none');
			jQuery('.packajoutnew').css('display','block');
		} catch(e) {
			//alert('erreur');
		}*/
		
		try {
			jQuery('#ajouter-panier').css('display','none');
			jQuery('#ajouter-panier-new-ajout-panier').css('display','');
			jQuery('.upsell-ajouter-panier').css('display','none');
			jQuery('.upsell-ajouter-panier-new-ajout-panier').css('display','');
		} catch(e) {
			//alert('erreur');
		}
		
		// AJOUT WHISHLIST POPUP
		jQuery('.ajouter-liste').fancybox({
			'frameWidth' : 440,
			'frameHeight' : 210,
			//'callbackOnShow' : function() {jQuery("#encart-panier").load("/checkout/cart/minicart #content-panier");},
			'zoomSpeedIn': 400, 
			'zoomSpeedOut': 400, 
			'overlayShow': true,
			'hideOnContentClick' : false
		});
		
		jQuery("#envoyer-ami").fancybox({
    		'frameWidth' : 780,
    		'frameHeight' : 600,
    		'zoomSpeedIn': 400, 
    		'zoomSpeedOut': 400, 
    		'overlayShow': true,
    		'hideOnContentClick' : false
        }); 
		
		//jQuery('a.addajax').click(function() { openpp(); });
		
		// PARTICULIER OU PRO DANS LA CREATION D'UN COMPTE
		jQuery('#radio-professionnel').click(function () {
			jQuery('#form_pro').show();
		});
		jQuery('#radio-particulier').click(function () {
			jQuery('#form_pro').hide();
		});
		
		//	Affiche ou non le champs de saisi du n° de carte PRO
		jQuery('#carte-pro-oui').click(function () {
			jQuery('#field_numcarte').show();
		});
		jQuery('#carte-pro-non').click(function () {
			jQuery('#field_numcarte').hide();
		});
		
		//NEWSLETTER
		//Affiche ou non le choix du magasin
		jQuery('#mag-proche-oui').click(function () {
			jQuery('#select_magasin').attr('disabled','');
			jQuery('#field-select-magasin').show();
		});
		jQuery('#mag-proche-non').click(function () {
			jQuery('#select_magasin').attr('disabled','disabled');
			jQuery('#field-select-magasin').hide();
		});
		
	}
);
