// JavaScript Document

$(document).ready(function() {

    setTimeout(function() {

        /***************************************************************************************/


        // ENVIRONNEMENT LABEL : pagination
        var pagination;
        var pagination1;
        var pagination2;
        var pagination3;
        var pagination4;

        pagination1 = "<div class='pagination'>";
        pagination2 = "<img src='/ressources/images/FR/js_prev.gif' alt=' < ' />";
        pagination3 = "<img src='/ressources/images/FR/js_next.gif' alt=' > ' />";
        pagination4 = "</div>";

        $(".container.labels").find(".label2,.label3,.label4,.label5").hide();

        // Pour chaque div, on ajoute les fleches, avec les exception pour la première et la dernière.
        $(".container.labels").find(".label1,.label2,.label3,.label4,.label5").each(function() {
            var current;
            current = $(this).attr('class').length;
            current = $(this).attr('class').substring(current - 1, current);

            if (current == '1') {
                pagination = pagination1 + pagination3 + pagination4;
                $(this).prepend(pagination);
            }
            else if (current == '5') {
                pagination = pagination1 + pagination2 + pagination4;
                $(this).prepend(pagination);
            }
            else {
                pagination = pagination1 + pagination2 + pagination3 + pagination4;
                $(this).prepend(pagination);
            }
        });

        // ajout de l'appel de la fonction au clic sur l'élément, avec les bon param
        $(".container.labels .pagination img").click(function() {
            var current;
            current = $(this).parent().parent().attr('class').length;
            current = $(this).parent().parent().attr('class').substring(current - 1, current);
            var sens;
            if ($(this).attr('src').indexOf("next") != -1) {
                sens = "next"
            }
            else if ($(this).attr('src').indexOf("prev") != -1) {
                sens = "prev"
            }

            labelTurnPage(sens, current);
        });

        /***************************************************************************************/
        // CHAMPS INSCRIPTION NEWS

        $('.newsletter :text').focus(function() {
            if ($(this).attr('value') == 'Mon adresse email') {
                $(this).attr('value', '');
            }
        });
        $('.newsletter :text').blur(function() {
            if ($(this).attr('value') == '') {
                $(this).attr('value', 'Mon adresse email');
            }
        });


        // CHAMPS LOGIN DISTRIBUTEUR
        $('.espace_distributeur :text:first').focus(function() {
            if ($(this).attr('value') == 'Login') {
                $(this).attr('value', '');
            }
        });
        $('.espace_distributeur :text:first').blur(function() {
            if ($(this).attr('value') == '') {
                $(this).attr('value', 'Login');
            }
        });
        $('.espace_distributeur :text:last').focus(function() {
            if ($(this).attr('value') == 'Mot de passe') {
                $(this).attr('value', '');
            }
        });
        $('.espace_distributeur :text:last').blur(function() {
            if ($(this).attr('value') == '') {
                $(this).attr('value', 'Mot de passe');
            }
        });

        /***************************************************************************************/


        // HISTORIQUE / SOCIETE - PAGINATION
        var pagination = "<div class='pagination'><img src='/ressources/images/FR/js_prev_grey.gif' alt=' < ' /><img src='/ressources/images/FR/js_next_grey.gif' alt=' > ' /></div>";

        // on ajoute la pagination au début et à la fin
        $('.historique .intro_footer, .societe .intro_footer').append(pagination);
        $('.historique #content, .societe #content').append(pagination);

        // Comme il n'y a que de slide, on masque la premiere flement, et la deuxieme slide
        $('.historique .pagination, .societe .pagination').find('img:first').hide();
        $('.historique .historique_2, .societe .societe_2').hide();

        //Si on clique sur la premiere image ( < )
        $('.historique .pagination img:even, .societe .pagination img:even').click(function() {
            $('.historique_2, .societe_2').hide().prev().show();
            $('.historique .pagination, .societe .pagination').find('img').toggle();
            calculDesHauteurs();
        });
        //Si on clique sur la deuxieme image ( > )
        $('.historique .pagination img:odd, .societe .pagination img:odd').click(function() {
            $('.historique_1, .societe_1').hide().next().show();
            $('.historique .pagination, .societe .pagination').find('img').toggle();
            calculDesHauteurs();
        });

        /***************************************************************************************/

        // TEMPLATE : callage des hauteurs dans le gabarit
        $("#menu").height($("#content-box").height() + 50);
        $("#content-bottom").css("top", $("#content-box").height() + 6);
        $("#footer").css("top", $("#content").height() + 40);

        // reglage pour la home
        setTimeout(function() {
            $(".home #content-bottom").css("top", 489);
            $(".home #footer").css("top", 503);
        }, 1);
        //reglage pour la recherche
        setTimeout(function() {
            $(".rechercheproduits #content-bottom").css("top", $("#content").height() + 12);
            $(".rechercheproduits #footer ").css("top", $("#content").height() + 20);
        }, 1);

        /***************************************************************************************/

        // FORMULAIRE : ajout de class afin de pouvoir ciblier les différents <input type="">
        $(":text").addClass("input-text");
        $(":radio").addClass("input-radio");
        $(":image").addClass("input-image");

        // ACTUALITE : div qui s'ouvre à l'ouverture du site, sur la home.
        if ($(".container").is(".home") && ($('.actualite').size() > 0) && ($('.div_mdp_oublie').size() == 0)) {
            div_actu('ouvrir');
            $(".fermer-actu").click(function() { div_actu('fermer'); });
        }
        if ($('.div_mdp_oublie').size() > 0) {
            $("#div-box").css({ width: '100%', height: $(document).height() }).show();
            $(".fermer-actu").click(function() { $('.div_mdp_oublie').remove(); $("#div-box").hide(); });
        }

        /***************************************************************************************/

        // MENU SERVICE : ajout de classe pour les premiers et derniers.
        $("ul.MenuService li:first").addClass("MenuService-first");
        $("ul.MenuService li:last").addClass("MenuService-last");

        $("ul.MenuEnvironnement li:first").addClass("MenuEnvironnement-first");
        $("ul.MenuEnvironnement li:last").addClass("MenuEnvironnement-last");

        $("ul.MenuConseil li:first").addClass("MenuConseil-first");
        $("ul.MenuConseil li:last").addClass("MenuConseil-last");

        /***************************************************************************************/
        // PLAN D'ACCES
        $('.link_plan_noyelles').click(function() {
            displayNoyellesMap();
            calculDesHauteurs();
        });
        $('.link_plan_colomiers').click(function() {
            displayColomiersMap();
            calculDesHauteurs();
        });
        $('.link_plan_retour').click(function() {
            displayListMap();
            calculDesHauteurs();
        });
        /***************************************************************************************/
        // MARIAGE COULEUR (CONSEIL)
        $('.onglet_mariage_couleur img').mouseover(function() {
            $(this).css('opacity', 1);
        });
        $('.onglet_mariage_couleur img').mouseout(function() {
            $(this).css('opacity', 0.8);
        });
        /***************************************************************************************/

        // CONSEIl - MARIAGE DES COULEURS
        $('.mariagecouleur .marier_couleur_chaude').hide();
        $('.mariagecouleur .marier_couleur_neutre').hide();
        calculDesHauteurs();



        $('.onglet_mariage_couleur img').click(function() {
            if ($(this).attr('src').indexOf("chaude") != -1) {
                resetOngletMariage();
                var source = $(this).attr("src").length;
                var newsource = $(this).attr("src").substring(0, source - 8) + ".jpg";
                $(this).attr("src", newsource);
                $('.mariagecouleur .marier_couleur_froide').hide();
                $('.mariagecouleur .marier_couleur_chaude').show();
                $('.mariagecouleur .marier_couleur_neutre').hide();
            }
            else if ($(this).attr('src').indexOf("froide") != -1) {
                resetOngletMariage();
                var source = $(this).attr("src").length;
                var newsource = $(this).attr("src").substring(0, source - 8) + ".jpg";
                $(this).attr("src", newsource);
                $('.mariagecouleur .marier_couleur_froide').show();
                $('.mariagecouleur .marier_couleur_chaude').hide();
                $('.mariagecouleur .marier_couleur_neutre').hide();
            }
            else if ($(this).attr('src').indexOf("neutre") != -1) {
                resetOngletMariage();
                var source = $(this).attr("src").length;
                var newsource = $(this).attr("src").substring(0, source - 8) + ".jpg";
                $(this).attr("src", newsource);
                $('.mariagecouleur .marier_couleur_froide').hide();
                $('.mariagecouleur .marier_couleur_chaude').hide();
                $('.mariagecouleur .marier_couleur_neutre').show();
            }
            calculDesHauteurs();
        });

        /***************************************************************************************/

        // DOC DISPO
        $('.documentsdisponibles').find('.marque_pro div, .marque_part div').hide();
        $('.documentsdisponibles').find('.marque_pro img, .marque_part img').click(function() {


            var reponse = $(this).next();
            if (reponse.is(':visible')) {
                reponse.animate(
				{ opacity: 0 }
				, 'slow');
            }
            else {
                $('.documentsdisponibles').find('.marque_pro div, .marque_part div').stop().css("opacity", 0).hide();
                $(this).next().show().animate({ opacity: 1 });
            }
        });
        /*


        /***************************************************************************************/
        // CONCEPT PART
        if ($('.nosconceptspart').size() > 0) {

            /*$('#content li li span').each(function() {
            $(this).attr('title', $(this).html()).html('');
            });*/
            $('#content li li span').each(function() {
                $(this).html("<font>" + $(this).attr('title') + "</font>");
                $(this).attr('title', '').find('font').css('visibility','hidden');
            });
            $('#content li li').hover(function() {
                $(this).parent().parent().find('p.color').html($(this).find('span font').html());
            },
            function() {
                $(this).parent().parent().find('p.color').html('');
            });
        }

        /***************************************************************************************/

        // GESTION COMPTE DISTRIBUTEUR
        if ($('.distributeurmoncompte').size() > 0) {

            if ($('.form_frame.active').size() > 0) {
                $('.form_frame:not(.active)').hide();
            } else {
                $('.form_frame:not(:first)').hide();
            }

            $('.MenuCompte li a').click(function() {
                var frameToShow = $(this).parent().attr('class');
                $('.form_frame').not('.' + frameToShow).hide();
                $('.' + frameToShow).show();
                calculDesHauteurs();
                return false;
            });
        }

        // Accueil espace distributeur
        if ($('.distributeurhome').size() > 0) {
            if ($('.part1 .theomail').size() == 0) {
                $('.part1 .commande').css('margin', '0 auto');
            }
        }
        // Menu espace distributeur
        if ($('.entete_distributeur').size() > 0) {
            $('.menu li:empty').hide();
        }


        calculDesHauteurs();

        afficheDate()

    }, 1);

    setTimeout(function() {
        calculDesHauteurs();
    }, 1000);

});