﻿
/******************************
* POPUP AJAX DETAIL PRODUIT
*******************************/
var xhr_object = null;

if (window.XMLHttpRequest) {
    // Firefox & Others
    xhr_object = new XMLHttpRequest();
}
else if (window.ActiveXObject) {
    // Internet Explorer 
    try {
        xhr_object = new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch (e) {
        try {
            xhr_object = new ActiveXObject("Microsoft.XMLHTTP");
        }
        catch (e) {
            alert("Votre navigateur ne supporte pas les objets XMLHTTPRequest...");
        }
    }
}
else {
    // XMLHttpRequest non supporté par le navigateur 
    alert("Votre navigateur ne supporte pas les objets XMLHTTPRequest...");
}


/*************************************
* Fonctions pour les popup (GENERAL)
**************************************/

//if (document.documentElement && document.documentElement.clientWidth) { window.TsDocRef = document.documentElement; } else { window.TsDocRef = document.body; }
//if ((window.ie || window.ie6) && !window.ie7) { window.IElt7 = true; } else { window.IElt7 = false; }

/************************
*    POPUP PANIER
*************************/

function openPopupColis(idColis) {


    // Filtre
    var flt = jQuery('#ctl00_filtreColis');
    if (flt) {
        flt.css('display', 'block');

        flt.click(function (event) {
            FermerPopup();
            fermerPopupPanier();
        });
    }

    var elt = $('#popcolis');

    var largeur = $(window).width();
    var hauteur = $(window).height();

    var top = Math.round((hauteur - elt.height()) / 2) + $(window).scrollTop();
    var left = Math.round((largeur - elt.width()) / 2) + $(window).scrollLeft();
    if (top < $(window).scrollTop()) top = $(window).scrollTop();

    elt.css({ 'top': top + 'px', 'left': left + 'px', 'display': 'block' });
}




function openPopupPanier(idColis) {

    // specialité, on recupere la position du bouton
    var btn = $("#add" + idColis);
    var pos = btn.position();   //28x133
     
    var top = pos.top + 32 - 176;
    var left = pos.left + 133 - 283;

    $('#popup_panier').css({ 'top': top + 'px', 'left': left + 'px', 'display': 'block' });

}


function fermerPopupPanier(scroll) {
    var elt = $('#popup_panier');
    if (elt) {
        elt.css({ 'display': 'none' });
    }

    var elt = $('#popcolis');
    if (elt) {
        elt.css({ 'display': 'none' });
    }

    var flt = jQuery('#ctl00_filtreColis');
    if (flt) {
        flt.css('display', 'none');
        flt.unbind('click');
    }
}

function eventClickPos(e) {
    var posx = 0;
    var posy = 0;
    if (!e) var e = window.event;
    if (e.pageX || e.pageY) {
        posx = e.pageX;
        posy = e.pageY;
    }
    else if (e.clientX || e.clientY) {
        posx = e.clientX + document.body.scrollLeft
			+ document.documentElement.scrollLeft;
        posy = e.clientY + document.body.scrollTop
			+ document.documentElement.scrollTop;
    }
    // posx and posy contain the mouse position relative to the document
    // Do something with this information
    return { x: posx, y: posy };
}

function $viewhide(div1, div2) {
    $('#'+div1).css({ 'visibility': 'visible', 'display': 'block' });
    $('#'+div2).css({ 'visibility': 'hidden', 'display': 'none' });
}


/*
* recupere uniquement le contenu html voulu sans les balise <html> <body> ....
*/

function SetPopupComplete(tsContent)
{
    var div = $('#fiche_pdt4');
    
    if (!div) { /*alert("ya pa popup");*/return; }

    var tsStart = '<!-- Start POPUP -->';
    var tsEnd = '<!-- End POPUP -->';
    if (tsContent.indexOf(tsStart) > -1 && tsContent.indexOf(tsEnd) > -1) {
        div.html( tsContent.substring(tsContent.indexOf(tsStart), tsContent.indexOf(tsEnd) + (tsEnd.length)) );
    } else {
        div.html( tsContent );
    }

    //TsEvalScripts($('fiche_pdt4').innerHTML);

    //var tsElementA = $ES('.close', 'fiche_pdt4');
    //for (var i = 0, x = tsElementA.length; i < x; i++) { tsElementA[i].setStyle('visibility', 'visible'); }
    setTimeout('SetPopup()', 20);

    //test
    //elt = $('fp_page2');
    //if (elt)
        //elt.setStyles({ 'visibility': 'hidden' });
}

function TsEvalScripts(evalResponse) {
    var script, scripts;
    scripts = [];

    var regexp = /<script[^>]*>([\s\S]*?)<\/script>/gi;

    while ((script = regexp.exec(evalResponse))) { scripts.push(script[1]) };

    scripts = scripts.join('\n');

    if (scripts) (window.execScript) ? window.execScript(scripts) : window.setTimeout(scripts, 0);
}

/*
* Positionne le popup
*/

function SetPopup() {
    var elt = $('#popup');
    if (!elt) { return; }

    //SetDimension(elt);
    SetPosition(elt);
}

function SetDimension(elt) {

/*
    var child = elet.firstChild.firstChild;

    if (child && child.nodeValue == 'Newsletter') {
        element.style.width = 379 + 'px';
        element.style.left = 40 + '%';
    } else {
        element.style.width = 730 + 'px';
    }
    element.style.height = element.clientHeight + 'px';
    */
}

function SetPosition(elt)
{
    //$(TsElement).setStyles({ 'visibility': 'hidden', 'display': 'block' });
    elt.css({ 'visibility': 'hidden', 'display': 'block' });

    var largeur = $(window).width();
    var hauteur = $(window).height();

    var top = Math.round((hauteur - elt.height()) / 2) + $(window).scrollTop();
    var left = Math.round( (largeur - elt.width()) / 2 ) + $(window).scrollLeft();
    if (top < $(window).scrollTop()) top = $(window).scrollTop();

    elt.css({ 'top': top + 'px', 'left': left + 'px', 'visibility': 'visible' });

    var elt2 = $('#fiche_pdt4');
    elt2.css( 'visibility', 'visible');

}



/* Cacher les popup */

function FermerPopup() {

	elt1 = jQuery('#fiche_pdt3');
    if (elt1) {
        elt1.html("");
        elt1.css('display','none');
    }

    elt2 = jQuery('#fiche_pdt4');
    if (elt2) {
        elt2.html("");
        elt2.css('display', 'none');
    }

    if (document.getElementById('ctl00_navNewsletter_scriptNewsletter')) {
        document.getElementById('ctl00_navNewsletter_scriptNewsletter').innerHTML = "";
    }

    var flt = jQuery('#ctl00_filtreColis');
    flt.css('display', 'none');
    flt.unbind('click');

    eval('SupprimerEvenementsDuClavier()');
    eval('AfficherColisSuivantPrecedentAvecClavier()');
}

function PopIngredients(id, w) {
    //if ($('#poping').css('display') == 'block') { PopIngClose(); return; }
    
    $.ajax({
        url: '../Offres/Ingredients/ingredients-' + id + '.html',
        method: 'GET',
        success: function (html) {
            var pop = $("#poping");

            $("#poping .contenu").html(html);
            if (w < 290 || w == undefined) w = 290;
            pop.width(w);

            var btn = $("#ing" + id);
            if (btn != null) {
                var pos = btn.position();
                pop.css("top", (pos.top + 18) + "px").css("left", pos.left + 70 - w + "px").css("cursor", "pointer");
            }

            pop.click(function (event) {
                PopIngClose();
            });

            pop.css('display', 'block');


        },
        error: ajaxError
    });


}

function PopIngClose() {

    var elt1 = $('#poping');
    if (elt1) {
        //elt1.html("");
        elt1.css('display', 'none');
    }
}



var ajaxError = function (obj, status, error) {
    var msg = "erreur AJAX";

    msg += ' (' + obj.status + ')';

    if (status !== undefined)
        msg += ' ' + status;

    if (error !== undefined)
        msg += ' ' + error;
    alert(msg);
};




/************************
*    POPUP NEWSLETTER 
*************************/

function openPopupNewsletter() {
    html = "<div id=\"popnews\" class=\"popup2\">";
    html += " <div class=\"ct_ident-head\"></div>";
    html += " <div class=\"asp1\"><h1>Votre inscription est validée</h1></div>";
    html += " <div class=\"asp2\">";
    html += "  <a onclick=\"FermerPopup()\"><img src=\"/Images/new/btn-fermer.png\" class=\"aspbtn\"></a>";
    html += " </div>";
    html += "<a class=\"newclose\" onclick=\"FermerPopup()\"></a>";
    html += " <div class=\"ct_ident-foot2\"></div>";
    html += "</div>";

    SetPopupComplete(html);
    
    document.getElementById('ctl00_filtreColis').style.display = "block";
    document.getElementById('fiche_pdt4').style.display = "block";
}

function openPopupNewsletterFaux(email) {
    html  = "<div id=\"popnews\" class=\"popup2\">";
    html += " <div class=\"ct_ident-head\"></div>";
    html += " <div class=\"asp1\"><h1>Erreur</h1></div>";
    html += " <div class=\"asp2\">";
    html += "  <p class=\"lh18\">'<strong><em>" + email + "</em></strong>' n'est pas un email valide.</p>"
    html += "  <a onclick=\"FermerPopup()\"><img src=\"/Images/new/btn-fermer.png\" class=\"aspbtn\"></a>";
    html += " </div>";
    html += "<a class=\"newclose\" onclick=\"FermerPopup()\"></a>";
    html += " <div class=\"ct_ident-foot2\"></div>";
    html += "</div>";

    SetPopupComplete(html);
    
    document.getElementById('ctl00_filtreColis').style.display = "block";
    document.getElementById('fiche_pdt4').style.display = "block";
}


/* ==== popup panier kdo ==== */

function CacherBulCadeau() { var bulkdo = $("p#bulkdo"); if (bulkdo.length > 0) bulkdo.remove(); }

function AfficherBulCadeau(id, mp, mr) {

    var txt = 'Vous avez ' + mp + ' &euro; de sp&eacute;cialit&eacute;s<br/>';
    txt += 'dans votre panier.<br /><br />';
    txt += '<span class=\'rosm\'>Il vous manque ' + mr + ' &euro; de<br />sp&eacute;cialit&eacute;s pour ce cadeau.</span>';

    CacherBulCadeau();

    $('body').append('<p id="bulkdo">' + txt + '</p>');

    var btn = $("#bul" + id);
    if (btn != null) {
        var pos = btn.position();
        var apos = $("#cadeau").position();	//en relative donc on ajoute position
        var top = Math.round(apos.top + pos.top - 100);
        var left = Math.round(apos.left + pos.left - 12);
		
		var elt = $('p#bulkdo');
        //elt.css("top", Math.round(pos.top - 100) + "px").css("left", Math.round(pos.left - 12) + "px").css("cursor", "pointer");
        elt.css("top", top + "px").css("left", left + "px").css("cursor", "pointer");
        //elt.css('display', 'block');
        elt.slideDown('fast');
    }



    $("p#bulkdo").click(function (event) {
        $("p#bulkdo").remove();
    });



}
