/*##############################################################################################
############ JS DES FONCTIONS DE GESTION D'AFFICHAGE DE LA PAGE DE CONNEXION AU MAGASIN ######### 
/*############################################################################################## */

// Variable GLOBALE
var docRef;
var afficheFiltre = false;
var X_navigateur = navigator.appName.toLowerCase();
var X_Platform   = navigator.platform.toLowerCase();
var X_version    = navigator.appVersion.toLowerCase();

// Version de Internet Explorer
if(X_navigateur == 'microsoft internet explorer'){
	X_version_deb = X_version.indexOf('(') + 1;
	X_version_fin = X_version.indexOf(')');
	X_version     = X_version.substring(X_version_deb, X_version_fin);
	XtabTmp       = X_version.split(';');
	for(i = 0; i < XtabTmp.length; i++){
		if(XtabTmp[i].indexOf('msie') > -1){
			 X_version = parseInt(XtabTmp[i].replace('msie', ''));
			 break;
		}
	}
}

/*############################################################################################## */
// Document en cours
function monDocument(){
	if(docRef == null){
		if(parent.document.documentElement && parent.document.documentElement.clientWidth){
			docRef = parent.document.documentElement;
		}else{
			docRef = document.body;
		}
	}
}
/*############################################################################################## */
function monParent(){
   if (parent) { // It is an iframe and there is a parent window
        if (parent.document.documentElement && parent.document.documentElement.clientHeight) {
            return parent.document.documentElement;
        } else {
            return parent.document.body;
        }
   } else { 
        if(!docRef) {
        	monDocument();
        }
        return docRef;
   }
}
function tailleFiltre(X_block){
    if(afficheFiltre){
         X_largeur = docRef.scrollWidth;
         docRef.clientHeight > docRef.scrollHeight ? X_hauteur = docRef.clientHeight : X_hauteur = docRef.scrollHeight
         X_block.style.width = X_largeur + 'px';
         X_block.style.height = X_hauteur + 'px';
     }
}
/*############################################################################################## */
// Affiche le filtre d'opacite
function show_filter(objName){
    var X_block = parent.document.getElementById(objName);

    if(X_block){
        // Gestion du filtre au onresize
        addListener(window.parent,'resize',function(){tailleFiltre(X_block)});
        X_block.style.display = 'block';
        if(!docRef){monDocument();}
        afficheFiltre = true;
        tailleFiltre(X_block);

        // Affiche les SELECT si < IE7
        if(X_navigateur == 'microsoft internet explorer' && X_version < 7){              
            gestion_select('hidden');
        } 	
        //Change opacite
        changeOpac(70, objName);
        X_block.style.visibility = 'visible';
    }
}
/*############################################################################################## */
//Change opacite
function changeOpac(opacity, id){
	var object = parent.document.getElementById(id);
	if(!object){return false;}
	object = object.style;
	object.opacity = (opacity / 100);
	object.MozOpacity = (opacity / 100);
	object.KhtmlOpacity = (opacity / 100);
	object.filter = "alpha(opacity=" + opacity + ")";
}
/*############################################################################################## */
// Cache le filtre d'opacite
function hide_filter(objName){
    var X_block = parent.document.getElementById(objName);
    if(X_block){
		// Affiche les SELECT si < IE7
        if(X_navigateur == 'microsoft internet explorer' && X_version < 7){
			gestion_select('visible');
		}
        X_block.style.display = 'none';
        afficheFiltre = false;
    }
}
/*############################################################################################## */
// Gere l affichage des popup/Iframe et les evenements attaches a celles-ci
function show_popup(objName, showPopup, paramToPopup){
    var X_block = parent.document.getElementById(objName);
    
    if(X_block) {
        var filter = parent.document.getElementById('filter');
        // Position de la popup au onresize ou onscroll
        addListener(window.parent,'resize',function(){window.parent.positionPopup(objName)});
    
        if(objName != 'popup-product-not-exist'){
            // Fermeture de la popup si l utilisateur appuie sur la touche echap
            // Iframes pas encore chargees
            addListener(X_block,'load', function(){
                var docIframe = typeof X_block.contentDocument!='undefined' ? X_block.contentDocument : X_block.contentWindow.document;
                X_block.contentWindow.focus();
                window.parent.addListener(docIframe,'keydown',function(e){if(e.keyCode == '27') window.parent.hide_all(objName)});
            });
            // Iframes deja chargees ou popup
            if(typeof X_block.contentWindow != "undefined") X_block.contentWindow.focus();
            addListener(document,'keydown',function(e){if(e.keyCode == '27') window.parent.hide_all(objName)});
            if(parent.document != document) addListener(parent.document,'keydown',function(e){if(e.keyCode == '27') window.parent.hide_all(objName)});
        
            // Fermeture de la popup si il y a eu un click en dehors de celle-ci
            addListener(filter,'click',function(){if(X_block.style.display == 'block') window.parent.hide_all(objName)});
        }
        
        if (objName=='filter_frame') X_block.src=urlConnexionMagasin;
        if (objName=='comparatifSelection') X_block.src=urlGdbc;

        if (objName=='popup-commentcamarche'){
            var trueUrlEbookingSteps = urlEbookingSteps;
            if (paramToPopup)  trueUrlEbookingSteps = urlEbookingSteps + paramToPopup;
            X_block.src=trueUrlEbookingSteps;
        }
        
        // On affiche l iframe une fois qu elle est chargee. A appliquer sur celles dont la source est definie dans cette fonction
        if(objName == 'filter_frame' || objName == 'comparatifSelection' || objName == 'popup-commentcamarche') addListener(X_block,'load', function(){displayX_block(X_block,showPopup)});
        else displayX_block(X_block,showPopup);
    }
    else return false;
}
/*############################################################################################## */
// Affiche la popup/iframe
function displayX_block(X_block,showPopup){
    X_block.style.visibility = 'hidden';
    X_block.style.display = 'block';
    positionPopup(X_block.id);
    if(typeof showPopup == 'undefined' || showPopup) X_block.style.visibility = 'visible';
}
/*############################################################################################## */
// Cache le Filtre et popup/iframe
function hide_all(TsPopup){
	hide_popup(TsPopup);
	hide_filter('filter');
}
/*############################################################################################## */
// Cache la popup/Iframe
function hide_popup(objName){   
    var X_block = parent.document.getElementById(objName);
    if(X_block){
        X_block.style.display = 'none';       
    } else {
    	return false;
    }
}
/*############################################################################################## */
// Calcule la position de la popup/iframe
function positionPopup(X_obj){
    var X_block = parent.document.getElementById(X_obj);
    if( !X_block ){return false;}
    if(!docRef){monDocument();}
    //alert(docRef);
    //alert(docRef.clientHeight + "  " + X_block.offsetHeight);
    var X_blockTop = (parseInt(docRef.clientHeight) - parseInt(X_block.offsetHeight)) /2;    
    if (parseInt(X_blockTop)<0) {
        X_blockTop = 20;
    }

    X_block.style.top = parseInt(docRef.scrollTop) + parseInt(X_blockTop) + 'px';
    
    var X_blockLeft = (parseInt(docRef.clientWidth) - parseInt(X_block.offsetWidth)) /2;
    if (parseInt(X_blockLeft)<0) {
        X_blockLeft = 20;
    }
    X_block.style.left = parseInt(docRef.scrollLeft) + parseInt(X_blockLeft) + 'px';
}

