﻿try {
    $(document).ready(function() {
        var i = 0;
        var tmp = "#ctl00__contenu_ctl";
        
        //hide les div au chargement
        while (i<10 && $(tmp + "0" + i + "__contenuTexteEntier").length > 0 ) {
            $(tmp + "0" + i + "__contenuTexteEntier").hide();
            i++;
        }
        i=0;
        while (i<10 && $(tmp + "0" + i + "__btnReplier").length > 0 ) {
            $(tmp + "0" + i + "__btnReplier").hide();
            i++;
        }
        //on poursuit apres 10
        if (i==10) {
            while ($(tmp + i + "__contenuTexteEntier").length > 0 ) {
                $(tmp + i + "__contenuTexteEntier").hide();
                i++;
            }
            i=10;
            while ($(tmp + i + "__btnReplier").length > 0 ) {
                $(tmp + i + "__btnReplier").hide();
                i++;
            }
        }
        
        
       //////////////////// VALIDATION LA POP UP DE NEWSLETTER ////////////////////////////////  
        var path="";
        $("#news_email_ok").click(function() {
            var result = ($("#news_email").val() != "");
            $("#news_noEmail").css({'display': 'none'});
            $("#news_msgSend").css({'display': 'none'});
	    //alert("result : " + result);
            if (result){
	       //alert("on envoi");
               _mail = $("#news_email").val();
               _country = $("#news_country").val();
                $.ajax({
                    type : 'POST',
                    url  : path+'subscribe.aspx',
                    data : {mail:_mail,country:_country},
                    success: function(result) {
                        //alert(result);
                        if(result=="send"){
                            //envoi effectué
                            //maskNewsletter();
                            $("#news_noEmail").css({'display': 'none'});
                            $("#news_msgSend").css({'display': 'block'});
                        }
                        else{
                            //email incorrect
                            $("#news_noEmail").css({'display': 'block'});
                            $("#news_msgSend").css({'display': 'none'});
                        }
                    }
                });
            }
		    else
		    {
			    $("#news_noEmail").css({'display': 'block'});
		    }
        });
        
    });
    function showNews(tmp){
        $("#" + tmp + "__contenuTexte").slideUp(200);
        $("#" + tmp + "__btnLire").slideUp(200, function(){
            $("#" + tmp + "__contenuTexteEntier").slideDown(300);
            $("#" + tmp + "__btnReplier").slideDown(300);
        });
    }
    function hideNews(tmp){
        $("#" + tmp + "__contenuTexteEntier").slideUp(200);
        $("#" + tmp + "__btnReplier").slideUp(200, function(){
            $("#" + tmp + "__contenuTexte").slideDown(300);
            $("#" + tmp + "__btnLire").slideDown(300);
        });
    }



    var loadingTimer,loadingFrame=1;

    function popupNewsletter(){
        showLoading();
        var pos = getViewport();
        $("#popNewsletter").css({'display': 'block'});
        $("#news_msgSend").css({'display': 'none'});
        $("#news_noEmail").css({'display': 'none'});
        $("#popNewsletter #popNewsletter_content").css({'left': ((pos[0] - 464) / 2 + pos[2]), 'top': ((pos[1] - 314) / 2 + pos[3])});
        return false;
    }

    function maskNewsletter(){
         $("#fancy_loading").hide();
         $("#popNewsletter").css({'display': 'none'});
         return false;
    }

    //////////////////// LOADING ////////////////////////////////
    function animateLoading() {
        if (!$("#fancy_loading").is(':visible')){
            clearInterval(loadingTimer);
            $("#fancy_overlay").hide();
            return;
        }

        $("#fancy_loading > div").css('top', (loadingFrame * -40) + 'px');

        loadingFrame = (loadingFrame + 1) % 12;
    };    
    function showLoading() {
        $("#fancy_overlay").show();
        clearInterval(loadingTimer);

        var pos = getViewport();

        $("#fancy_loading").css({'left': ((pos[0] - 40) / 2 + pos[2]), 'top': ((pos[1] - 40) / 2 + pos[3])}).show();
        loadingTimer = setInterval(animateLoading, 66);
    };
    function getViewport() {
        /*Anciennement :
        return [$(window).width(), $(window).height(), $(document).scrollLeft(), $(document).scrollTop() ];  
        (changé car le href=# renvoie systématiquement vers le haut
        */
        return [$(window).width(), $(window).height(), 0, 0 ];  
    }
} catch(e){
    console.log (e.message);
}
