﻿var currentTop = 265;
var currentWidth = 560;
var currentHeight = 400;
var CurrentRelMenu = "";
var Showable = true;
var Shown = false;
var TryToShow = false;
var AnimateSpeed = 1500;
var MenuClickable = true;

$(document).ready(function () {

    var width = ($(window).width() - 1000) / 2;

    InitializeSubMenu();


    //Positionnement initial du fond
    $(".Background").css("background-position", width - 3000 + "px 0px");

    //    $(document.documentElement).keyup(function (event) {
    //        if (event.keyCode == 37) {
    //            $("#previous").click();
    //        } else if (event.keyCode == 39) {
    //            $("#next").click();
    //        }
    //    });

    //Gestion du click dans le menu principal
    //Action : On cache le sous-menu précédent et le contenu, on slide puis on affiche le nouveau sous menu et le contenu
    $(".MenuItem").click(
        function () {

            if (MenuClickable) {
                MenuClickable = false;
                MenuItemClick($(this));
            }

            return false;
        }
    );

    $(".SubMenuToMenuItem").click(
        function () {

            var elem = $(this);

            $(".Menu1 a[rel='" + elem.attr("rel") + "']").eq(1).click();
        }
    );

    //Définition des tailles initiales
    SetSize();

    //Gestion du redimensionnement du navigateur pour ajouter les caches
    $(window).resize(function () {
        SetSize();
    });

    //Gestion du click sur les liens su footer
    $(".FooterLinkPopup").click(function () {

        //on cache le contenu actuel pour laisser place à la popup
        HidePopup();

        var elem = $(this);

        var position = elem.offset();
        var size = {};
        size.width = elem[0].offsetWidth;
        size.height = elem[0].offsetHeight;

        $("#FooterPopup").css("top", position.top + "px");
        $("#FooterPopup").css("left", position.left + "px");
        $("#FooterPopup").css("height", size.height + "px");
        $("#FooterPopup").css("width", size.width + "px");

        $.ajax({
            type: "GET",
            url: "/Home/Load",
            data: "View=" + elem.attr("rel") + "&lang=FR",
            success: function (msg) {
                $(".FooterPopupContent").html(msg);

                var width = ($(window).width() - 1000) / 2;

                $("#FooterPopup").animate({ "top": "100px", "left": width + 200 + "px", "width": "600px", "height": "440px" }, function () {

                    $(".Overlay").fadeTo("slow", 0.6);

                });
            }
        });
    });

    //Gestion de la fermeture de la popup
    $(".FooterPopupHeader a").click(function () {

        $("#FooterPopup").fadeOut(function () {

            $(".Overlay").fadeOut();

        });

    });


    if ($('#RelToClick').val() == "contact/FormulaireMedecin") {
        ShowFormulaireMedecin();
    }
    else {
        $(".Menu1 a[rel='" + $('#RelToClick').val() + "']").eq(0).click();
    }

});

function MenuItemClick(elem) {

    //On récupère les parametres pour l'affichage du contenu
    currentTop = elem.attr("top");
    currentWidth = elem.attr("iwidth");
    currentHeight = elem.attr("iheight");

    var ToHide = CurrentRelMenu;
    CurrentRelMenu = elem.attr("submenu");

    //On cache le contenu
    HidePopup(elem.attr("rel"), ToHide);

    //Gestion du slide
    var index = $(".Menu1 ul").children().index(elem.parent());

    if (index == -1) {
        index = $(".Menu1 ul").children().index($(".Menu1 a[rel='" + elem.attr("rel").split("/")[0] + "/general']").eq(1).parent());
    }

    var delta = 6 - index;
    if (delta != 0) {
        $(".Menu1 ul").animate({ "left": "+=" + delta * 142 + "px" }, AnimateSpeed, function () {
            var absdelta = Math.abs(delta);
            while (absdelta != 0) {
                if (delta > 0) {
                    $(".Menu1 ul").children().eq(6).clone(true).insertBefore($(".Menu1 ul li:first-child"));
                    $(".Menu1 ul li:last-child").remove();
                }
                else {
                    $(".Menu1 ul").children().eq(6).clone(true).insertAfter($(".Menu1 ul li:last-child"));
                    $(".Menu1 ul li:first-child").remove();
                }
                absdelta--;
            }
            $(".Menu1 ul").css("left", "-426px");
        });
    }

    //On affiche le nouveau sous menu et on affiche le contenu
    $(".Background").animate({ "background-position": "+=" + delta * 1000 + "px 0px" }, AnimateSpeed, function () {
        ShowPopup();

        ShowSubMenu(CurrentRelMenu);

        

    });
}

