/*##############################################################################################
################### JS DES FONCTIONS DE GESTION DU HEADER UNIVERS ############################# 
/*############################################################################################## */

/*############################################################################################## */
// Parametrage
var shopUrl = "/";

/*############################################################################################## */
/* Chargement du mega-bouton (positionnement, gestion des evenements, ...) */
function chargementUnivers(){
    // Fix pour le menu IE6
    if(X_navigateur == 'microsoft internet explorer' && X_version < 7){
        var SportChild = document.getElementById('sport').firstChild;
        var MarqueChild = document.getElementById('marque').firstChild;
        
        SportChild.firstChild.style.display = 'inline';
        SportChild.style.width = (SportChild.firstChild.offsetWidth+18) + 'px';
        SportChild.firstChild.style.display = 'block';
        
        MarqueChild.firstChild.style.display = 'inline';
        MarqueChild.style.width = (MarqueChild.firstChild.offsetWidth+18) + 'px';
        MarqueChild.firstChild.style.display = 'block';
        
        document.getElementById('cadre-sports').style.width = '800px';
    }
	
    var tempo = null;
    // Cache le cadre au bout de 20 ms
    function hideCadre(bouton,cadre){
        if(tempo == null) tempo = setTimeout(function(){doEventCadreOut(bouton,cadre)},"20");
    }
    // Fonction qui cache le cadre
    function doEventCadreOut(bouton,cadre){
        if(X_navigateur == 'microsoft internet explorer' && X_version < 7){
            changeBlocClass(bouton,'');
        }
        hideBloc(cadre);
        tempo = null;
    }
    // Fonction qui empeche le onmouseout de s executer : le onmouseout s execute a chaque passage sur un element enfant du cadre, il faut donc annuler l action
    function stopEventCadreOut(){
        if (tempo!=null){
            clearTimeout(tempo);
            tempo = null;
        }
    }
    
    // Attribution de l evenement qui stoppe le onmouseout sur chaque element enfant des deux cadres
    var liSport = document.getElementById('sport');
    for(var i = 0;i<liSport.childNodes.length;i++) liSport.childNodes[i].onmouseover = function(){stopEventCadreOut();};
    
    var liMarque = document.getElementById('marque');
    for(var i = 0;i<liMarque.childNodes.length;i++) {liMarque.childNodes[i].onmouseover = function(){stopEventCadreOut();};};
    
    // Position et taille des sous menu
    document.getElementById('cadre-sports').style.left = (document.getElementById('sport').firstChild.offsetLeft) + 'px';
    document.getElementById('cadre-marques').style.left = (document.getElementById('marque').firstChild.offsetLeft ) + 'px';
    document.getElementById('cadre-marques').style.width = (document.getElementById('marque').firstChild.offsetWidth-2) + 'px';
    
    // Action boutton du menu au passage de la souris
    document.getElementById('sport').onmouseover = function(){
        // Cache les SELECT si < IE7
        // Class pour ie6 pour afficher la flèche vers le bas
        if(X_navigateur == 'microsoft internet explorer' && X_version < 7){
            changeBlocClass('sport','ieHover');
            gestion_select('hidden');
        }
        hideBloc('cadre-marques');
        showBloc('cadre-sports');	
        document.getElementById('sport').firstChild.style.zIndex = '902';
        document.getElementById('marque').firstChild.style.zIndex = '901';
    }
    document.getElementById('marque').onmouseover = function(){
        // Class pour ie6 pour afficher la flèche vers le bas
        if(X_navigateur == 'microsoft internet explorer' && X_version < 7) changeBlocClass('marque','ieHover');
        hideBloc('cadre-sports');
        showBloc('cadre-marques');
        document.getElementById('sport').firstChild.style.zIndex = '901';
        document.getElementById('marque').firstChild.style.zIndex = '902';
    }
    
    document.getElementById('sport').onmouseout = function(){hideCadre('sport','cadre-sports');};
    document.getElementById('marque').onmouseout = function(){hideCadre('marque','cadre-marques');};
}
/*############################################################################################## */
/* Construit la liste des marques dans le mega-bouton */
function importNaviMbBrands() {

    var func = function (xmlHttp, httpStatus) {
        oGlobalXml = xmlHttp.responseXML;
        if(oGlobalXml!=null) {
            var marques = oGlobalXml.getElementsByTagName("item");
            var listeMarques = document.getElementById('liste-marques');
            //Le nombre max d articles VS est fixe dans maxNbPrdVs
            for (var i = 0; i < marques.length; i++) {
                var elementListe = document.createElement("li");
                var nomMarque = getChildNodeValue(marques[i],"literal");
                nomMarque = nomMarque.charAt(0).toUpperCase() + nomMarque.substring(1).toLowerCase();
                var lien = createElement_a("marque-" + i, 'marque',nomMarque,'/'+ _language+getChildNodeValue(marques[i],"url"), elementListe);
                listeMarques.appendChild(elementListe);
            }
        }
    }

    var oProcess = new IAjaxProcess(brandListUrl, func);
    oProcess.run();
}
/*############################################################################################## */
/* Cree le panneau de navigation lateral */
function importNaviPanel() {
    var menu_coulissant = document.getElementById("menu_coulissant");
    var cadreSports = document.getElementById('cadre-sports');
    var univers = cadreSports.getElementsByTagName("ul");
    var listeUnivers = document.createElement("ul");

    for(var i = 0;i < univers.length;i++){
        var eltListeUnivers = document.createElement("li");
        if(i == 0) eltListeUnivers.className = "first";
        // Si c est un univers avec une liste de rayons
        if(univers[i].childNodes.length > 1){
            // Ajout d'un univers
            var titreUnivers = document.createElement("span");
            titreUnivers.className = "univers-rayon";
            titreUnivers.appendChild(document.createTextNode(univers[i].firstChild.innerHTML));
            var containerTitreUnivers = createElement_a('','','','#','');
            containerTitreUnivers.appendChild(titreUnivers);
            eltListeUnivers.appendChild(containerTitreUnivers);
            // Ajout de la liste des rayons de l'univers
            var listeRayon = document.createElement("ul");
            var rayons = univers[i].getElementsByTagName("li");
            for(var j = 0;j < rayons.length;j++){
                if(rayons[j].className != "titre")
                    listeRayon.appendChild(rayons[j].cloneNode(true));
            }
            eltListeUnivers.appendChild(listeRayon);
        }
        // Si c est un rayon seul
        else {
            var rayonSeul = univers[i].firstChild.firstChild.cloneNode(true);
            rayonSeul.firstChild.className = "univers-rayon";
            eltListeUnivers.appendChild(rayonSeul);
        }
        listeUnivers.appendChild(eltListeUnivers);
    }
    menu_coulissant.appendChild(listeUnivers);
    
    initSlideDownMenu(false);
}

