/*##############################################################################################
################### JS DES FONCTIONS DE GESTION DU HEADER UNIVERS ############################# 
/*############################################################################################## */

/*############################################################################################## */
/* Chargement du menu de navigation (positionnement, gestion des evenements, ...) */
function chargementUnivers() {
    // Correction du bug IE6
    try { 
        document.execCommand("BackgroundImageCache", false, true); 
    } 
    catch(err) {}
    var boutons = document.getElementById('menu-navigation').firstChild.childNodes;
    var cadres = new Array();
    for(var i = 0;i < boutons.length;i++)
        if(boutons[i].firstChild.id.indexOf('cadre-') != -1) cadres.push(boutons[i].firstChild);

    var tempo = null;
    var idBoutonActif = '';
    // 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) {
        changeBlocClass(bouton,'bouton');
        if(document.getElementById(cadre)) hideBloc(cadre);
        if(idBoutonActif == 'sport') gestion_select('visible');
        tempo = null;
        idBoutonActif = '';
    }
    // 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 onmouseover sur chaque bouton
    for(var i = 0;i < boutons.length;i++)
        boutons[i].onmouseover = function() {
            if(idBoutonActif == this.id) stopEventCadreOut();
            else {
                changeBlocClass(idBoutonActif,'bouton');
                hideBloc('cadre-' + idBoutonActif);
                // Cache les SELECT si < IE7
                if(this.id == 'sport' && X_navigateur == 'microsoft internet explorer' && X_version < 7) gestion_select('hidden');
                // Reaffiche les SELECT si < IE7
                else if(idBoutonActif == 'sport' && X_navigateur == 'microsoft internet explorer' && X_version < 7) gestion_select('visible');
                idBoutonActif = this.id;
                showBloc('cadre-' + this.id);
                this.className = 'bouton over';
            }
        };

    // Attribution de l evenement onmouseout sur chaque bouton
    for(var i = 0;i < boutons.length;i++) boutons[i].onmouseout = function(){hideCadre(this.id,'cadre-' +this.id);};

    // Attribution de l evenement qui stoppe le onmouseout sur chaque element enfant des boutons
    for(var i = 0;i < boutons.length;i++)
        for(var j = 0;j < boutons[i].childNodes.length;j++) boutons[i].childNodes[j].onmouseover = function(){stopEventCadreOut();};

    // Taille des cadres (sauf cadre sport)
    var tailleCadre, tailleBgCadreHautG, idBouton;
    for(var i = 0;i < cadres.length;i++) {
        if(cadres[i].id.indexOf('cadre-sport') == -1) {
            idBouton = cadres[i].id;
            idBouton = idBouton.substr(idBouton.indexOf('-') + 1);
            tailleCadre = document.getElementById(idBouton).offsetWidth;
            cadres[i].style.width = tailleCadre + 14 + "px";
            
            // Taille des span haut et bas
            tailleBgCadreHautG = parseInt(tailleCadre / 2);
            document.getElementById("bgCadreHautG-" + idBouton).style.width = tailleBgCadreHautG - 5 + "px";
            document.getElementById("bgCadreHautD-" + idBouton).style.width = tailleCadre - tailleBgCadreHautG - 5 + "px";
            document.getElementById("bgCadreBas-" + idBouton).style.width = tailleCadre + "px";
            
            // Taille de la liste (taille du cadre avec les coins arrondis - les paddings de 10px - les 2 bordures de 1px)
            document.getElementById("liste-" + idBouton).style.width = (tailleCadre + 14) - 22 + "px";
        }
    }
}

