//REQUIERT jQuery

//menus_deroulants.js

//Auteur : Francois Moreau



MENUS_DEROULANTS = [

    { contenant:   'div.theme',

      onglet:      function() { return $('div:first', this); },

      sous_themes: function() { return $('div:last',  this); }

    },

    

    { contenant:   '#ressources:has(#ressourcessousmenu)',

      onglet:      function() { return $('div:first', this); },

      sous_themes: function() { return $('div:last',  this); },

      bouton:      'boutonsTop'

    }, 

    

    { contenant:   '#ressources:not(:has(#ressourcessousmenu))', //Menus #ressources avec #ressourcessousmenu a l'exterieur du contenant

      onglet:      function() { return $(this); },

      sous_themes: function() { return $('#ressourcessousmenu'); },

      bouton:      (-1==location.href.indexOf('/index.php') &&

                    -1==location.href.indexOf('/fdrfm.php') ) ? 'boutonsTop' : 'ressources' //variation a la page d'accueil

    },

    

    { contenant:   '#presentation',

      onglet:      function() { return $(this); },

      sous_themes: function() { return $('#presentationsousmenu'); },

      bouton:      'presentation'

    }

];



//Execution au chargement du DOM

$(function () {

    var chronos = {};

    

    function affiche_sous_themes (index, onglet, sous_themes) {

        clearTimeout(chronos[index]);

        sous_themes.show();

        if(sous_themes.filter('#ressourcessousmenu').length) {

            var pos_sous_menu = (onglet.offset().left + onglet.width() + 10);

            if(pos_sous_menu + sous_themes.width() > jQuery(window).width()) {

                pos_sous_menu = jQuery(window).width() - sous_themes.width();

            }

            sous_themes.css({ left: pos_sous_menu +'px' });

        }

        $('img:first', onglet).attr('class', 'ouvert');

        $('a', onglet).addClass('ressourcesOn');

    }

    

    function cache_sous_themes (index, onglet, sous_themes) {

        chronos[index] = setTimeout(

            function() { 

                sous_themes.hide();

                $('img:first', onglet).attr('class', 'fermer');

                $('a', onglet).removeClass('ressourcesOn');

            },

            500

        );

    }

    

    $.each( MENUS_DEROULANTS, function(index_menu, menu) {

        $(menu.contenant).each( function(index) {

            var onglet       = menu.onglet.apply(this);

            var sous_themes  = menu.sous_themes.apply(this);

            var index_chrono = index_menu + '/' + index;

            

            if('bouton' in menu) {

                var id_bouton = menu.bouton;

                var onmouseover = function() { affiche_sous_themes(index_chrono, onglet, sous_themes); if("function" == typeof fsButton) fsButton(id_bouton, -40);  };

                var onmouseout  = function() { cache_sous_themes  (index_chrono, onglet, sous_themes); if("function" == typeof fsButton) fsButton(id_bouton, 0);    };

            } else {

                var onmouseover = function() { affiche_sous_themes(index_chrono, onglet, sous_themes); };

                var onmouseout  = function() { cache_sous_themes  (index_chrono, onglet, sous_themes); };            

            }

    

            onglet.bind('mouseover', onmouseover);

            onglet.bind('mouseout',  onmouseout);

    

            sous_themes.bind('mouseover', onmouseover);

            sous_themes.bind('mouseout',  onmouseout); 

        } );

    } );

    

    



    

    

    /* Le positionnement par ancrage se fait avant la dissimulation des sous-themes, resultat :

       la position de depart est decalee d'une distance egale au plus grand sous-theme.

       On repositionne donc apres chargement, s'il y a lieu. */

    if(-1 != location.href.indexOf('#')) location.href = location.href.toString();

} );


