/******************/
/* Menu functions */
/******************/
window.Menu = {
	delay		: 600,
	timer		: null,
	menuitem	: null,
	
	/**
	 * apply
	 * @param	string	selector
	 */
	apply: function( selector ) {
		$(selector).hover(Menu.open, Menu.setTimer);
		$(document).click(Menu.close);
	},
	
	/** 
	 * cancelTimer
	 */
	cancelTimer: function() {
		if(Menu.timer)	{
			clearTimeout(Menu.timer);
     		Menu.timer = null;
		}
	},
	
	/**
	 * setTimer
	 */
	setTimer: function() {
		Menu.timer = window.setTimeout(Menu.close, Menu.delay);
	},
		
	/** 
	 * close
	 * @param	string	current_menu_id
	 */
	close: function( current_menu_id ) {
		if(Menu.menuitem)	{
			if(Menu.menuitem.data("menuID") != current_menu_id)	
			{
				$("ul", Menu.menuitem).css({ height: 'auto', zIndex: "" }).stop().slideUp();
				$(Menu.menuitem).removeClass("open");
			}
		}
	},
			
	/** 
	 * open
	 */
	open: function() {
		current_menu = $(this);
		
		current_menu.addClass("open");
		
		// uniek menu id per submenu, dit om bij het sluiten te checken of niet de actieve wordt gesloten
		if(!current_menu.data("menuID"))	{
			current_menu.data("menuID", (Math.random() +''+ Math.random()).replace(/\./g,""))
		}
		
		Menu.cancelTimer();
		Menu.close( current_menu.data("menuID") );
		Menu.menuitem = current_menu;
		
		$("ul", Menu.menuitem).css({ height: 'auto', zIndex: 100 }).stop().slideDown();	
	}

};

/*****************************************************************/
/* Functie om hele blokken klikbaar te maken, en hover toevoegen */
/*****************************************************************/
$.fn.hoverClick = function()
{
	this.each(function()
	{
		if($("a:first", this).length)
		{
			$(this).hover(
				function() { $(this).addClass("hover").css("cursor", "pointer"); },
				function() { $(this).removeClass("hover").css("cursor", "pointer"); }
			);
			
			$(this).attr("title", $("a:first", this).attr("title"));
			
			$(this).click(function(){
				window.location = $("a:first", this).attr("href");
			});
		}
	});
	
	return this;
};
