(function($){
	$.fn.dropDown = function(options) {

	// Set default options
	var defaults = {
		speed: 'fast',
		active_class: 'active',
		use_hoverIntent: true,
		hoverIntent: {
			interval: 20,
			timeout: 200,
			sensitivity: 7
		}
	};
  
  // Merge default options with those supplied
	var options = $.extend(defaults, options);
	
	// Iterate over top level anchors
	$(this).find('> li > a').each(function() {
	
		var anchor = $(this);
		
		 // FIRST TIER
		if(anchor.parent().find('ul').length > 0) {
		
			// Get sub-menu object
			var sub_menu = anchor.parent().find('> ul');
			var sub_menu_width = (anchor.parent().outerWidth() - 2);
			
			sub_menu.css({
				'width':sub_menu_width,
				'left':0
			});
			
			// Setup first tier sub-menu interactivity
			setup_menu(sub_menu, anchor);
			
			// SECOND TIER
			if(sub_menu.find('> li > a').length > 0) { 
			
				sub_menu.find('> li > a').each(function() {
				
					var anchor = $(this);
					
					if(anchor.parent().find('ul').length > 0) { // anchor has a sub-menu
						var sub_menu = anchor.parent().find('ul');
								setup_menu(sub_menu, anchor);
								sub_menu.css({
									'left':(sub_menu_width + 2)
								});
					}
				
				});
				
			}
		
		}
	
	});


	function setup_menu(sub_menu, anchor) {
		
		// Hide sub-menu	
		sub_menu.hide();
		
		// Check if hoverIntent is available for use
		check_hoverIntent();
		
		// Add has_sub_menu class to parent for styling
		anchor.parent().addClass('has_sub_menu');
		
		// Function to show menu
		var show_menu = function() {
			sub_menu.fadeIn(options.speed);
			anchor.addClass(options.active_class);
		}

		// Function to hide menu
		var hide_menu = function() {
			sub_menu.fadeOut(options.speed);
			anchor.removeClass(options.active_class);
		}
		
		// Add hover event to anchor...
		if(options.use_hoverIntent) {
			// ... using hoverIntent() 
			var hoverIntentOverOut = { over: show_menu, out: hide_menu };				
			var hoverIntentOptions = $.extend(options.hoverIntent, hoverIntentOverOut);			
			anchor.parent().hoverIntent(hoverIntentOptions);
		} else {
			// ... using jQuery hover()
			anchor.parent().hover(show_menu, hide_menu);
		}
			
	}
	
	// Function to check if hoverIntent is available and disable if not	
	function check_hoverIntent() {
		if(jQuery().hoverIntent) {
			return true;
		} else {
			options.use_hoverIntent = false;
			alert('The jQuery dropDown plugin is configured to use the the hoverIntent, however it is currently unavailable. To use hoverIntent with the dropDown plugin, please download from...\n\n http://cherne.net/brian/resources/jquery.hoverIntent.html.\n\nAlternatively, set "use_hoverIntent: false" in the dropDown configuration.');
			return false;
		}
	}

 };
})(jQuery);
	
