﻿//************************** Initialisation des évènements de la zone tremplin  ********************
function InitTremplin() {
    // Initialisation des évènements de la zone tremplin
    InitTremplinEvents()
        
}
//**************************Animation d'ouverture/fermeture des blocs tremplin***************
function InitTremplinAnimation() {
    //
    $(".tremplintitle > a[href='#']").unbind("click");

    //
    $(".tremplintitle > a[href='#']").click(function () {
        TremplinAnimation($(this));
        return false;
    });
}

function TremplinAnimation(headerBox) {
    var divBox = headerBox.parent().next("div.Accordeon");
    if (divBox.length != 1)
        return;
    if (divBox.is(":hidden")) {

        //Fermer les cartouches ouvertes        
        $("div#TremplinRight").children("div.Accordeon:visible").each(function () {

            var openDivBox = $(this);
            var openHeaderBox = openDivBox.prev("h3").children("a");

            if (openHeaderBox.length == 1) {
                BoxClose(openHeaderBox, openDivBox);
            }
        });

        // ouvrir la cartouche fermée
        BoxOpen(headerBox, divBox);
    }


}


//************************** Initialisation de l'évènement d'inscription ********************
function InitTremplinEvents() {
    // Initialisation de l'évènement d'inscription
    InitBtnSubscribe();
    // Initialisation de l'évènement de changement du magasin
    InitComboSelectShop();
    // Initialisation de l'évènement de Login et ensuite de vote
    InitBtnLoginAndRating();
    // Initialisation de l'évènement de vote
    InitBtnRating();

}
//************************** Initialisation des évènement de tremplin pour les pages Cms ********************
function InitTremplinCmsEvents() {
    // Initialisation de l'évènement de changement du magasin
    InitComboSelectShop();

    //Définir l'animation des blocs  Tremplin
    InitTremplinAnimation();
}
//************************** Initialisation de l'évènement d'inscription ********************
function InitBtnSubscribe() {
    var btnElem = $("div#InscriptionTremplin > img.ButtonInscriptionTremplin");
    // Tester si on est dans le cas où on dispose de bouton d'inscription
    if (btnElem.length == 0)
        return;
    // Affecter l'évènement click sur le bouton d'inscription
    btnElem.click(function () {
        SubscribeToCurrentTremplin();
    });
}
//************************** Initialisation de l'évènement de changement du magasin ********************
function InitComboSelectShop() {
    $("div#HitParadeParMagasin > select").change(function () {
        LoadHitParadeParMagasin($(this).val());
    });
}
//************************** Charger Hit parade par magasin ********************
function LoadHitParadeParMagasin(shopid) {
    //alert('charger Hit parade par magasin, magasinid = ' + shopid);

    var strData = "";
    strData += "ShopId=" + shopid;
    //Appel Ajax pour charger les groupes en fonction de l'Id du magasin
    $.ajax({
        url: "/Ajax/TremplinHitParadeByShop.aspx",
        data: strData,
        cache:false,
        success: LoadHitParadeByShopSucceed,
        error: LoadHitParadeByShopError
    });
}

function LoadHitParadeByShopSucceed(data) {
    $("div#HitParadeParMagasinContent").html(data);
}

function LoadHitParadeByShopError(data) {
    alert('Le chargement de Hit parade par magasin a rencontré un problème!');
}
//************************** Initialisation de l'évènement de Login et ensuite de vote ********************
function InitBtnLoginAndRating() {
        OpenLayerLoginAndRating();
}
//************************** Initialisation de l'évènement de vote ********************
function InitBtnRating() {
        OpenLayerRating(false);
}
//************************** Inscription au tremplin en cours ********************
function SubscribeToCurrentTremplin() {
    //récupérer GroupId
    var groupId = $("input#hid_GroupId").val();

    //Appel JSON demande de rejoindre un groupe
    $.ajax({
        type: "POST",
        url: JsonServices + "/SubscribeToCurrentTremplin",
        data: "{groupId:" + groupId + "}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (data) {
            if (data.d)
                TremplionSubscriptionSuccess(data);
            else
                alert("Erreur lors de l'inscription au tremplin.");
            },
            error: function (xhr, ajaxOptions, thrownError) {
                TremplionSubscriptionError(xhr, ajaxOptions, thrownError); // erreur http xml technique
            }
    });
}
//************************** Inscription au tremplin en cours réussie ********************
function TremplionSubscriptionSuccess(data) {
    alert("Félicitations !\n\nVotre inscription au tremplin en cours s'est correctement effectuée.");
    // Rafraîchir la page
    window.location.reload(true);
}
//************************** Inscription au tremplin a échoué ********************
function TremplionSubscriptionError(xhr, ajaxOptions, thrownError) {
    var err = eval("(" + xhr.responseText + ")");
    alert("Vous ne pouvez pas vous inscrire au tremplin.\n" + err.Message);
}
//************************** Ouverture de FancyBox d'authentification et ensuite de vote  *********************************
function OpenLayerLoginAndRating() {

$("div#LoginVoteTremplin > a#hrefLoginAndVoteTremplin").fancybox({
        'transitionIn': 'elastic',
        'transitionOut': 'elastic',
        'speedIn': 600,
        'speedOut': 200,
        'height': 480,
        'width': 700,
        'padding': 0,
        'autoDimensions': false,
        'scrolling' : false,
        'type': 'iframe'
    });

}
//************************** Ouverture de FancyBox de vote **********************************
function OpenLayerRating(callback) {

    if (callback) {
        $.fancybox({
            'transitionIn': "elastic",
            'transitionOut': "elastic",
            'width': 505,
            'height': 340,
            'autoDimensions': false,
            'type': 'iframe',
            'href': $("div#LoginVoteTremplin > a#hrefLoginAndVoteTremplin").attr("xhref")
        });
    }
    else {
        $("div#VoteTremplin > a#hrefVoteTremplin").fancybox({
            'transitionIn': "elastic",
            'transitionOut': "elastic",
            'width': 502,
            'height': 400,
            'padding': 20,
            'autoDimensions': true,
            'scrolling': false,

            'type': 'iframe'
        });
    }
}