/*############################################################################################## */
/*############################################################################################## */
/*##################################### BOUTIQUE  ############################################### */
/*############################################################################################## */
/*############################################################################################## */
/* Creation du manu de navigation de gauche de la boutique */
function importShopNaviPanel() {
    var menu_coulissant = document.getElementById("menu_coulissant");
    var cadreSports = document.getElementById('cadre-sports');
    var univers = cadreSports.getElementsByTagName("ul");
    var listeUnivers = document.createElement("ul");

    var iFirstRayon = "";
    var iSelectedUniversMenuItemId;
    var iSelectedUniversId;
    var iSelectedRayon = _idDepartement;
    var idCurrentRayon = "";
    var iCptUnivers = 0;    

    for(var i = 0;i < univers.length;i++){
        var eltListeUnivers = document.createElement("li");
        if(i == 0) eltListeUnivers.className = "first";
        // Si c est un univers avec une liste de rayons
        if(univers[i].childNodes.length > 1){
            iCptUnivers ++;
            // Ajout d'un univers
            var titreUnivers = document.createElement("span");
            titreUnivers.className = "univers-rayon";
            titreUnivers.appendChild(document.createTextNode(univers[i].firstChild.innerHTML));
            //var containerTitreUnivers = createElement_a('','','','#','');
            var containerTitreUnivers = createElement_a('univers_' + i,'','','#','');
            containerTitreUnivers.appendChild(titreUnivers);
            eltListeUnivers.appendChild(containerTitreUnivers);

            // Ajout de la liste des rayons de l'univers
            var listeRayon = document.createElement("ul");
            var rayons = univers[i].getElementsByTagName("li");
            for(var j = 0;j < rayons.length;j++){
                if(rayons[j].className != "titre") {
                    var myLink = rayons[j].cloneNode(true);
                    idCurrentRayon = myLink.firstChild.firstChild.id;
                    // Assignation des variables de gestion du menu
                    if (iFirstRayon == "") iFirstRayon = idCurrentRayon;
                    if (idCurrentRayon == iSelectedRayon) {
                        iSelectedUniversMenuItemId = iCptUnivers;
                        iSelectedUniversId = i;
                        myLink.firstChild.firstChild.style.fontWeight = "bold";
                    }
                    // Traitement
                    myLink.firstChild.firstChild.href = shopUrl + "?idRayon=" + idCurrentRayon;
                    myLink.firstChild.firstChild.id = "rayon_" + idCurrentRayon;
                    listeRayon.appendChild(myLink);
                    }
            }
            eltListeUnivers.appendChild(listeRayon);
        }
        
        // Si c est un rayon seul
        else {
            var rayonSeul = univers[i].firstChild.firstChild.cloneNode(true);
            idCurrentRayon = rayonSeul.firstChild.id;
            // Assignation des variables de gestion du menu
            if (iFirstRayon == "") iFirstRayon = idCurrentRayon;
            //if (idCurrentRayon == iSelectedRayon) iSelectedUnivers = i;
            // Traitement
            rayonSeul.firstChild.className = "univers-rayon";
            rayonSeul.firstChild.href  = shopUrl + "?idRayon=" + idCurrentRayon;
            rayonSeul.firstChild.id = "rayon_" + idCurrentRayon;
            eltListeUnivers.appendChild(rayonSeul);
        }
        
        listeUnivers.appendChild(eltListeUnivers);
    }
    
    menu_coulissant.appendChild(listeUnivers);

    // Init du menu coulissant
    initSlideDownMenu(false);
    // Mise en valeur element selectionne
    if (iSelectedUniversId != null) {
        showSubMenu(true,document.getElementById("mainMenuItem" + iSelectedUniversMenuItemId));
        var oMyElt = document.getElementById("univers_" + iSelectedUniversId);
        if (oMyElt) oMyElt.id = "da";
    }
    else {
        var oMyElt = document.getElementById("rayon_" + iSelectedRayon);
        if (oMyElt) oMyElt.id = "da";
    }

    return (iFirstRayon);
}

