// JavaScript Document
/* horaires
------------------------------------------------------------------------------*/

$(document).ready(function() {
    // mdejoux : fiche horaire excel
    $(".grille_fiche_horaire, .grille_nav_horaire").hide();
    $(".grille_fiche_horaire:first, .grille_nav_horaire:first").show();

    $(".horaires_suivants a, .horaires_precedents a").click(function() {
        // On masque tous les éléments
        //$(".grille_fiche_horaire").hide();
        $(".heures, .onglet, .grille_fiche_horaire, .grille_nav_horaire").hide();

        // On affiche la div de la bonne grille horaire
        $($(this).attr("href")).show();

        // On affiche la bonne nav (horaires suivants / horaires précédents)
        $($(this).attr("href") + "_nav_horaire").show();

        // On affiche les bons onglets et horaires
        $($(this).attr("href")).children(".heures:first").show();
        $($(this).attr("href")).children(".onglet:first").show();

        /*$($(this).attr("href")).show();

        $($(this).attr("href")).prev(".heures").show();
        $($(this).attr("href")).prev(".heures").find('.actif a').focus();

        $(this).blur();*/
        return (false);
    });
    // mdejoux


    $(".heures, .onglet").hide();
    $(".heures:first, .onglet:first").show();

    $(".heures a, a.plan").click(function() {
        $(".heures, .onglet").hide();
        $($(this).attr("href")).show();

        $($(this).attr("href")).prev(".heures").show();
        $($(this).attr("href")).prev(".heures").find('.actif a').focus();

        $(this).blur();
        return (false);
    });

    /**************************************************************/

    $("ul.subMenu:not('.open_at_load')").hide();
    $("li.toggleSubMenu > a").each(function() {
        //$("li.toggleSubMenu h3 span").each( function () {
        var TexteSpan = $(this).text();
        $(this).replaceWith('<a href="" title="Afficher le sous-menu">' + TexteSpan + '</a>');
    });

    $('.open_at_load').prev("a").attr("title", "Masquer le sous-menu");

    $("li.toggleSubMenu > a, li.toggleSubMenu > h3 span").click(function() {
        var $this = $(this);
        //alert("debut : "+ $this.next("ul.subMenu").is(":visible"));
        //alert($this.next("ul.subMenu").count);
        if ($this.next("ul.subMenu").is(":visible")) {
            //alert("vizible true");
            $this.attr("title", "Afficher le sous-menu").next("ul.subMenu").slideUp(1000);
            $this.parent("li").removeClass("open");
        }
        else {
            //alert("vizible false");
            $("ul.subMenu").slideUp(1000);
            $("li.toggleSubMenu").removeClass("open");
            $("li.toggleSubMenu > a, li.toggleSubMenu > h3 span").attr("title", "Afficher le sous-menu");
            $this.attr("title", "Masquer le sous-menu").next("ul.subMenu").slideDown(1000);
            $this.parent("li").addClass("open");
        }
        //alert("fin : "+ $this.next("ul.subMenu").is(":visible"));
        return false;
    });

    /**************************************************************/

    $("ul.subMenu:not('.open_at_load')").hide();

    $("li.toggleSubMenu h3 > span").each(function() {
        var TexteSpan = $(this).text();
        $(this).replaceWith('<a href="" title="Afficher le sous-menu">' + TexteSpan + '</a>');
    });

    $('.open_at_load').prev().children("a").attr("title", "Masquer le sous-menu");


    // version antoine
    /*$("#te_menuFaq li.toggleSubMenu").each(function() {
    var self = this;
    var $collapsable = $('ul', this);
    var $link = $('h3 > a', this); // lien cliquable déclenchant le slide
    //$collapsable.css('border', '1px solid green');
    $link
    //.css('border', '1px solid red')
    .click(function() {
    if($(self).hasClass('open')) {
    $collapsable.slideUp('slow');
    $(self).removeClass('open');
    }
    else {
    $('#te_menuFaq li.toggleSubMenu.open').removeClass('open').find('ul').slideUp('slow');
    $collapsable.slideDown('slow');
    $(self).addClass('open');
    }
    return false;
    });
    });*/

    $("#te_menuFaq li.toggleSubMenu h3 > a").click(function() {
        if ($(this).parent().next().is(":visible")) {
            $(this).parent().next().slideUp(1000, function() {
                $(this).parent().removeClass("open");
                $(this).prev().children("a").attr("title", "Afficher le sous-menu");
            });
            $("#te_menuFaq .open h3 > a").attr("title", "Masquer le sous-menu");
        }
        else {
            $("#te_menuFaq ul.subMenu").slideUp("slow", function() {
                $(this).parent().removeClass("open");
            });
            $(this).parent().next().slideDown(1000, function() {
                $(this).parent().addClass("open");
                $(this).prev().children("a").attr("title", "Masquer le sous-menu");
            });
            $("#te_menuFaq .open h3 > a").attr("title", "Afficher le sous-menu");
        }
        return false;
    });

    //version safe
    /*$("li.toggleSubMenu h3 > a").click(function() {
    var $a = $(this);
    if($(this).parent().next().is(":visible")) {
    $(this).parent().next().slideUp(2000, function() {
    $(this).parent().removeClass("open");
    $(this).prev().children("a").attr("title", "Afficher le sous-menu");
    });
    $(".open h3 > a").attr("title", "Masquer le sous-menu");
    }
    else {
    $("ul.subMenu").slideUp("slow", function() {
    $(this).parent().removeClass("open");
    });
    $(this).parent().next().slideDown(2000, function() {
    $(this).parent().addClass("open");
    $(this).prev().children("a").attr("title", "Masquer le sous-menu");
    });
    $(".open h3 > a").attr("title", "Afficher le sous-menu");
    }
    return false;
    });*/

    //    $("ul.subMenu:not('.open_at_load')").hide();
    //    $("li.toggleSubMenu span").each(function() {
    //        var TexteSpan = $(this).text();
    //        $(this).replaceWith('<a href="" title="Afficher le sous-menu">' + TexteSpan + '</a>');
    //    });
    //    $('.open_at_load').prev("a").attr("title", "Masquer le sous-menu");
    //    $("li.toggleSubMenu > a").click(function() {
    //        if ($(this).next("ul.subMenu:visible").length != 0) {
    //            $(this).next("ul.subMenu").slideUp("slow", function() {
    //                $(this).parent().removeClass("open");
    //                $(this).prev("a").attr("title", "Afficher le sous-menu");
    //            });
    //            $(".open a").attr("title", "Masquer le sous-menu");
    //        }
    //        else {
    //            $("ul.subMenu").slideUp("slow", function() { $(this).parent().removeClass("open") });
    //            $(this).next("ul.subMenu").slideDown("slow", function() {
    //                $(this).parent().addClass("open");
    //                $(this).prev("a").attr("title", "Masquer le sous-menu");
    //            });
    //            $(".open a").attr("title", "Afficher le sous-menu");
    //        }
    //        return false;
    //    });

    // Date limite à deux mois
    DateNow = new Date();
    DateLimit = new Date(DateNow.getFullYear(), DateNow.getMonth() + 2, DateNow.getDate());

    $('.date-pick').datePicker({
        endDate: DateLimit.asString()
    });
    $('.date-pick').change(function() {
        v = $(this).attr("value");
        v = v.split(/\//);
        $(".date_aller option[value='" + v[0] + "']").attr("selected", true);
        $(".mois_aller option[value='" + v[1] + "']").attr("selected", true);
        $(".annee_aller option[value='" + v[2] + "']").attr("selected", true);
    });


    $('.date-pick2').datePicker({
        endDate: DateLimit.asString()
    });
    $('.date-pick2').change(function() {
        v = $(this).attr("value");
        v = v.split(/\//);
        $(".date_retour option[value='" + v[0] + "']").attr("selected", true);
        $(".mois_retour option[value='" + v[1] + "']").attr("selected", true);
        $(".annee_retour option[value='" + v[2] + "']").attr("selected", true);
    });

    $(".agenda_selection > dl").addClass("hide");
    $(".agenda_lien").click(function() {
        $(this).parent().find("dl").toggleClass("hide");
        return false;
    });

    // CLA - 20091124 - 001
    if (jQuery('.myselectbox').length > 0) {
        jQuery('.myselectbox').selectbox();
    }
    if (jQuery('.myselectbox2').length > 0) {
        jQuery('.myselectbox2').selectbox();
    }
    // CLA - 20091124 - 001
    // GFA@25/01/2010 1478
    if (jQuery('.myselectbox3').length > 0) {
        jQuery('.myselectbox3').selectbox();
    }
    // GFA@25/01/2010 1479
    if (jQuery('.myselectbox4').length > 0) {
        jQuery('.myselectbox4').selectbox();
    }
    if (jQuery('.myselectbox5').length > 0) {
        jQuery('.myselectbox5').selectbox();
    }
    
    $(".menu2 a").append("<em></em>");

    $(".menu2 a").hover(function() {
        $(this).find("em").animate({ opacity: "show" }, "slow");
        var hoverText = $(this).attr("title");
        $(this).find("em").text(hoverText);
        ma_var = $(this).attr("title");
        $(this).attr("title", "");
        ma_alt = $(this).find("img").attr("alt");
        $(this).find("img").attr("alt", "");

    }, function() {
        $(this).find("em").animate({ opacity: "hide" }, "fast");
        $(this).attr("title", ma_var);
        $(this).find("img").attr("alt", ma_alt);

    });

    if ($('.te_message_client').is('div')) {
        $('.te_message_client tr').each(function() {
            $(this).mouseover(function() {
                $(this).addClass('te_over_tr');
            });
            $(this).mouseout(function() {
                $(this).removeClass('te_over_tr');
            });
        });
    }
    //sdi
    $(function() {
        /*$(".te_heures_loisirs, .onglet_loisirs").hide();
        $(".te_heures_loisirs:first, .onglet_loisirs:first").show();*/

        $(".te_heures_loisirs a").click(function() {
            $(".te_heures_loisirs, .onglet_loisirs").hide();
            $($(this).attr("href")).show();

            $($(this).attr("href")).prev(".te_heures_loisirs").show();

            $(this).blur();
            return (false);
        });
    });
    //sdi fin

});

function init_focus_home() {
    fobj = document.getElementById("focus_home");
    fobj.className = 'dyn';
    fobj.cur = 0;
    fobj.chld = fobj.getElementsByTagName("DIV");
    fobj.className = 'dyn';
    for (i = 0; i < fobj.chld.length; i++) {
        evt = fobj.chld[i].getElementsByTagName("H2")[0];
        tablnk = document.createElement("A");
        tablnk.setAttribute("href", "#");
        // tablnk.setAttribute("title", "Onglet actif");
        tablnk.innerHTML = evt.innerHTML;
        evt.innerHTML = '';
        evt.appendChild(tablnk);
        evt = fobj.chld[i].evt = tablnk;
        evt.prt = fobj;
        evt.onclick = function() {
            return this.prt.show_focus_home(1 - fobj.cur);
        }
    }
    fobj.show_focus_home = function(ind) {
        fobj.chld[1 - ind].style.visibility = 'hidden';
        fobj.chld[(fobj.cur = ind)].style.visibility = 'visible';
        return false;
    }
    fobj.show_focus_home(0);
}

function favoris(_url, _titre) {
    if (navigator.appName != 'Microsoft Internet Explorer') {
        window.sidebar.addPanel(_titre, _url, "");
    }
    else {
        window.external.AddFavorite(_url, _titre);
    }
}
