/**
 * @author Martin
 */
//Compile these functions while the document is loading
(function($){
	
	/* v1 Function
	$.fn.OpenSubMenu = function(){
		$(this).each(function(){
			
			var obj	=	$(this);
			var innerMenu	=	obj.find('ul');
			
			$(this).mouseenter(function(){
				innerMenu.delay(400).fadeTo(100,1);
				
			}).mouseleave(function(){
				innerMenu.stop().hide();
				
			});
		});
	};
	*/
	
	//v2
	$.OpenSubMenu = function(obj){
		
		//Variables declaration
		var listToOpen	=	obj.next();																	//Desired list
		var allLists		=	$("div#catalogMenu li.masterCategory ul");	//All lists
		var timedOut		=	'';																					//Variable to hold the timeout
		
		//Hide any opened lists
		allLists.stop().hide();
		
		//Open our list with a bit of delay
		listToOpen.delay(150).fadeTo(200,1);
		
		//On mouse leave set timeout to hide the list of not accessed
		obj.mouseleave(function(){
			timedOut	=	setTimeout(function(){
				listToOpen.hide();
			},500);
		});
		
		//If mouse enters the list, clear the timeout
		listToOpen.mouseenter(function(){
			clearTimeout(timedOut);
			timedOut	=	'';
			
		//If mouse leaves the list set a new timeout to hide the menu
		}).mouseleave(function(){
			timedOut	=	setTimeout(function(){
				listToOpen.hide();
				
				//Clear
				listToOpen.unbind();
				listToOpen	=	'';
				allLists		=	'';
				obj					=	'';
			},500);
		});
	};
})(jQuery);

$(function(){
//Body on load begins

	/* v1 CALL
		$("div#catalogMenu li.masterCategory").OpenSubMenu();
	*/
	
	//v2 assignment is via in the HTML <a onClick="...">

//End of Body on load
});

