﻿$(document).ready(function() {
    // Centrer element
    jQuery.fn.centerScreen = function(loaded) {
        var obj = this;
        if (!loaded) {
            obj.css('top', $(window).height() / 2 - this.height() / 2);
            obj.css('left', $(window).width() / 2 - this.width() / 2);
            $(window).resize(function() {
                obj.centerScreen(!loaded);
            });
        } else {
            obj.stop();
            obj.animate({ top: $(window).height() / 2 - this.height() / 2, left: $(window).width() / 2 - this.width() / 2 }, 200, 'linear');
        }
    }

    jQuery.fn.extend({
        facultatif: function(phrase) {
            $(this).focus(function() {
                if ($(this).val().length == 0 || $(this).val() == phrase) {
                    $(this).removeClass("jVide");
                    $(this).val("");
                }
            });

            $(this).blur(function() {
                if ($(this).val().length == 0) {
                    $(this).addClass("jVide");
                    $(this).val(phrase);
                }
            });

            if ($(this).val().length > 0 && $(this).val() != phrase) {
                $(this).removeClass("jVide");
            }
            else {
                $(this).addClass("jVide");
                $(this).val(phrase);
            }
        }
    });

    /// Fonction de control de saisie heure
    jQuery.fn.extend({
        controlTime: function() {
            $(this).blur(function() {
                var hrValid = false;
                var tmpHrDeb = $(this).val();

                switch (tmpHrDeb.length) {
                    case 1:
                        exp1 = new RegExp("^[0-9]$");
                        if (exp1.test(tmpHrDeb)) {
                            $(this).val("00:0" + tmpHrDeb);
                            hrValid = true;
                        }
                        break;
                    case 2:
                        exp2 = new RegExp("^[0-5][0-9]$");
                        if (exp2.test(tmpHrDeb)) {
                            $(this).val("00:" + tmpHrDeb);
                            hrValid = true;
                        }
                        break;
                    case 3:
                        exp3 = new RegExp("^[0-9][0-5][0-9]$");
                        if (exp3.test(tmpHrDeb)) {
                            $(this).val("0" + tmpHrDeb.substr(0, 1) + ":" + tmpHrDeb.substr(1, 2));
                            hrValid = true;
                        }
                        break;
                    case 4:
                        exp4 = new RegExp("^(20|21|22|23|[01][0-9])[0-5][0-9]$");
                        if (exp4.test(tmpHrDeb)) {
                            $(this).val(tmpHrDeb.substr(0, 2) + ":" + tmpHrDeb.substr(2, 2));
                            hrValid = true;
                        }
                        break;
                    case 5:
                        exp5 = new RegExp("^(20|21|22|23|[01][0-9]):[0-5][0-9]$");
                        hrValid = exp5.test(tmpHrDeb);
                        break;
                }

                if (!hrValid) {
                    $(this).val("");
                }
            });
        }
    });

    /// Fonction de calcul de la position
    jQuery.fn.extend({
        findPos: function() {
            obj = $(this).get(0);
            var curleft = obj.offsetLeft || 0;
            var curtop = obj.offsetTop || 0;
            while (obj = obj.offsetParent) {
                curleft += obj.offsetLeft
                curtop += obj.offsetTop
            }
            return { x: curleft, y: curtop };
        }
    });

    /*
    * Ouverture et Fermeture des panneaux 'actuMsg'
    */

    $(".btActu").click(function() {
        var panneau = $(this).parent().parent(".enteteActu").next(".actuMsg");

        if ($(this).parent().parent(".enteteActu").next(".actuMsg:visible").length != 0) {
            panneau.slideUp("fast");
            $(this).text("[ Consulter ]");
        }
        else {
            panneau.slideDown("fast");
            $(this).text("[ Fermer ]");
        }
    });

    $(".btTitre").click(function() {
        var panneau = $(this).parent(".enteteActu").next(".actuMsg");
        var btActu = $(this).parent(".enteteActu").children().children(".btActu");

        if ($(this).parent(".enteteActu").next(".actuMsg:visible").length != 0) {
            panneau.slideUp("fast");
            btActu.text("[ Consulter ]");
        }
        else {
            panneau.slideDown("fast");
            btActu.text("[ Fermer ]");
        }
    });

    /// Bouton FAQ

    $(".panelFAQText").hide();
    $(".btFAQ").click(function() {
        var panneau = $(this).parent().parent(".panelFAQTit").next(".panelFAQText");



        if ($(this).parent().parent(".panelFAQTit").next(".panelFAQText:visible").length != 0) {
            panneau.slideUp("fast");
            $(this).text("[ Consulter ]");
        }
        else {
            panneau.slideDown("fast");
            $(this).text("[ Fermer ]");
        }
    });
    $(".btFAQTit").click(function() {
        var panneau = $(this).parent(".panelFAQTit").next(".panelFAQText");
        var btActu = $(this).parent(".panelFAQTit").children().children(".btFAQ");

        if ($(this).parent(".panelFAQTit").next(".panelFAQText:visible").length != 0) {
            panneau.slideUp("fast");
            btActu.text("[ Consulter ]");
        }
        else {
            panneau.slideDown("fast");
            btActu.text("[ Fermer ]");
        }
    });
});