/*############################################################################################## */
// Affiche ou Cache les SELECT de la page
function gestion_select(X_option){
    var X_balises = parent.document.getElementsByTagName('select');
    for(var i=0; i<X_balises.length; i++){
       X_balises[i].style.visibility = X_option;
    }
    if (window.frames['basketContent']){
        var X_balisesFrame=window.frames['basketContent'].document.getElementsByTagName('select');
         for(var i=0; i<X_balisesFrame.length; i++){
           X_balisesFrame[i].style.visibility = X_option;
        }
     }
    // Cela cache egalement les select des popups local-price-content et stocks multi-magasins
    // Il faut donc rafficher ces select
    if (document.getElementById("popup-local-price")) {    
      X_balises = document.getElementById("popup-local-price").getElementsByTagName('select');
        for(var i=0; i<X_balises.length; i++){
            X_balises[i].style.visibility = 'visible';
        }
    }
    if (document.getElementById("popup-product-stores-disponibility")) {    
      X_balises = document.getElementById("popup-product-stores-disponibility").getElementsByTagName('select');
        for(var i=0; i<X_balises.length; i++){
            X_balises[i].style.visibility = 'visible';
        }
    }
}
/*############################################################################################## */
// Affiche ou Cache le bloc reconnaitre du formulaire de connexion magasin en fonction de la case cochee ou non
function gestion_checkbox() {    
    if(isCheck('checkb')) {
        showBloc('rememberme');
        checkOrUncheck('checkb', true);
    }
    else {
        checkOrUncheck('checkb', false);
        hideBloc('rememberme');
    }
}

/*############################################################################################## */
// Ajoute une action lors d'un evenement javascript
function addListener(elt,type, expression) {
    if (window.addEventListener) {
        elt.addEventListener(type, expression,false);
    }
    else if (window.attachEvent) {
        elt.attachEvent('on' + type, expression);
    }
}

/*############################################################################################## */
// Affiche une popup standard
function show_popup_std(sDocUrl, iDocWidth, iDocHeight) {
    if (iDocWidth == "") iDocWidth = 640;
    if (iDocHeight == "") iDocHeight = 480;
    
    var oDiv = document.getElementById("popup-std");
    var oFrame = document.getElementById("popup-std-iframe");
    if (!oDiv || !oFrame || sDocUrl == "") return false;

    // Affichage filtre
    show_filter('filter');
    
    // Fixe taille de la div
    oDiv.style.width = iDocWidth + 20 + "px";
    oDiv.style.height = iDocHeight + 35 + "px";
    
    // Creation de l iFrame
    createElement_iframe("iframe-std", sDocUrl, "", iDocWidth + "px", iDocHeight + "px", false, oFrame);

    //Gestion positionnement
    oDiv.style.visibility = 'hidden';
    oDiv.style.display = 'block';
    positionPopup('popup-std');
    addListener(window.parent,'resize',function(){positionPopup("popup-std")});

    // Affichage
    oDiv.style.visibility = 'visible';
}

/*############################################################################################## */
// Ferme la popup standard
function close_popup_std(){

    var oDiv = document.getElementById("popup-std");
    var oFrame = document.getElementById("popup-std-iframe");
    
    if (!oDiv || !oFrame) return false;
    
    // Masque la popup + supprime iFrame
    if (oFrame.hasChildNodes()) while (oFrame.childNodes.length > 0) oFrame.removeChild(oFrame.firstChild);
    oDiv.style.display = 'none';
    hide_filter('filter');
}
