﻿$(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="";
    $("#email_ok").click(function() {
        var result = ($("#popNewsletter #email").val() != "");
        $("#noEmail").css({'display': 'none'});
        $("#msgSend").css({'display': 'none'});
	//alert("result : " + result);
        if (result){
	   //alert("on envoi");
           _mail = $("#popNewsletter #email").val();
           _country = $("#popNewsletter #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();
                        $("#noEmail").css({'display': 'none'});
                        $("#msgSend").css({'display': 'block'});
                    }
                    else{
                        //email incorrect
                        $("#noEmail").css({'display': 'block'});
                        $("#msgSend").css({'display': 'none'});
                    }
                }
            });
        }
		else
		{
			$("#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'});
    $("#msgSend").css({'display': 'none'});
    $("#noEmail").css({'display': 'none'});
    $("#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() {
    return [$(window).width(), $(window).height(), $(document).scrollLeft(), $(document).scrollTop() ];  
}