$(document).ready(function () {
    axis.themes.defaultTheme.initAddToCartButtonAnim();
    axis.themes.defaultTheme.initBreadcrumb();
    axis.themes.defaultTheme.initProductsTabs();
    axis.themes.defaultTheme.adjustCartTabSize();
    axis.themes.defaultTheme.collapseNouveaute();
    axis.themes.defaultTheme.initSiteMapTreeView();
    axis.themes.defaultTheme.initMobileSlide();

    axis.themes.benjo.initAddToCartButtonAnim();
    axis.themes.benjo.HideNextButtonCart();
    axis.themes.benjo.stepIndent_adjust();
    axis.themes.benjo.MoveLoginPasswordInCart();
    axis.themes.benjo.CyclingProductSelection();
    axis.themes.benjo.CarouselRelatedProducts();
    axis.themes.benjo.MenuRollHover();
    axis.themes.benjo.formMoveValidator(); //utilisé pour déplacer les validateurs dans le formulaire d'inscription
    axis.themes.benjo.cakemail();

    preSetShipMode();
    hideConfirmationStuff();

    //Dans le processus mot de passe oublié
    afficherBoutonEnter();

    //Dans fiche
    setFiche();
    ajouterFlecheMenu();
    cacherListeProduits();
    creationFilAriane();
    afficherCartMenuGauche();

    //Ajustement CakeMail
    setCakeMail();

    //ajustement margin listeProduit
    setMarginProdList();


});


if (!axis) { var axis = {}; }
if (!axis.themes) { axis.themes = {}; }
if (!axis.themes.defaultTheme) {
	axis.themes.defaultTheme = {};


	///Anime les boutons addto cart
	axis.themes.defaultTheme.initAddToCartButtonAnim = function () {
		//add to cart FR
		$("body.l_fr .prodlist .list .prod .ppd div.add2c ").hover(
			function () { $(this).stop().animate({ width: '130px' }, 'fast'); },
			function () { $(this).stop().animate({ width: '40px' }, 'fast'); }
		);
		//add to cart EN
		$("body.l_en .prodlist .list .prod .ppd div.add2c ").hover(
			function () { $(this).stop().animate({ width: '105px' }, 'fast'); },
			function () { $(this).stop().animate({ width: '40px' }, 'fast'); }
		);

		//add to wishlist FR
		$("body.l_fr .prodlist .list .prod .ppd div.add2wl ").hover(
			function () { $(this).stop().animate({ width: '185px' }, 'fast'); },
			function () { $(this).stop().animate({ width: '40px' }, 'fast'); }
		);
		//add to wishlist EN
		$("body.l_en .prodlist .list .prod .ppd div.add2wl ").hover(
			function () { $(this).stop().animate({ width: '140px' }, 'fast'); },
			function () { $(this).stop().animate({ width: '40px' }, 'fast'); }
		);

		//add to giftlist FR
		$("body.l_fr .prodlist .list .prod .ppd div.add2gl ").hover(
			function () { $(this).stop().animate({ width: '170px' }, 'fast'); },
			function () { $(this).stop().animate({ width: '40px' }, 'fast'); }
		);
		//add to giftlist EN
		$("body.l_en .prodlist .list .prod .ppd div.add2gl ").hover(
			function () { $(this).stop().animate({ width: '135px' }, 'fast'); },
			function () { $(this).stop().animate({ width: '40px' }, 'fast'); }
		);
	};


	///Crée les onglets dans la fiche produit
	axis.themes.defaultTheme.initProductsTabs = function () {

		$(".proddetail .tab_content").hide();
		$(".proddetail ul.prod-tabs li:first").addClass("active").show();
		$(".proddetail .tab_content:first").show();

		//Sur le click
		$(".proddetail ul.prod-tabs li").click(function () {

			$(".proddetail ul.prod-tabs li").removeClass("active");
			$(this).addClass("active");
			$(".proddetail .tab_content").hide();

			var activeTab = $(this).find("a").attr("href"); //commme les ancres
			$("#" + activeTab.split("#")[1]).fadeIn();
			return false;
		});
	};


	///Ajuste la largeur des tabs dans le cart, si l'identification est activée
	axis.themes.defaultTheme.adjustCartTabSize = function () {

	};


	///Collapse les nouveautés par type
	axis.themes.defaultTheme.collapseNouveaute = function () {

		//On les collapse seulement si le paging n'est pas actif car le code ne supporte pas le scroll à la position initiale
		if ($(".axm_nouveaute div.pagingIndexCtrl div.resultsDisplay").length <= 0) {

			$(".axm_nouveaute h3~div.type-container").hide();

			var firstUl = $(".axm_nouveaute h3~div.type-container").get(0);
			if (firstUl) { $(firstUl).show(); }

			$(".axm_nouveaute h3").css('cursor', 'pointer');
			$(".axm_nouveaute h3").click(function () {
				$(".axm_nouveaute h3~div.type-container").slideUp();
				var ul = $(this).nextAll("div.type-container");
				if (!ul.is(":visible")) {
					ul.slideDown();
				}
			});
		}
	};


	///Transforme le plan du site en treeview
	axis.themes.defaultTheme.initSiteMapTreeView = function () {

		$(".axm_sitemap>ul").addClass("filetree"); //ajout classe sur le conteneur		
		$(".axm_sitemap>ul>li>a").wrap('<span class="folder" />'); //classe supersection
		$(".axm_sitemap>ul>li>ul>li>a").wrap('<span class="folder" />'); //classe section
		$(".axm_sitemap>ul>li>ul>li>ul>li>a").wrap('<span class="file" />'); //classe page
		//on transforme en treeview
		$(".axm_sitemap>ul").treeview({
			collapsed: true,
			animated: "medium",
			persist: "location"
		});
	};

	///Ajoute les ">" dans le fil d'ariane
	axis.themes.defaultTheme.initBreadcrumb = function () {
		//on exclue les subpagenavigation non sélectionnée car ils sont renderé pareil
		var $crumb = $('.axc_breadcrumb li li:not(li.m_sp_not_sel)');
		if ($crumb.size() > 0) {
			$crumb.before(' > ');
		}
	};

	axis.themes.defaultTheme.initMobileSlide = function () {

		$(".containerLnks").hover(
			function () { $(this).stop().animate({ width: '135px' }, 'fast'); },
			function () { $(this).stop().animate({ width: '10px' }, 'fast'); }
		);
	};
};