function SetSize() {
    var width = ($(window).width() - 1000) / 2;

    $(".Cache").css("width", width);
    $(".Logo").css("left", width);
    $(".Header").css("left", width);
    $(".FooterContent").css("left", width);
    $(".Popup").css("right", width + 30);
    $(".Fiche").css("left", width + 30);
}

//Animation qui descend la fleche du sous menu
function DownArrowMenu(callback) {

    $(".Arrow").animate({ "bottom": "-6px" }, callback);
}

//Animation qui monte la fleche du sous menu
function UpArrowMenu() {
    $(".Arrow").animate({ "bottom": "0px" });
}

//Affichage du sous menu
function ShowSubMenu(toShow) {

    if ($(".SubMenu" + toShow).length > 0) {
        ArrowFadeIn(toShow);
        DownArrowMenu(function () {
            $(".SubMenu" + toShow).fadeTo("slow", 0.9, function () { MenuClickable = true; });
        });
    }
    else
        MenuClickable = true;
}

//Disparition du sous menu
function HideSubMenu(toHide) {
    $(".SubMenu" + toHide).fadeOut(function () {
        UpArrowMenu();
        ArrowFadeOut(toHide);
    });
}

//Changement de couleur de la fleche du sous menu
function ArrowFadeOut(toHide) {

    $(".Arrow").removeClass("Arrow" + toHide);

}

//Changement de couleur de la fleche du sous menu
function ArrowFadeIn(toShow) {

    $(".Arrow").addClass("Arrow" + toShow);

}

function ShowPopup() {

    if (Showable) {
        if (!Shown) {
            $(".Popup").fadeIn();
            Shown = true;
            TryToShow = false;
        }
    }
    else {
        TryToShow = true;
    }
}


//Gestion de la disparition du contenu + chargement du contenu suivant en Ajax
function HidePopup(toload, tohide) {

    HideSubMenu(tohide);

    
    $(".Fiche").fadeOut();


    if (Shown)
        $(".Popup").fadeOut(function () {

            LoadContent(toload);

        });
    else
        LoadContent(toload);

    Showable = false;
    Shown = false;

    
}

function LoadContent(toload) {

    if (toload != undefined) {
        $.ajax({
            type: "GET",
            url: "/Home/Load",
            data: "View=" + toload + "&lang=FR",
            success: function (msg) {
                $(".PopupContent").html(msg);

                $(".Popup").css("top", currentTop + "px");
                $(".Popup").css("width", currentWidth + "px");
                $(".Popup").css("height", currentHeight + "px");

                $(".PopupContent").css("width", currentWidth - 40 + "px");
                $(".PopupContent").css("height", currentHeight - 40 + "px");

                if (toload == "equipe/general")
                    InitializeEquipe();

                InitializeLinkContent();
                

                Showable = true;
                if (Shown == false && TryToShow == true)
                    ShowPopup();
            }
        });
    }

}

//Gestion du click sur les liens du sous menu pour chargement en ajax
function InitializeSubMenu() {
    $(".SubMenuItem").click(function () {

        var elem = $(this);

        $.ajax({
            type: "GET",
            url: "/Home/Load",
            data: "View=" + elem.attr("rel") + "&lang=FR",
            success: function (msg) {
                $(".PopupContent").html(msg);

                $(".PopupContent #LienRetour").css("display", "none");

                InitializeLinkContent();
            }
        });
    });
}

