/*
 * 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 || {});
		
        this.each(function(i1, thisDropMenu){ //for each dropmenu nav
            $('li', thisDropMenu).each(function(i2, thisLI){ //for each LI within the dropmenu nav
				$subnavULs = $('ul:first', thisLI); //get first subnav (UL) within this LI
				if ($subnavULs.length==0) return; //this LI has no subnav
	            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) {
						$.data(thisLI, 'hidden',false);
						$subnav = $(thisLI.subnav);

						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(thisLI.ShowSubnav, thisLI.HideSubnav);
			});
			
			$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 */
		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);
