/**
 * Argstraphica's Pulldown Menu 	v1.00.1	//	14.12.2010	//	jQuery 1.4.2+
 * 
 * @about		Enables the use of .pullDown() and .subNav() functions
 * 				without needing to add a load of rubbish to the main js
 * 				file of a website.
 * 
 * @author		Mark Thompson
 * @copyright	Artsgraphica 2010+
 * @verNum		v1.00.1
 * @lastUpdate	14.12.2010
 * @dependency	REQUIRES jQuery hoverIntent plugin
 * 
 * 
 * 
 * * Version History
 * @history		v1.00.1	:	First Implementation setup and basic use enabled, setup of 'Javadoc' information up here!
 * 
 * 
 * 
 * * List of things to do, ideas and whatever else!
 * @TODO		Make the plugin auto class all it's descenders
 * @TODO		Make the plugin auto style all it's descenders
 * @TODO		Make the plugin auto fire off it's sub-items
 * 
 * 
 * 
 * * Things that would be nice, but are a bit big at the moment
 * @BIGTODO		Make the plugin independent of hoverIntent...
 * @BIGTODO		Minify the plugin once we are happy with a version
 * 
 * 
 * * List of available functions, and their defaults
 * @function	pullDown(delay = 300)
 * @function	subNav(delay = 0)
 * 
 * 
 */
(function($){
	/**
	 * Pulldown function
	 */
	$.fn.pullDown = function(options){
		// Set default timeout to 300ms
		var defaults = {
			delay:300
		}
		
		// Override with options as necessary
		var options = $.extend(defaults,options);
		
		// This will work on class, or id, so iterate
		return this.each(function(){
			// set obj
			obj = $(this);
			
			// Create the configuration HoverIntent is required for this plugin
			var config = {
				over:		openTopNav,
				timeout: 	options.delay,
				out:		closeTopNav
			};
			// Assign this to the object
			$(obj).hoverIntent(config);
			
			// Open Function
			function openTopNav(){
				// Find the first a and make 'active' so that is displays a hovered background
				$(this).find('a:first').addClass('active');
				// Fade in the ul child fast
				$(this).find('ul.depSubNav').fadeIn('fast');
			}

			// Close Function
			function closeTopNav(){
				// Now turn off the active class on the first a
				$(this).find('a:first').removeClass('active');
				// And fade out the ul child
				$(this).find('ul.depSubNav').fadeOut('fast');
			}
		});
	};
	
	/**
	 * This handles the sub nav items
	 */
	$.fn.subNav = function(options){
		// Set default timeout to 0ms (no hover delay)
		var defaults = {
				delay:0
		}
		
		// Override options as necessary
		var options = $.extend(defaults,options);
		
		// This will work on a calss, or id, so iterate
		return this.each(function(){
			// Set the obj
			obj = $(this);
			
			// Create a HoverIntent config
			var config = {
				over:		openSubNav,
				timeout:	options.delay,
				out:		closeSubNav
			};
			// Now assign it
			$(obj).hoverIntent(config);
			
			// Open Function
			function openSubNav(){
				// Find the first a and make 'active' so that is displays a hovered background
				$(this).find('a:first').addClass('active');
				// Fade in immediate descending ul child
				$(this).find('> ul.subSubNav').fadeIn('fast');
			}

			// Close Function
			function closeSubNav(){
				// Now turn off the active class on the first a
				$(this).find('a:first').removeClass('active');
				// Fade in immediate descending ul child
				$(this).find('> ul.subSubNav').fadeOut('fast');
			}
		});
	};
	
})(jQuery);
