/*                                    */
/* Hover                              */
/*                                    */


var Hover = {
  EXIT_DELAY  : 600,
  
  lastTimer   : null,
  lastCommand : null,
  inhibit     : false,
  
  clearCurrent: function(exec) {
    if(!this.lastTimer) return
    clearTimeout(this.lastTimer)
    if (exec) eval(this.lastCommand)
    this.lastTimer = this.lastCommand = null
  },
  
	endWith: function(command) {
    if(this.inhibit) return
    this.lastCommand = command
    this.lastTimer = setTimeout(command, this.EXIT_DELAY)
  },
  
  toggle: function(on, txt, offset) {
    if(this.inhibit) return
    
   	if(on) {
   	  if(!$('info'))
   	    new Insertion.Top($('body'), '<div onmouseover="menu.title.keep();" onmouseout="menu.title.end(true);" id="info" style="z-index: 100; left: '+(offset[0]+110)+'px; top: '+(offset[1]+20)+'px";>'+txt+'</div>')
   	} else {
   	  if($('info'))
   	    Element.remove('info');
   	}
  }
}


/*                                    */
/* Menu                               */
/*                                    */

var menu = {
  
  cache: null,
  
  toggle: function(link) {          
    if (this.cache) {
      Element.hide(this.cache+'_sub');
      Element.removeClassName(this.cache, 'active');    
    }  
    
    if (this.cache != link.id) {
      this.cache = link.id    
      Element.addClassName(link, 'active');    
      Element.show(link.id + '_sub');
    } else {
      this.cache = null;
    }
	},
	
	title: {	  
	  begin: function(link, txt) {
	    Hover.clearCurrent(true);
	    var offset = Position.cumulativeOffset(link);
	    Hover.toggle(true, txt, offset);	    
	  },
	  
	  keep: function() {
		  Hover.clearCurrent()
		},
	  
	  end: function(delay) {
      if (delay) Hover.endWith("menu.title.end()")
      else Hover.toggle(false); 
	  }	  
	}
}

/*                                    */
/* Menu                               */
/*                                    */

var test = {
	hover: {
		begin: function(id) {	  
			Hover.clearCurrent(true)
			Hover.toggle(true,id)
			Element.addClassName(id+'_container', 'current')
		},
		
		keep: function(id) {
		  Hover.clearCurrent()
		},
		
		end: function(id, delay) {
			if (delay) Hover.endWith("menu.hover.end('"+ id +"')")
      else {
        Hover.toggle(false, id)
        Element.removeClassName(id+'_container', 'current')
      }
		}
	}
}