/*############################################################################################## */
/* Assure la gestion du mega-bouton de la boutique */
function megaBoutonShopHandler(sCookieMagasin) {
    // Cas magasin
    if (sCookieMagasin != null) {
        if (getSwitchMagasin('FLAG_IS_SOLD_PERIOD') && getSwitchSite('SHOP_SALES')) _mbShopAffBouton();
    }
    // Cas centrale
    else {
        if (getSwitchSite('FLAG_IS_SOLD_PERIOD') && getSwitchSite('SHOP_SALES')) _mbShopAffBouton();
    }
}

/*############################################################################################## */
function _mbShopAffBouton() {
    // Affiche bouton
    showBloc("shop-sales","inline");
    // Elargissement zone boutons
    var oBts = document.getElementById("mega-bouton");
    if (oBts) oBts.style.width= "370px";
    // Deplace MDR
    var oMdr = document.getElementById("bandeaurecherche");
    if (oMdr) oMdr.style.left = "370px";
}

/*############################################################################################## */
function _mbShopGetFirstRayon() {
    var cadreSports = document.getElementById('cadre-sports');
    var univers = cadreSports.getElementsByTagName("ul");
    var iFirstRayon = "";

    for(var i = 0;i < univers.length;i++){
        // Si c est un univers avec une liste de rayons
        if(univers[i].childNodes.length > 1){
            var rayons = univers[i].getElementsByTagName("li");
            for(var j = 0;j < rayons.length;j++){
                if(rayons[j].className != "titre") {
                    var myLink = rayons[j].cloneNode(true);
                    if (iFirstRayon == "") iFirstRayon = myLink.firstChild.firstChild.id;
                    }
            }
        }
        
        // Si c est un rayon seul
        else {
            var rayonSeul = univers[i].firstChild.firstChild.cloneNode(true);
            if (iFirstRayon == "") iFirstRayon = rayonSeul.firstChild.id;
        }
        
    }
    return (iFirstRayon);
}