/*##############################################################################################
############# JS DES FONCTIONS D APPEL DE CHARGEMENT AJAX DES XML ET DE POST CHARGEMENT ####### 
#################################### POUR LA PARTIE SWITCH ##################################### 
/*############################################################################################## */
var loadedSwitches = false; 
var switchStatusError = false;
/*############################################################################################## */
/* Met le flag switchStatusError a true si probleme lors de la reponse http de chargement des switch */
var POST_LOAD_SWITCH_SERVICE_FUNCTION = function postSwitchService(httpStatus, _joService) {
            // Si la reponse du service switch n est pas correct : pb erep --> mode degrade   
            if(httpStatus != null && httpStatus==200 && _validateSwitchServiceReponse(_serviceSwitchContextId)) {
        	        loadedSwitches = true;		
            }
            else {
                       loadedSwitches = true;
        	        switchStatusError = true;
        	        hideBloc('blocPrixTpsReel');
        	        _urlErep = null; // si _urlErep vaut null, aucun service ne fonctionnera -> mode degrade
            }        
}
/*############################################################################################## */
/* Determine les parametres pour la chargement des switchs en fonction de la presence du cookie : switch site OU switch site et magasin*/
function loadSwitchesConnectOrNot() {
                var numMagCookie = getEltFromUrl(getCookie('decathlon'), 'idMagasin');
                if(numMagCookie!=undefined){	
                        // cas magasin 
                        loadSwitches (_client, SWITCH_KIND_SITE, _siteNumber, _siteNumber, SWITCH_KIND_STORE, numMagCookie, numMagCookie);
                }
                else {
                        // cas centrale 
                        loadSwitches (_client, SWITCH_KIND_SITE, _siteNumber, _siteNumber, null, null, null);
                }
}
/*############################################################################################## */
/* Charge tous les switchs (site OU switch site et magasin) */
function loadSwitches(_client, _kind1, _number1, _subNumber1, _kind2, _number2, _subNumber2) {	
               _urlErep = getUrlErep(_SERVICE_SWITCH, _client, _kind1, _number1, _subNumber1, _kind2, _number2, _subNumber2, POST_LOAD_SWITCH_SERVICE_FUNCTION) ;                
               if(_urlErep != "" && _urlErep!=undefined){             
                    _serviceSwitchContextId = _initAndExecuteSwitchService(_urlErep, getErepLanguageCode( _language), _number1, null, _number2,_kind1,_number1,_subNumber1,_kind2,_number2,_subNumber2,POST_LOAD_SWITCH_SERVICE_FUNCTION); 
                }
}
/*############################################################################################## */
/* Liste des actions a executer en fonction de l etat des switch et du cookie magasin et de la page sur laquelle on se trouve */
function postSwitchAction() {
            if (loadedSwitches == null || ! loadedSwitches) {
                    window.setTimeout("postSwitchAction()", 10);
            } 
            else {
                    // COMMUN A TOUTES PAGES 
                    // Test si connexion valide
                     cookieMagasin = isConnexionEnable();

                    // Maj de l ID centrale
                    getCentralId(cookieMagasin);

                   // COMMUN A TOUTES PAGES SAUF GDBC, VENTE COMPLEMENTAIRE, EBOOKING-STEPS ET STOCKS MULTI-MAGS CAR IFRAME
                   if (typePage!='famille-gdbc' && typePage!='modele-vc' && typePage!='ebooking-steps' && typePage!='psd' && typePage!='oxylane-comments') {
                           if(switchStatusError){
                                // Affichage de la popup d'infos
                                var iframe = createElement_iframe("popup-hp-0", "", "", "", "", null, document.body);
                                iframe.style.display = 'none';
                                openNewWindowScrollEffect(0,window.parent.urlPopupBugErep,'popup-bug-service',null);
                            }
                            // search engine : update du central number
                            searchEngineHandler();
                            // Header                        
                           showBlocIfCookieHeader(cookieMagasin);
                           // Chargement des parametres du header univers
                           chargementUnivers();
                           // Creation de la liste des marques
                           //importNaviMbBrands();
                           // Moteur de recherche                           
                            if (!getSwitchSite('ACTIVATION_MDR')) {                                 
                                hideBloc('bandeaurecherche');
                            }
                             // Lien changement de langue a cote des guides conseils
                            if (_siteNumber != "4809998") showBloc('affichlienlangue','inline');
                            // Skin de Noel
                            changeClassIfXmas();
                            // Skin pour image d ambiance
                            changeClassIfOpeco();
                            // Soldes
                            changeClassIfSold(cookieMagasin);
                            // Ajout d un gestionnaire sur l evenement onclick du doucment
                            window.document.onclick = documentOnclickEventHandler;
                            // Modification du lien contact-us dans le bottom-link selon magasin
                            /*if(document.getElementById('contactLink') && cookieMagasin)
                                getStoreInfos(getEltFromUrl(cookieMagasin, "urlMag"),POST_LOAD_CONTACT_STOREINFOS_FUNCTION);*/

                            // Gestion du bouton shop soldes
                            megaBoutonShopHandler(cookieMagasin);
                            // Gestion du bouton shop promo
                            if((typePage != "miniBoutique" || typeBoutique != "mega-shop-promo") && document.getElementById("bouton-promo")) displayBoutonShopPromo(cookieMagasin);

                            // Gestion des popups au site
                            popupsHandler(cookieMagasin);

                            // Tag repartition visiteurs connectes
                            if (typeof pageTracker != 'undefined') gaUserIsConnected(cookieMagasin);
                            
                            // Suppression du cookie famille s il en existe un
                            if(typePage != 'famille') deleteCookie('famille');
                    }
                   
                   // SPECIFIQUE PAGE                      
                   // Homepage
                    if (typePage=='homepage') {
                        showBlocIfCookieHome(cookieMagasin,rootPath);
                        // Creation du panneau lateral de navigation
                        importNaviPanel();
                        // Gestion de l evenement mousewheel sur le flash menu-sports
                        hookEvent('so-flash-menu-sports', 'mousewheel', printInfo);
                        // TG Homepage   
                        if (PSI_QMODELS_ARRAY.length > 0 && !cookieMagasin) {
                           loadModelsConnectOrNot(cookieMagasin);       
                        }
                    }
                    // Rayon classic
                    if (typePage=='rayon') {                            
                        showBlocIfCookieRayon(cookieMagasin);   
                         // TG Rayon                            
                         if (typeof PSI_QMODELS_ARRAY != 'undefined' && PSI_QMODELS_ARRAY.length>0) {
                            loadModelsConnectOrNot(cookieMagasin);   
                         }
                         else
                             if (tabTypeBoutique.length > 0 && getSwitchSite('THEMATIC_SHOPS')) loadCardinaliteBoutiqueConnectOrNot(cookieMagasin);
                    }
                      // Famille classic
                    if (typePage=='famille') {
                        // Produit hero Famille
                        if (PSI_QMODELS_ARRAY.length>0) {
                            typePage='famille-hero';
                            loadModelsConnectOrNot(cookieMagasin);
                        }
                        // Depth famille
                        else loadFamilyConnectOrNot(cookieMagasin);
                        showBlocIfCookieFamilleClassic(cookieMagasin);
                    }    
                    // Famille gdbc
                     if (typePage=='famille-gdbc') {
                          loadFamilyConnectOrNot(cookieMagasin);   
                          showBlocIfCookieFamilleComparatif();
                     }
                    // Modele classic
                     if (typePage=='modele') {

                        // Prix stock modele
                        loadModelConnectOrNot(cookieMagasin, _language);

                        //Gestion SlideShow
                        if (cookieMagasin !=null) {	
		myIdMagasin = getEltFromUrl(cookieMagasin, 'idMagasin');
		loadFamily(_client, _siteNumber, _centralNumber, myIdMagasin, _familyId);
	         }
	         else loadFamily(_client, _siteNumber, _centralNumber, null, _familyId);
                    }                                
                    
                    // Modele vente complementaire                         
                     if (typePage=='modele-vc') {                                 
                         if (PSI_QMODELS_ARRAY.length>0) {
                            loadModelsConnectOrNot(cookieMagasin);   
                         }
                    }                         

                    // HP Marque
                     if (typePage=='HPMarque') {  
                         // TG HP Marque    
                         if (typeof PSI_QMODELS_ARRAY != "undefined" && PSI_QMODELS_ARRAY.length>0) {
                            loadModelsConnectOrNot(cookieMagasin);   
                         }
                    }   
                     // Modele magasin
                     if (typePage=='magasin') {               
                         loadThirdParty(_client, THIRD_KIND_STORE, idMagasinEm, idMagasinEm);	
                    }
                    // Mini boutique
                      if (typePage=='miniBoutique') {
                          if (typeBoutique.indexOf("mega-") != -1) loadArboMegaShop(cookieMagasin);
                          else loadBoutiqueConnectOrNot(cookieMagasin);
                      }
                    // iFrame ebooking-steps
                    if (typePage=='ebooking-steps') {

                        if (cookieMagasin!=null) {
                            idMagasin = getEltFromUrl(cookieMagasin, 'idMagasin');
                            // Affichage bouton resa dans la div d explication du service eresa    
                            if(getSwitchSite('BOOKING') && getSwitchMagasin('FLAG_BOOKABLE')) {
                                showBloc('boutonStartResa'); 
                            }
                        }
                        else {
                            // Affichage bouton connection magasin dans la div d explication du service eresa            
                            if (getSwitchSite('LOCAL_STOCK')) {
                                showBloc('boutonConnectStore');
                            }
                        }
                        
                        // SPEC BE : affichage bouton voir video
                        if (_siteNumber == "10696451") {
                            hideBloc('boutonStartResa');
                            hideBloc('boutonConnectStore');
                            showBloc('boutonShowVideo');
                        }
                        
                        // Affichage des blocs adequats en fonction du parametre passe dans l url                     
                        var bmask = window.location.search;
                        if (bmask != "") {
                            var bmaskIndex = bmask.indexOf("=");
                            if (bmaskIndex != 0) {
                                bmask = bmask.substring(bmaskIndex + 1);

                                if (bmask == '1') {
                                    // Si on passe dans l url la parametre bmask = 1, on masque certains blocs de la popup
                                    hideBloc('presentationSteps');
                                    hideBloc('descriptionSteps');
                                    showBloc('generalConditions');
                                }
                            }
                        }
                    }                    

                    // eBooking
                    if (typePage=='eBooking') {
                        loadEbooking(cookieMagasin);
                    }

                     // Modele magasin
                     if (typePage=='psd' && window.parent.srcDone) {
                         initDisplayPsd();
                         loadStoreList(getEltFromUrl(cookieMagasin, 'idMagasin'));
                    }

                   // Avis Oxylane
                    if (typePage=='oxylane-comments') {
                        loadAvisOxylane();
                    }

                   // Spec BE : check LAD
                    if (typePage=='be-check-lad') {
                        cladLoadCheck(cookieMagasin);
                    }

                    // Contact your store
                    if (typePage=='ContactYourStore'){
                        cysLoad();
                    }           
}
}
/*############################################################################################## */
/* Retourne l etat du switch (false si probleme lors de la reponse http de chargement des switch)  */
function getSwitchSite(switchName) {                
	if(switchStatusError) {
	        return false;
	}
	else {
                    var oService = getService(_serviceSwitchContextId); 
                    if (oService ==null) {return null;} 
                    var oContent = oService.getContent(); 
                    if (oContent ==null) {return null;}
                    try {
                        return oContent.getSwitch(switchName, SWITCH_KIND_SITE, _siteNumber, _siteNumber, null).isEnabled();
                    }
                    catch (e) {
                        return false;
                    }
             }
}
/*############################################################################################## */
/* Retourne l etat du switch du magasin du cookie (false si probleme lors de la reponse http de chargement des switch)  */
function getSwitchMagasin(switchName) {                
	if(switchStatusError) {
	        return false;
	}
	else {	        
                    var oService = getService(_serviceSwitchContextId); 
                    if (oService ==null) {return null;} 
                    var oContent = oService.getContent(); 
                    if (oContent ==null) {return null;}
                    try {
                        return oContent.getSwitch(switchName, SWITCH_KIND_STORE, idMagasin, idMagasin, null).isEnabled();
                    }
                    catch (e) {
                        return false;
                    }
             }
}