//Gestion du click sur les liens du contenu pour chargement en ajax
function InitializeLinkContent() {

    $("#ContactLink").click(function () {

        var elem = $(this);

        var position = elem.offset();
        var size = {};
        size.width = elem[0].offsetWidth;
        size.height = elem[0].offsetHeight;

        $("#FooterPopup").css("top", position.top + "px");
        $("#FooterPopup").css("left", position.left + "px");
        $("#FooterPopup").css("height", size.height + "px");
        $("#FooterPopup").css("width", size.width + "px");

        $.ajax({
            type: "GET",
            url: "/Home/Load",
            data: "View=Contact/Formulaire",
            success: function (msg) {
                $(".FooterPopupContent").html(msg);

                InitializeFormulaire();

                var width = ($(window).width() - 1000) / 2;

                $("#FooterPopup").animate({ "top": "66px", "left": width + 54 + "px", "width": "850px", "height": "450px" }, function () {

                    $(".Overlay").fadeTo("slow", 0.6);

                });
            }
        });
    });

    $("#EcardLink").click(function () {

        var elem = $(this);

        var position = elem.offset();
        var size = {};
        size.width = elem[0].offsetWidth;
        size.height = elem[0].offsetHeight;

        $("#FooterPopup").css("top", position.top + "px");
        $("#FooterPopup").css("left", position.left + "px");
        $("#FooterPopup").css("height", size.height + "px");
        $("#FooterPopup").css("width", size.width + "px");

        $.ajax({
            type: "GET",
            url: "/Home/Load",
            data: "View=" + elem.attr("rel") + "&lang=FR",
            success: function (msg) {
                $(".FooterPopupContent").html(msg);

                var width = ($(window).width() - 1000) / 2;

                $("#FooterPopup").animate({ "top": "100px", "left": width + 104 + "px", "width": "750px", "height": "620px" }, function () {

                    $(".Overlay").fadeTo("slow", 0.6);

                });
            }
        });
    });

    $("#MapLink").click(function () {

        var elem = $(this);

        var position = elem.offset();
        var size = {};
        size.width = elem[0].offsetWidth;
        size.height = elem[0].offsetHeight;

        $("#FooterPopup").css("top", position.top + "px");
        $("#FooterPopup").css("left", position.left + "px");
        $("#FooterPopup").css("height", size.height + "px");
        $("#FooterPopup").css("width", size.width + "px");

        $.ajax({
            type: "GET",
            url: "/Home/Load",
            data: "View=" + elem.attr("rel") + "&lang=FR", 
            success: function (msg) {
                $(".FooterPopupContent").html(msg);

                var width = ($(window).width() - 1000) / 2;

                $("#FooterPopup").animate({ "top": "100px", "left": width + 200 + "px", "width": "620px", "height": "465px" }, function () {

                    $(".Overlay").fadeTo("slow", 0.6);

                });
            }
        });
    });

    $(".PopupContent a").click(function () {

        var elem = $(this);

        if (elem.attr("id") != "EcardLink" && elem.attr("id") != "MapLink" && !elem.hasClass("SubMenuToMenuItem") && !elem.hasClass("LinkToMenuItem") && !elem.hasClass("ContactLink")) {

            $.ajax({
                type: "GET",
                url: "/Home/Load",
                data: "View=" + elem.attr("rel") + "&lang=FR",
                success: function (msg) {
                    $(".PopupContent").html(msg);

                    InitializeLinkContent();
                }
            });
        }
    });

    $(".PopupContent .SubMenuToMenuItem").click(
        function () {

            var elem = $(this);

            $(".Menu1 a[rel='" + elem.attr("rel") + "']").eq(1).click();
        }
    );

    $(".PopupContent .LinkToMenuItem").click(
        function () {

            MenuItemClick($(this));
        }
    );
}


