/*
 * MENU ACCORDEON SUR LE MENU DE GAUCHE
 */
function accordeong()
{
	$("div.categorieg .titremenu:not(a)").wrapInner('<a href="#"></a>');
	$("div.categorieg div.contenumenu").hide();
	$("div.categorieg .titremenu").click( function () {
	     
	// Si le sous-menu était déjà ouvert, on le referme :
        if ($(this).next("div.contenumenu:visible").length != 0) {
            $(this).next("div.categorieg div.contenumenu").slideUp("normal");
        }
        // Si le sous-menu est caché, on ferme les autres et on l'affiche :
        else {

            $("div.categorieg div.contenumenu").slideUp("normal");
            $(this).next("div.categorieg div.contenumenu").slideDown("normal");
        }
        return false;
    });  
}

/*
 * MENU ACCORDEON SUR LE MENU DE DROITE
 */
function accordeond()
{
	$("div.categoried .titremenu:not(a)").wrapInner('<a href="#"></a>');
	$("div.categoried div.contenumenu").hide();
	$("div.categoried .titremenu").click( function () {
	     
	// Si le sous-menu était déjà ouvert, on le referme :
        if ($(this).next("div.contenumenu:visible").length != 0) {
            $(this).next("div.categoried div.contenumenu").slideUp("normal");
        }
        // Si le sous-menu est caché, on ferme les autres et on l'affiche :
        else {

            $("div.categoried div.contenumenu").slideUp("normal");
            $(this).next("div.categoried div.contenumenu").slideDown("normal");
        }
        return false;
    }); 
}

/*
 * CHARGEMENT DES MENUS HORIZONTAUX (TAILLE + MENUS DEROULANT) ET VERTICAUX (SOUS-PAGES)
 */
function chargementmenu()
{

   $("#menuh ul.menu").attr("id","nav");

   if($('#menuh ul').length>0)
   {
	// activer menus déroulants
	$('#menuh ul').droppy({speed: 200});
	
	// calcul largeur menu
	largeurmenuh=0;
	$('#menuh ul li').map(function(){ largeurmenuh=largeurmenuh+$(this).width(); });
	$('#menuh ul.menu').width(largeurmenuh + 3);
        $('#menuh ul#nav').width(largeurmenuh + 3);


   }
   if($('#menub ul').length>0)
   {
	largeurmenub=0;
	$('#menub ul li').map(function(){ largeurmenub=largeurmenub+$(this).width(); });
	$('#menub ul').width(largeurmenub+3);
   }

   $("#menug .categorieg ul li ul").hide();
   $("#menug .categorieg ul li").hover(function(){
      $('ul', this).show();
   },function(){
      $('ul', this).hide();
   }); 

   $("#menud .categoried ul li ul").hide();
   $("#menud .categoried ul li").hover(function(){
      $('ul', this).show();
   },function(){
      $('ul', this).hide();
   }); 
    
}

/*
 * Droppy 0.1.2 (plugin jQuery menus déroulants)
 * (c) 2008 Jason Frame (jason@onehackoranother.com)
*/
$.fn.droppy = function(options) {
    
  options = $.extend({speed: 250}, options || {});
  
  this.each(function() {
    
    var root = this, zIndex = 1000;
    
    function getSubnav(ele) {
      if (ele.nodeName.toLowerCase() == 'li') {
        var subnav = $('> ul', ele);
        return subnav.length ? subnav[0] : null;
      } else {
        return ele;
      }
    }
    
    function getActuator(ele) {
      if (ele.nodeName.toLowerCase() == 'ul') {
        return $(ele).parents('li')[0];
      } else {
        return ele;
      }
    }
    
    function hide() {
      var subnav = getSubnav(this);
      if (!subnav) return;
      $.data(subnav, 'cancelHide', false);
      setTimeout(function() {
        if (!$.data(subnav, 'cancelHide')) {
          $(subnav).slideUp(options.speed);
        }
      }, 200);
    }
  
    function show() {
      var subnav = getSubnav(this);
      if (!subnav) return;
      $.data(subnav, 'cancelHide', true);
      $(subnav).css({zIndex: zIndex++}).slideDown(options.speed);
      if (this.nodeName.toLowerCase() == 'ul') {
        var li = getActuator(this);
        $(li).addClass('hover');
        $('> a', li).addClass('hover');
      }
    }
    
    $('ul, li', this).hover(show, hide);
    $('li', this).hover(
      function() { $(this).addClass('hover'); $('> a', this).addClass('hover'); },
      function() { $(this).removeClass('hover'); $('> a', this).removeClass('hover'); }
    );
    
  });
  
};

/*WIDGETS*/
$(document).ready(function(){    
   $('.categorieg, .categoried').map(
      function(){
         $(this).append('<div class="clear"></div>');
      }
   );

   /*PAGES PAR BLOC*/
   if($('.rows').length > 0)
   {
   
      var row_width = $('.rows').width();
      if(row_width/2 != Math.round(row_width/2))
      {
         $('.cols').css('width', row_width + 19);
      }
 
      //Largeur des lignes
      var r = 0;
      var div = document.getElementsByTagName('div');
      while(element = div[r++])
      {
         if(element.className == 'rows'){
            var rows_width = element.offsetWidth;
            var rows_container = element;
         }
      }
      if(rows_container)
      {
         //Reporter la largeur
         var i = 0;
         var div = rows_container.getElementsByTagName('div');
         while(element = div[i++])
         {
            if(element.className == 'cols'){
               element.style.width = (rows_width + 20) + 'px';
            }
         }
      }
   }
});