/*############################################################################################## */
/* Gere l evenement onmousewheel sur le menu-sport flash de la homepage */
function hookEvent(element, eventName, callback) {
    if (typeof(element) == "string") element = document.getElementById(element);
    if (element == null) return;
    if (element.addEventListener) {
        if(eventName == 'mousewheel') element.addEventListener('DOMMouseScroll', callback, false);  
            element.addEventListener(eventName, callback, false);
    }
    else if(element.attachEvent) element.attachEvent("on" + eventName, callback);
}
function cancelEvent(e) {
    e = e ? e : window.event;
    if(e.stopPropagation) e.stopPropagation();
    if(e.preventDefault) e.preventDefault();
    e.cancelBubble = true;
    e.cancel = true;
    e.returnValue = false;
    return false;
}
function printInfo(e) {
    e = e ? e : window.event;
    var normal = e.detail ? e.detail * -1 : e.wheelDelta / 40;
    var sens = normal / Math.abs(normal);
    
    cancelEvent(e);
    
    document.getElementById('so-flash-menu-sports').scrollMenu(sens);
}

/*############################################################################################## */
/* Cree le panneau de navigation lateral sur la HP */
function importNaviPanel() {
    var divSports = document.getElementById("menu-sports");
    if(divSports) {
        var sports = document.getElementById('cadre-sport').getElementsByTagName("a");
        var divContenu = document.createElement("div");
        var menuGauche = document.createElement("ul");
    
        for(var i = 0;i < sports.length;i++){
            var liSport = document.createElement("li");
            liSport.appendChild(sports[i].cloneNode(true));
            menuGauche.appendChild(liSport);
        }
        divContenu.appendChild(menuGauche);
        divSports.appendChild(divContenu);
    }
}

/*############################################################################################## */
/* Assure la gestion du bouton de la boutique soldes */
function megaBoutonShopHandler(sCookieMagasin) {

    if(getSwitchSite('SHOP_SALES'))
        if ((sCookieMagasin != null && getSwitchMagasin('FLAG_IS_SOLD_PERIOD')) || (sCookieMagasin == null && getSwitchSite('FLAG_IS_SOLD_PERIOD'))) showBloc("shop-sales","inline");
}

function displayBoutonShopPromo() {
    var idMagasin = "null";
    if(cookieMagasin != null) idMagasin = getEltFromUrl(cookieMagasin, 'idMagasin');
    
    var cookieArbo = getCookie("cookieArbo-promo");
    var idMag = null;
    if(cookieArbo != null) idMag = getEltFromUrl(cookieArbo, 'idMag');
    
    if(cookieArbo && idMag == idMagasin) {
        var unRayon = getEltFromUrl(cookieArbo, 'unrayon');
        if(unRayon == "true") showBloc("bouton-promo");
    }
    else {
        // Creation du contexte
        var oContext = new JoServiceContext(_urlErep, _language, _siteNumber, _centralNumber, idMagasin);
        oContext.setDepartments();
        oContext.setPriceKindFilter(SERVICE_CONSTANTS.PRICE_KIND_PROMO);
        // Creation du service
        var oService = new JoService(oContext);  
        // Execution du service
        oService.execute(POST_DISPLAY_BOUTON_SHOP_PROMO);
    }
}

var POST_DISPLAY_BOUTON_SHOP_PROMO = function(httpStatus, _joService) {
    var oCardinalities = _joService.getCardinality(SERVICE_CONSTANTS.CARD_KIND_DEPT);
    var cardinalities = oCardinalities.getCardinalities(SERVICE_CONSTANTS.CARD_KIND_DEPT);

    var idMagasin = null;
    if(cookieMagasin != null) idMagasin = getEltFromUrl(cookieMagasin, 'idMagasin');
    var valeurCookieArbo = "idMag=" + idMagasin + "&amp;rayons=";
    
    if(cardinalities) {
        for (var i = 0;i < cardinalities.length;i++) 
            valeurCookieArbo += cardinalities[i].substring(cardinalities[i].indexOf("#",1) + 1, cardinalities[i].lastIndexOf("#")) + "-";

        showBloc("bouton-promo");
        valeurCookieArbo += "&amp;unrayon=true";
    }
    else valeurCookieArbo += "&amp;unrayon=false";
    
    setCookie("cookieArbo-promo",valeurCookieArbo,null,'/');
}