if (!axis.themes.benjo) {
	axis.themes.benjo = {};

	//déplace l'explorer de wishlist dans la colonne de gauche pour un design plus ergonomique
	axis.themes.benjo.initAddToCartButtonAnim = function () {
		$('.wishlist .cart_left').append($('.wishListExplorer'));
	};

	axis.themes.benjo.HideNextButtonCart = function () {
		if ($('.step_conf').length > 0) {    
			$('.benjoBtn, .checkout_steps').hide();           //nav + checkout steps
		}
	};

	axis.themes.benjo.stepIndent_adjust = function () {
		if ($('.step_ident').length > 0 && $('.ax_inscription').length == 0) {
			$('body').addClass('step_ident');
		}
		else if ($('.step_ident').length > 0 && $('.ax_inscription').length > 0) {
			$('.checkout_btnzone').hide();
			$('.cart_left').hide();
		}
	};

	axis.themes.benjo.MoveLoginPasswordInCart = function () {
		$('.step_ident .identification .log_labelzone.password span.lbl').after($('.step_ident .identification .log_linkzone'));
	};
	
	axis.themes.benjo.CyclingProductSelection = function() {
		$(".select_1 .prod").wrapAll('<div class="cycleroot">');
		$(".select_2 .prod").wrapAll('<div class="cycleroot">');
	    $(".select_1 .cycleroot").cycle({ fx: 'scrollRight', speed: 500, timeout: 5000});
		$(".select_2 .cycleroot").cycle({ fx: 'scrollRight', speed: 500, timeout: 5000, delay: 1000 });

		$(".l_fr .select_1").prepend('<div class="nouveautes_fr"><img src="themes/default/images/Benjo/nouveautes.png"> </div>');
		$(".l_en .select_1").prepend('<div class="nouveautes_en"><img src="themes/default/images/Benjo/nouveautes_en.png"> </div>');
		$(".l_fr .select_2").prepend('<div class="aubaines_fr"><img src="themes/default/images/Benjo/aubaine.png"> </div>');
		$(".l_en .select_2").prepend('<div class="aubaines_en"><img src="themes/default/images/Benjo/aubaine_en.png"> </div>');
	};

	axis.themes.benjo.CarouselRelatedProducts = function () {
		var VISIBLE = 4;

		var lists = $(".prodlist_related div.list_related div.prod_related");
		lists.wrapAll('<div class="jCarouselLite"><ul></ul></div>');
		lists.wrap('<li>');
		if (lists.length >= VISIBLE) {
			$(".jCarouselLite").jCarouselLite({ auto: 5000, visible: VISIBLE });
		}
	};
	
	axis.themes.benjo.MenuRollHover = function() {
		$('#filtreCatalogue .fltr').each(function () {

			if ($('h4', $(this)).length > 0) {
				$('h4', $(this)).hover(
					function () {
						$(this).next('.lbl_featgrp').show();
					},
				/*au survol du sous-menu*/
					function () {
						$(this).next(".lbl_featgrp").hover(
							function () { $(this).show(); },
							function () { $(this).hide(); }
						);
						$(this).next('.lbl_featgrp').hide();
					}
				);
			}
			else if ($('h3', $(this)).length > 0) {
				$('h3', $(this)).hover(
					function () {
						$(this).next('.divRptCategorie').show();
					},
					function () {
						$(this).next(".divRptCategorie").hover(
							function () { $(this).show(); },
							function () { $(this).hide(); }
						);
						$(this).next('.divRptCategorie').hide();
					}
				);
			}
		});
    };
    axis.themes.benjo.formMoveValidator = function () {
        //on passe au travers de tous les <div class="requis"> 
        $('.axm_inscription .requis').each(function () {
            //et on les append dans le ".lbl" contenu dans le ".line" le plus proche
            $(this).appendTo($('.lbl', $(this).next(".line")));
        });
        //cas spécial pour la confirmation qui n'a pas le même mark up
        $(".confirmPassword .requis").remove();
        $(".confirmPassword .MessageErreur").appendTo('.confirmPassword .lbl');
        $(".requisconfirm").appendTo('.confirmPassword .lbl');

       };

       axis.themes.benjo.cakemail = function () { 
       	var $fields = $("input.cm_inputMail");
       	$fields.addClass('cmauto');

       	// Valable en tout temps onFocus vide les champs
       	$fields.each(function (i) {
       		$(this).data('backupValue', this.value);

       		$(this).focus(function () {
       			if ($(this).hasClass('cmauto')) {
       				$(this).val($(this).data('backupValue'));
       				$(this).removeClass('cmauto');
       			}
       		});
       		$(this).blur(function () {
       			if ($(this).val() == '') {
       				$(this).addClass('cmauto');
       				//si FR
       				if ($fields.length > 0) {
       					if ($('body').hasClass('l_fr')) {
       						$fields[0].value = "Courriel";
       					}
       					else {
       						$fields[0].value = "Email";
       					}
       				}
       			}
       		});
       	});

       	//si FR
       	if ($fields.length > 0) {
       		if ($('body').hasClass('l_fr')) {
       			$fields[0].value = "Courriel";
       		}
       		else {
       			$fields[0].value = "Email";
       		}
       	}
       };

};

