function dropdown_setup_hover_event( element, duration, listener ) {
	return new HoverEvent( element, duration, function() { listener.show( element ); } );
}

function dropdown_set_timeout( listener ) {
	return window.setTimeout( function() { listener.hide( listener.active, true ); }, listener.timeoutduration );
}

function dropdown_setup_timeout_events( listener ) {
	Event.observe( $(listener.active+"-dropdown"), "mouseout", function(event) { listener.startTimeout(); } );
	Event.observe( $(listener.active+"-dropdown"), "mouseover", function(event) { listener.stopTimeout(); } );
	Event.observe( $(listener.active), "mouseover", function(event) { listener.stopTimeout(); } );	
	Event.observe( $(listener.active), "mouseout", function(event) { listener.startTimeout(); } );	
}

function dropdown_remove_timeout_events( listener ) {
	Event.stopObserving( $(listener.active+"-dropdown"), "mouseout", function(event) { listener.startTimeout(); } );
	Event.stopObserving( $(listener.active+"-dropdown"), "mouseover", function(event) { listener.stopTimeout(); } );
	Event.stopObserving( $(listener.active), "mouseover", function(event) { listener.stopTimeout(); } );
}

function dropdown_showmenu( element ) {
  id = element.parentNode.id;
  	
  	if (this.active == id) return;
    if (this.active != null) this.hide( this.active, false );
  	
  	if($(id+"-dropdown")) {  
	  
  	this.active = id;
	
  	var button = $(id);
  	var menu = $(id+"-dropdown");
  	thewidth = button.offsetWidth;
  	if (window.XMLHttpRequest) { // if not ie6 use min-width else use width
        menu.style.minWidth = button.offsetWidth-16+"px";
    } else {
  	    menu.style.width = button.offsetWidth-16+"px";
    }
  	menu.style.zIndex="200";
  	menu.setOpacity(1.0);
  	menu.style.left = (Position.cumulativeOffset(button)[0] + this.offsetx) + "px";
  	menu.style.top = (Position.cumulativeOffset(button)[1] + this.offsety) + "px";
  	menu.style.display='block';
	
  	dropdown_setup_timeout_events( this );
	
  	Element.addClassName(button, button.id+"-active");
  }
}

function dropdown_hidemenu( id, grace ) {
	var button = $(id);
	
	if($(id+"-dropdown")) {
  	var menu = $(id+"-dropdown");
	
  	if (grace) {
  		new Effect.Fade( menu, {duration: 0.25} );
  	} else {
  		menu.hide();
  	}
	
  	dropdown_remove_timeout_events( this );
	
  	Element.removeClassName( button, button.id+"-active" );
	
  	if (this.active == id) this.active = null;
  }
}

function dropdown_timeout_start() {
	if (this.timeout != null) return;
	this.timeout = dropdown_set_timeout( this );
}

function dropdown_timeout_stop() {
	if (this.timeout != null) window.clearTimeout(this.timeout);
	this.timeout = null;
}

function DropDown( element, duration, offsetx, offsety ) {
  
	this.element = $(element);
	this.duration = duration;
	this.offsetx = offsetx;
	this.offsety = offsety;
	
	this.show = dropdown_showmenu;
	this.hide = dropdown_hidemenu;
	
	this.active = null;
	
	this.timeout = null;
	this.timeoutduration = 1000;
	this.startTimeout = dropdown_timeout_start;
	this.stopTimeout = dropdown_timeout_stop;
	
	var anchors = this.element.getElementsByTagName("A");
	for (var i=0; i<anchors.length; i++) {
		if (Element.hasClassName(anchors[i], "dropdown-link")) {
			dropdown_setup_hover_event( anchors[i], this.duration, this );
		}
	}
}

Event.observe(window, 'load', function () { 
  new DropDown( $('main-nav-container'), 200, 0, 31 ); 
});