var currentEtapeFormulaire = 1;
function InitializeFormulaire() {

//    $.validator.addMethod("phone", function (ph, element) {
//        if (ph == null) {
//            return false;
//        }
//        var stripped = ph.replace(/[\s()+-]|ext\.?/gi, "");
//        // 10 is the minimum number of numbers required
//        return ((/\d{10,}/i).test(stripped));
//    }, "Please enter a valid phone number");

    $("#FormulaireContact").validate();

    currentEtapeFormulaire = 1;

    $(".FormulaireContactMenu div").click(function () {

        var indx = $(".FormulaireContactMenu div").index($(this));

        $("#FormulaireContact fieldset").each(function () {

            $(this).css("display", $("#FormulaireContact fieldset").index($(this)) == indx ? "block" : "none");


        });

    });


    $(".NextButton").click(function () {


        if ($("#FormulaireContact").valid()) {

            currentEtapeFormulaire++;

            CheckFormulaireContactButton();

            $("#FormulaireContact fieldset").each(function () {

                $(this).css("display", $("#FormulaireContact fieldset").index($(this)) == currentEtapeFormulaire - 1 ? "block" : "none");
                $("#EtapeFormulaireImage").attr("src", "/content/image/etape" + currentEtapeFormulaire + ".gif");
            });
        }
    });

    $(".PreviousButton").click(function () {

        currentEtapeFormulaire--;

        CheckFormulaireContactButton();

        $("#FormulaireContact fieldset").each(function () {

            $(this).css("display", $("#FormulaireContact fieldset").index($(this)) == currentEtapeFormulaire - 1 ? "block" : "none");
            $("#EtapeFormulaireImage").attr("src", "/content/image/etape" + currentEtapeFormulaire + ".gif");
        });
    });

    

}

function CheckFormulaireContactButton() {

    if (currentEtapeFormulaire < 2) {
        $(".PreviousButton").css("display", "none");
        $(".NextButton").css("display", "block");
        $(".SubmitButton").css("display", "none");
    }
    else if (currentEtapeFormulaire > 1) {
        $(".PreviousButton").css("display", "block");
        $(".NextButton").css("display", "none");
        $(".SubmitButton").css("display", "block");
    }
    else {
        
        $(".PreviousButton").css("display", "block");
        $(".NextButton").css("display", "block");
        $(".SubmitButton").css("display", "none");
    }
}

function FormulaireSuccess() {

    $(".CloseButton").click(function () {

        $("#FooterPopup").fadeOut(function () {

            $(".Overlay").fadeOut();

        });
    });

}

function FormulaireMedecinSuccess() {

    $(".CloseButton").click(function () {

        $("#FooterPopup").fadeOut(function () {

            $(".Overlay").fadeOut();

        });
    });

    if ($("#Identifiant").length > 0) {
        LaunchTagForm($("#Identifiant").val()); 
    }

}

function ShowFormulaireMedecin()
{
    var elem = $(this);

    var position = elem.offset();
    var size = {};
    size.width = elem[0].offsetWidth;
    size.height = elem[0].offsetHeight;

    $("#FooterPopup").css("top", 0 + "px");
    $("#FooterPopup").css("left", 0 + "px");
    $("#FooterPopup").css("height", 0 + "px");
    $("#FooterPopup").css("width", 0 + "px");

    $.ajax({
        type: "GET",
        url: "/Home/Load",
        data: "View=Contact/FormulaireMedecin",
        success: function (msg) {
            $(".FooterPopupContent").html(msg);

            InitializeFormulaire();

            var width = ($(window).width() - 1000) / 2;

            $("#FooterPopup").animate({ "top": "66px", "left": width + 54 + "px", "width": "850px", "height": "450px" }, function () {

                $(".Overlay").fadeTo("slow", 0.6);

            });
        }
    });
}