//on coche par défaut le premier choix de shipping
function preSetShipMode() {
    if ($(".shipmode_table").length > 0 && $(".shipmode_table tr td input:checked").length <= 0) {
        $(".shipmode_table tr:first-child td input").attr('checked', 'checked');
    }
}
//Cache les liens de FAQ à l'étape de confirmation.
function hideConfirmationStuff() {
    if ($(".step_conf").length > 0 || $(".step_validation").length>0) {
        $(".cart_links_include").not(":eq(0)").css('display', 'none');
    }
}



//show bouton ENTRER dans le processus de "forgetten password"
function afficherBoutonEnter() {
    if ($(".log_summary > *").is(":visible") || !$(".identification  .log_usagerlogin .password").length) {
        $(".log_buttonzone").show();
        $(".divUseAccountIntro .identification").append($(".divUseAccountIntro .benjoBtn"));
    }
}


function setFiche() {
    if ($(".proddetail").length) {
        //Désactive l'ouverture du pop up de quantity 
        //quand le produit est en rupture de stock ( via .oos )
        $(".proddetail .oos .add2c a").removeAttr('href');
        $(".t_2 #colRecent").css('margin', '0');
    }
    if ($(".axc_advsearch_prod").length) {
        $(".t_1 .prodlist_recent").css('margin-top', '64px');
    }
}

function creationFilAriane(){

    //Fil d'ariane
    if ($('#tagBody').hasClass('l_fr'))
    {
        $("#colTop .menu ul li.m_ss_sel").before("<li><a href='accueil.aspx'>Accueil</a></li>");
    }
    else {
        $("#colTop .menu ul li.m_ss_sel").before("<li><a href='accueil.aspx'>Home</a></li>");
    }
    
      $("#colTop .menu ul li a").after("<span class='filArianeSeparation'> &gt;</span>");

    $("#colTop .menu li.m_s_sel .filArianeSeparation").css("display", "none");
}

function cacherListeProduits() {

    //Liste Produits récemment consultés
    if ($(".list_recent").children().length == 0) 
    {  
        $(".list_recent").append("<span> Aucun article consult&eacute; </span>");
        $(".l_en .list_recent").append("<span>Any items recently viewed</span>");
   
    }

}

function ajouterFlecheMenu() {
    //flêche à côté des filtres de menu
    $("h3#filtre_lbFiltreCategorie").append("<span style='padding-left:8px'><img src='themes/benjo5001/images/Benjo/fleche_filtre.png'/></span>");
    $(".feat_grp h4").append("<span style='padding-left:8px'><img src='themes/benjo5001/images/Benjo/fleche_filtre.png'/></span>");

}

function afficherCartMenuGauche() {
    //faire apparaitre le menu de gauche dans la création de compte via panier
    if ($(".step_ident").length > 0 && $(".axm_inscription").length > 0) {
        $("body.step_ident .cart_left").css('cssText', 'display: block !important');
    }

}

function setCakeMail() {

    if ($(".axm_cakeMail").length >0) {
        $(".axm_cakeMail .valUserName").removeAttr("style");
        $(".axm_cakeMail .valUserName").css({ 'color': 'red', 'display': 'none' });
    }
}

function setMarginProdList() {
    //results de recherche
    $(".results .prod .prodlist .prod").css('margin-right', '29px');
    $(".axm_prodlist .list .prod").css('margin-right', '34px');
    $(".axm_prodlist .list .prod:nth-child(3n+3)").css("margin-right", "0");
    var i = -1;
    $(".results .prod .prodlist .prod").each(function () {
        i = i + 1;
        if (i % 3 == 2) {
            $(this).css("margin-right", "0");
        }
    });
}