/*############################################################################################## */
/* Execute le service switch et retourne l ID du contexte*/
function _initAndExecuteSwitchService(_theRoot, _theLanguage, _theSiteId, _theCentralId, _theStoreId,_theThirdKind, _theThirdNumber, _theThirdSubNumber,_theThirdKind2, _theThirdNumber2, _theThirdSubNumber2,_theLocalFunc) {

    var oContext = new JoServiceContext(_theRoot,getErepLanguageCode( _language), _theSiteId, _theCentralId, _theStoreId);
    oContext.setSwitches(_theThirdKind,_theThirdNumber,_theThirdSubNumber,_theThirdKind2,_theThirdNumber2,_theThirdSubNumber2);
    var oService = new JoService(oContext);
    oService.execute(_theLocalFunc);
    
    return oService.getId();
}
/*############################################################################################## */
/* valide la reponse du service switch (erreur - vide ou autres)*/
function _validateSwitchServiceReponse(_serviceId) {
    try {
       var oService = getService(_serviceId) ;
       var oContent = oService.getContent();
       if (oContent != null) {
           var switchesList = oContent.getSwitchesList() ;
            if (switchesList != null) {
               if (switchesList.length > 0) return true;
            }
        }
    
        return false
    }
    catch (e) {
        return false;
    }
}

/*############################################################################################## */
/* Liste des actions a executer en fonction de l etat des switch */
function postSwitchActionDktTv() {
            if (loadedSwitches == null || ! loadedSwitches) {
                    window.setTimeout("postSwitchActionDktTv()", 10);
            }
            else {
               if(switchStatusError){
                    // Affichage de la popup d'infos
                    var iframe = createElement_iframe("popup-hp-0", "", "", "", "", null, document.body);
                    iframe.style.display = 'none';
                    openNewWindowScrollEffect(0,window.parent.urlPopupBugErep,'popup-bug-service',null);
                }
                // Test si connexion valide
                cookieMagasin = isConnexionEnable();
                /* Recuperation du prix erep du produit */
                loadModelDktTvConnectOrNot(cookieMagasin,_language);
                /* Chargement des produits affiches sous le player */
                loadModelsDktTv(_client, _siteNumber, _centralNumber, null);
            }
}
