/* --------------------------------------------------------------- 
   Author : Remi Palard
   June 2008
   remi.palard@gmail.com  
--------------------------------------------------------------- */

var menu = {
 selected : '',
 active: {top:false,sub:false},
 timer : 0,
 timeBeforeAutoHide : 2000,
 timeBeforeActivation : 500,
 originalMenu : false,
 menuChecked : false,
 maxElements : 0,
 sub: 'submenuContainer',
 
 /* ----------------------------------- */  
   
 start: function(el) { 
  this.getMaxElements();
  var id = $(el).up().id;
  this.active.top = true;
  setTimeout(function() {
    this.display(id);
  }.bind(this),this.timeBeforeActivation);
 },
 
 /* ----------------------------------- */  
 
 getMaxElements: function() { 
  if (this.maxElements == 0) {
    $('menu').select('td').each(function(el){
      if (el.className.indexOf('menug') > -1 || el.className.indexOf('menuw') > -1) this.maxElements++;
    }.bind(this));
  }
 },
 
 /* ----------------------------------- */   
   
 quickStart: function(el) {
   var id = $(el).up().id;
   this.active.top = true;
   this.display(id);
 },
 
 /* ----------------------------------- */   
   
 display: function(el) {   
  if (this.active.sub) return;
  if (el == this.selected) return;
  
  // Remove last element
  if (this.selected != '') {
    this.lowlight(this.selected);
    this.hideSubmenu(this.selected); 
  } 
  
  // Get Original Hightlight
  if (! this.menuChecked) {
    this.menuChecked = true;
    $$('#menu td').each(function(m){
      if ((m.className).toString().indexOf('-on') > -1) this.originalMenu = m.id;  
    }.bind(this)); 
  }
    
  // Highlight current menu
  if (this.originalMenu) this.lowlight(this.originalMenu);  
  this.highlight(el);
  
  // Set Selected Menu
  this.selected = el;
  
  // If submenu exists
  if ($('sub' + this.selected)) { 
    
    // Prevent flickering
    $(this.sub).setOpacity(1).setStyle({visibility:'hidden'}).show();
    $('sub' + this.selected).show();
      
    // Get Position
    var nb = parseInt(this.selected.replace(/menu/,''));
    var tdPos = $(this.selected).positionedOffset();
    
    // Set Position
    if (nb == 1) {
      // Left
      $(this.sub).style.left = tdPos.left -20 + 'px';      
    
    } else if(nb == this.maxElements) {
      // Right
       var pos = tdPos.left + $(this.selected).getWidth() - $('sub' + this.selected).getWidth();
      $(this.sub).style.left = pos +10 + 'px';
         
    } else {
      // Center          
      var pos = tdPos.left + ($(this.selected).getWidth() /2 ) - ($('sub' + this.selected).getWidth() /2);
      $(this.sub).style.left = pos + 'px';
    }
      
    // Start Sub Menu
    $(this.sub).setStyle({visibility:''}).hide();   
    if (Prototype.Browser.IE6) $(this.sub).show(); 
    else Effect.Appear(this.sub,{duration:0.6});        
  }
 },
 
 /* ----------------------------------- */ 
 
 stop: function(source) {  
  if (source.id == this.sub) this.active.sub = false;
  else this.active.top = false;  
  this.autoHide();
 },

 /* ----------------------------------- */  
    
 autoHide: function() {
   if (this.active.top || this.active.sub) {
    this.timer = 0; 
    return; 
   } 
        
   if(this.timer < this.timeBeforeAutoHide){
    this.timer += 100; 
    setTimeout('menu.autoHide()',99); 
   } else { 
    this.timer = 0; 
    this.hideSubmenu();     
   }  
 },

 /* ----------------------------------- */ 
 
 hideSubmenu: function() {
  if ($('sub' + this.selected)) {
    $('sub' + this.selected).hide(); 
    $(this.sub).hide();       
  }
  this.showOriginal();
  this.selected = '';
 },
 
 /* ----------------------------------- */ 

 activate: function(source) { 
  if (source.id == this.sub) this.active.sub = true;
  else this.active.top = true;  
 },
 
 /* ----------------------------------- */ 
 
 highlight: function(el) {
   $(el).className = $(el).className.replace(/-off/,'-on');
 },
 
/* ----------------------------------- */ 
 
 lowlight: function(el) {
   if (el == null || el == undefined || el == '') return;
   $(el).className = $(el).className.replace(/-on/,'-off'); 
 },
 
 /* ----------------------------------- */ 
 
 showOriginal: function() {  
    if (! this.active.top && ! this.active.sub) {
      this.lowlight(this.selected);
      if (this.originalMenu) this.highlight(this.originalMenu);  
    }
 }
 
 /* ----------------------------------- */  
 
};

/* --------------------------------------------------------------- */
