var requestContext = null;
function contextualizeFrontPage() {
if(contextvalide == 1 ){
    clearTimeout(requestContext);
}
else{
   // DWRContextualizer.getCurrentContext({callback:function(data) {
       var data=getCurrentContext();
	 
        var currentShop = data;
  
        if(currentShop) {
            if ( currentShop.save =='false' || currentShop.save == false ) {
                var defaultlink = "";
                if(prisme!='1090509516'){
                 defaultlink = "<a onclick=\"xt_med('C','1','zone_permanente::prismes::magasins_et_services','N');\" href=\"/mpng2-front/pre?zone=zonecatalogue&amp;idLSPub=1090509516&amp;renderall=on\" accesskey=\"5\"><img name=\"Services\" src=\"/images/main_top_tabs/magasins_et_services.gif\" alt=\"Magasins et services\"  /></a>";
                }else{
                 defaultlink = "<a onclick=\"xt_med('C','1','zone_permanente::prismes::magasins_et_services','N');\" href=\"/mpng2-front/pre?zone=zonecatalogue&amp;idLSPub=1090509516&amp;renderall=on\" accesskey=\"5\"><img name=\"Services\" src=\"/images/main_top_tabs/magasins_et_services_on.gif\" alt=\"Magasins et services\"  /></a>";

                }
                //dwr.util.setValue("divmag", defaultlink , { escapeHtml:false } );
                if(getCookie("anonymous_save")!=null){
                  //DWRContextualizer.getContext("anonymous", {callback:function(data) {
                        var contextShop = {
            'save':getCookie("anonymous_save"),
            'shop':getCookie("anonymous_shop")
                }
                         var shortShopCode = getCookie("anonymous_shop");
                         while (shortShopCode.indexOf('0') == 0)
			 { 
			 shortShopCode = shortShopCode.substring(1);
			 }
//			 alert('contextualizeFrontPage'+shortShopCode);

//if(contextShop.save == 'true' ){  
                        var accbegin = '<a onclick="xt_med(\'C\',\'38\',\'acceder_magasin_arche\',\'N\');" href="javascript:restoreContext();">Acc&eacute;der &agrave; votre magasin de ';
                        var accend = '</a>';
			DWRShopContextualizer.getContextualizedShopByCode( shortShopCode,  function(data) {
                            var contextualizedShop = data;
                            
                            if(document.getElementById('pathbarmag') && contextualizedShop != null){
                            dwr.util.setValue("pathbarmag", accbegin + contextualizedShop.name + accend, { escapeHtml:false } );
                            }

                        });
                        //}
                    //}
                   // });
                }
                    if(getCookie("ci_save")!=null){
                    //DWRContextualizer.getContext("ci",  {callback:function(dataci) {
                        var contextShopCI = {
            'save':getCookie("ci_save"),
            'shop':getCookie("ci_shop")
        }
                        if(contextShopCI) {
                            if(contextShopCI.save != null) {
                                if(contextShopCI.save == 'false'){
                                    var accbegin = '<a onclick="xt_med(\'C\',\'38\',\'acceder_magasin_arche\',\'N\');" href="javascript:accederMagsinCI();">Acc&eacute;der &agrave; votre magasin de ';
                                    var accend = '</a>';
				    var shortShopCode = getCookie("ci_shop");
				    while (shortShopCode.indexOf('0') == 0)
				    { 
				    shortShopCode = shortShopCode.substring(1);
				    }
				    DWRShopContextualizer.getContextualizedShopByCode( shortShopCode, function(data) {
                                        var contextualizedShop = data;
                                        if(document.getElementById('pathbarmag')){
                                        dwr.util.setValue("pathbarmag", accbegin + contextualizedShop.name + accend, { escapeHtml:false } );
                                        }
                                    });
                                }
                            }
                        }
                    //}
                    //});
                    }
                  contextvalide=1;
            } else {
	            var shortShopCode = currentShop.shop;
		    while (shortShopCode.indexOf('0') == 0)
		      { 
		      shortShopCode = shortShopCode.substring(1);
		      }
		    DWRShopContextualizer.getContextualizedShopEIIDByCode(shortShopCode, {callback:function(eiid) {
                    var shopeiid = eiid;
                    var link = "";
                var defaultlink = "";
                if(prisme!='1090509516'){
                 link = "<a onclick=\"xt_med('C','1','zone_permanente::prismes::magasins_et_services','N');\" href=\"/mpng2-front/pre?zone=zonecatalogue&amp;idEILSPub=" + shopeiid + "&amp;renderall=on\" accesskey=\"5\"><img name=\"Services\" src=\"/images/main_top_tabs/mon_magasin.gif\" alt=\"Magasins et services\"  /></a>";
                }else{
                 link = "<a onclick=\"xt_med('C','1','zone_permanente::prismes::magasins_et_services','N');\" href=\"/mpng2-front/pre?zone=zonecatalogue&amp;idEILSPub=" + shopeiid + "&amp;renderall=on\" accesskey=\"5\"><img name=\"Services\" src=\"/images/main_top_tabs/mon_magasin_on.gif\" alt=\"Magasins et services\"  /></a>";

                }
                    //dwr.util.setValue("divmag", link , { escapeHtml:false } );
                    
                    //affichage de l'onglet 'trouver un magasin'
                    //if(document.getElementById('trouvermag')){
                    //    obj = document.getElementById("trouvermag");
                    //    obj.style.display = 'inline';
                    //}
                }
                });
                if(currentShop.shop=='139'){
                  getImgvenduenline();
                }

                if(shortShopCode=='14' || shortShopCode=='13' || shortShopCode=='60'){
                    var shopName = "";
                    switch(shortShopCode){
                        case "13":
                            shopName="montigny";
                            break;
                        case "14":
                            shopName="valenciennes";
                            break;
                        case "60":
                            shopName="caen";
                            break;
                    }
                    if(document.getElementById("boutiqueRetraitExpress")){
			var lienMag = "<a onclick='xt_med(\"C\",\"1\",\"navigation_permanente::header::mon_magasin::retrait_express\",\"N\");' href='http://retrait-"+shopName+".leroymerlin.fr' style='color:#FF9900'>&quot;retrait express&quot;</a>";
                        document.getElementById("boutiqueRetraitExpress").style.display="block";
			document.getElementById("boutiqueRetraitExpress").innerHTML = lienMag;
                        //if(document.getElementById("liretrait")){
                        //    document.getElementById("liretrait").style.display="block";
                        //}
                    }
                }
                //var img = '/images/pickup/leroymerlin_'+currentShop.shop +'.gif';           
                //if(document.getElementById('logoimgarche')){
                //    document.getElementById('logoimgarche').setAttribute("src", img);
                //    document.getElementById('mouseoutlogo').innerHTML=img;
                //}
                //if(document.all){
                //    document.getElementById('logoimg').setAttribute('className','logopickup');
                //}else{
                //    document.getElementById('logoimg').setAttribute('class','logopickup');
                //}
                displayContextualizePart();
                contextvalide=1;
                   
                if(document.getElementById('pathbarmag') && document.getElementById('pathbarmag').innerHTML == '') {
                    //affichage du lien uniquement si on n'est pas logge au CI, cf logpersonalaccount.tpl                    � � 
		  
                    var quitterbegin = '<a onclick="xt_med(\'C\',\'1\',\'navigation_permanente::header::mon_magasin::quitter_magasin\',\'N\');" href="javascript:removeContext();">Quitter mon magasin';
                    var quitterend = '</a>';
                    DWRShopContextualizer.getContextualizedShopByCode( shortShopCode, {callback:function(data) {
                        var contextualizedShop = data;var link = "";
                        if(document.getElementById('pathbarmag')){
                            document.getElementById('pathbarmag').innerHTML=quitterbegin + contextualizedShop.name + quitterend;
			  if(logg == 'false'){ 
   			     //document.getElementById('pathbarmag').innerHTML=quitterbegin;
				}
			    DWRShopContextualizer.getContextualizedShopEIIDByCode(shortShopCode, {callback:function(eiid) {
                                var shopeiid = eiid;
                        alert("test");        
                                var defaultlink = "";
				var nomMagAff = '';
				if(contextualizedShop.name.length > 20){
				    nomMagAff = contextualizedShop.name.substr(0, 20) + "...";
				} else {
				    nomMagAff = contextualizedShop.name;
				}
                                if(prisme!='1090509516'){
                                      link = "<a title=\"" + contextualizedShop.name + "\"" + "style=\"color:#FF9900\" onclick=\"xt_med('C','1','header::mon_magasin::accueil_mag','N');\" href=\"/mpng2-front/pre?zone=zonecatalogue&amp;idEILSPub=" + shopeiid + "&amp;renderall=on\" accesskey=\"5\">" + nomMagAff + "</a>";
                                }else{
                                       link = "<a title=\"" + contextualizedShop.name + "\"" + "style=\"color:#FF9900\" onclick=\"xt_med('C','1','header::mon_magasin::accueil_mag','N');\" href=\"/mpng2-front/pre?zone=zonecatalogue&amp;idEILSPub=" + shopeiid + "&amp;renderall=on\" accesskey=\"5\">" + nomMagAff + "</a>";
                                }
				
			    document.getElementById('nomMagasin').innerHTML=link;
			    //document.getElementById('nomMagasin').style.color="#FF9900";
			    document.getElementById('trouverMonMagasin').style.display="none";
                            }
                           });
                        }
                        dwr.util.setValue("pathbarmag", quitterbegin + contextualizedShop.name + quitterend, { escapeHtml:false } ); 
			dwr.util.setValue("nomMagasin", contextualizedShop.name, { escapeHtml:false } );  
			document.getElementById('nomMagasin').style.display="block";
			//if(logg == 'false'){
			    dwr.util.setValue("pathbarmag", quitterbegin, { escapeHtml:false } );    	
                        //}
                        alert('context ok');
                    }, 
                    timeout:5000,       
                    errorHandler:function(msg) {
                           alert("Une erreur s'est produite : "+ msg);
                    }
                    });
                }  
            } 
        } else {
                var defaultlink = "";
                if(prisme!='1090509516'){
                 defaultlink = "<a onclick=\"xt_med('C','1','zone_permanente::prismes::magasins_et_services','N');\" href=\"/mpng2-front/pre?zone=zonecatalogue&amp;idLSPub=1090509516&amp;renderall=on\" accesskey=\"5\"><img name=\"Services\" src=\"/images/main_top_tabs/magasins_et_services.gif\" alt=\"Magasins et services\"  /></a>";
                }else{
                 defaultlink = "<a onclick=\"xt_med('C','1','zone_permanente::prismes::magasins_et_services','N');\" href=\"/mpng2-front/pre?zone=zonecatalogue&amp;idLSPub=1090509516&amp;renderall=on\" accesskey=\"5\"><img name=\"Services\" src=\"/images/main_top_tabs/magasins_et_services_on.gif\" alt=\"Magasins et services\"  /></a>";

                }
             //dwr.util.setValue("divmag", defaultlink , { escapeHtml:false } );
             contextvalide=1;
        }
        
   /* },
    timeout:5000,       
    errorHandler:function(msg) {
        //   alert("Une erreur s'est produite : "+ msg);
    }
    
    });*/
    }
}
      
function displayContextualizePart() {

    if(document.getElementById('bloccontextualize1')){
        obj = document.getElementById("bloccontextualize1");
        obj.style.display = 'block';
    }
    if(document.getElementById('bloccontextualize2')){
        obj = document.getElementById("bloccontextualize2");
        obj.style.display = 'block';
    }
    if(document.getElementById('bloccontextualize3')){
        obj = document.getElementById("bloccontextualize3");
        obj.style.display = 'block';
    }
}  
function getImgvenduenline() {
    tags = document.getElementsByTagName('img');
  //  alert("tags.length"+tags.length)
    for (i = 0; i < tags.length; i++) {
        if (tags[i].id == 'imagevenduenline') {
            tags[i].style.display="none";

        }
    }

    tagsLabel = document.getElementsByTagName('label');
    for (i = 0; i < tagsLabel.length; i++) {
        if (tagsLabel[i].id == 'labelvenduenline') {
            tagsLabel[i].style.display="none";

        }
    }
}


