/*
 * initialize dropmenu on the outer-most UL that contains all nested LI's and UL's.
 * options are listed and described at bottom of this doc
 */
(function($){
    $.fn.dropmenu = function(options){
        options = $.extend($.fn.dropmenu.defaults, options || {});
		
		//these vars are for options.subnavHideDelay
		var hideDelayTimer = null;
		var hideDelayElem = null;
		
        this.each(function(i1, thisDropMenu){ //for each dropmenu nav
            $('li', thisDropMenu).each(function(i2, thisLI){ //for each LI within the dropmenu nav
				var $subnavULs = $('ul:first', thisLI); //get first subnav (UL) within this LI
				if ($subnavULs.length==0){ //this LI has no subnav
					$(thisLI).mouseenter(function(){
						if (hideDelayTimer) { //clear any old delay timer
							clearTimeout(hideDelayTimer);
							hideDelayTimer = null;
							if (hideDelayElem != thisLI) {
								hideDelayElem.HideSubnav();
							}
						}
						hideDelayElem = null;
					});
					return;
				}
	            thisLI.subnav = $subnavULs[0];

	            thisLI.HideSubnav = function(){
					$(thisLI.subnav).hide();
					$.data(thisLI, 'hidden',true); // helps fix animations
	            }
				
				thisLI.HideSubnavIfNecessary = function() {
					if ($.data(thisLI,'hidden')) { 
						thisLI.HideSubnav();
					} 
				}
				
	            thisLI.ShowSubnav = function(){
					if(typeof($.data(thisLI, 'hidden'))=='undefined' || $.data(thisLI, 'hidden')==true) {
						var $subnav = $(thisLI.subnav);
						
						if (!$subnav.is(":animated")) { //dont get stuck in an animation show/hide loop
							$.data(thisLI, 'hidden',false);
							if (options.animation == 'fade' && (options.subnavOpacity == null || options.subnavOpacity >= 100 || options.subnavOpacity < 0)) { //fade no opacity
								$subnav.fadeIn(options.animationDuration, thisLI.HideSubnavIfNecessary);
							}
							else if (options.animation == 'fade') { //fade with opactiy
								$subnav.css({
									'opacity': 0,
									'display': 'block'
								}).animate({
									opacity: options.subnavOpacity / 100
								}, options.animationDuration);
							}
							else { //if (options.animation == 'slide') {
								if (options.subnavOpacity != null && options.subnavOpacity < 100 && options.subnavOpacity >= 0) {
									$subnav.css({
										'opacity': options.subnavOpacity / 100
									});
								}
								$subnav.slideDown(options.animationDuration, thisLI.HideSubnavIfNecessary);
							}
						}
					}
	            }
				
				$(thisLI).hover(
					function(){ //hover over
						if (options.subnavHideDelay) {
							if (hideDelayTimer) { //clear any old delay timer
								clearTimeout(hideDelayTimer);
								hideDelayTimer = null;
								if (hideDelayElem != thisLI) {
									hideDelayElem.HideSubnav();
								}
							}
							hideDelayElem = null;
						}
						thisLI.ShowSubnav();
					},
					function(){ //hover out
						if (options.subnavHideDelay) {
							if(hideDelayTimer){ //clear any old delay timer
								clearTimeout(hideDelayTimer);
								hideDelayTimer = null;
								if (hideDelayElem != thisLI) {
									hideDelayElem.HideSubnav();
								}
							}
							
							//set the new delay timer
							hideDelayElem = thisLI;
							hideDelayTimer = setTimeout(function(){
								clearTimeout(hideDelayTimer);
								hideDelayTimer = null;
								hideDelayElem.HideSubnav();
							}, options.subnavHideDelay);
						}
						else {
							thisLI.HideSubnav();
						}
					});
			});
			
			var $allSubnavs = $('li ul', thisDropMenu); //any UL within an LI is a subnav
            $('a:first', $allSubnavs.parent()).addClass(options.containsSubMenuCssClass); //add class to the <a> of any LI that contain a subnav
        });
    };
    
    $.fn.dropmenu.defaults = {
		animation: "slide", 						/* available animations: "slide", "fade" (for no animation, set animationDuration to 0). note "fade" with opacity < 100 may not work for subnavs more than 1 level deep in IE. slide with 100 opacity seems to work for multiple levels */
        animationDuration: 175,						/* speed to run the animation when showing a subnav. 0 with any animation should be instant */
		subnavOpacity: null,						/* range 0 to 100, or null. NOTE: null==100, which means no opacity is ever set!!! CSS opacity wins if this is 100 or null. also this is useful when IE fucks up and wont display subnavs more than 1 level deep */
		subnavHideDelay: 0,						/* milliseconds to wait before hiding the subnav */
		containsSubMenuCssClass: "containsSubMenu"	/* class to the <a> of any LI that contain a subnav. this can be used for arrows or some sort of indicator of a nested navigation */
    };
})(jQuery);

