var menu = new function() {
	this.slide_speed = 200;
	this.timer_delay = 1000;  //time it takes the main menu to close automatically after the mouse moves away
	
	this.menu_selector = "#menu-main-menu";
	this.q = [];
	this.timer = 0;
	this.items = [];
	
	this.setup = function() {
		var children = jQuery(this.menu_selector).children('li');
		
		for(var i=0; i<children.length; i++) {
			var menu_item = jQuery(children[i]);
			
			menu_item.hover(
								function() { 
									menu.queue(jQuery(this), false); 
								}, 
								function() { 
									menu.queue(jQuery(this), true); 
								} 
							);	
			this.items.push(menu_item);
		}
	};
	
	this.restart = function() {
		clearInterval(this.timer);
		this.timer = setInterval("menu.tick()", this.timer_delay);
	};
	
	this.queue = function(menu_item, hide) {
		if (hide) {
			var found = false;
			for(var j=0; j<this.q.length; j++) {
				if (this.q[j].attr('id') == menu_item.attr('id')) {
					found = true;	
					break;
				}
			}
			if (!found) {
				this.q.push(menu_item);
			}
			this.restart();
		} else {
			if (menu_item.children('ul').length > 0) {				
				menu_item.children('ul').slideDown(this.slide_speed);
				
				this.close_all(menu_item);
				
				this.q = [];
			}
		}
	};

	this.close_menu = function(menu_item) {
		menu_item.children('ul').slideUp(this.slide_speed);	
	}

	this.close_all = function(menu_item) {
		for( var i=0; i<this.items.length; i++) {
			var cur_item = this.items[i];
			if (cur_item.attr('id') != menu_item.attr('id')) {
				this.close_menu(cur_item);
			}
		}
	};

	this.tick = function() {
		for(var i=0; i<this.q.length; i++) {
			this.close_menu(this.q[i]);
		}
		this.q = new Array();
	};
};
jQuery(document).ready(function() {
	menu.setup();								
});
