(function($) {

	
	var subMenuOpenedOnPageLoad = false;
	var opened_tab;

    $.fn.nmcDropDown = function(options) {
        // build main options before element iteration
        var opts = $.extend({}, $.fn.nmcDropDown.defaults, options);

        // iterate each matched element
        return this.each(function() {
            var menu = $(this);
            submenus = menu.children('li:has('+opts.submenu_selector+')');
            
            if (opts.fix_IE) {
                // Fix IE 6+7 z-index bug
                menu.css('z-index', 51)
                    .parents().each(function(i) {
                        if ($(this).css('position') == 'relative') {
                            $(this).css('z-index', (i + 52));
                        }
                    });
                submenus.children(opts.submenu_selector).css('z-index', 50);
            }
			
            // Function that is called to show the submenu
            over = function() {
				
				//check if hovered element equals the pre loaded submenu/tab to prevent it from disappearing on hover
				if($(this).find("a").html() == opened_tab){}else{
				
					//check if hover element has a sub menu
	            	var thisMenuHasChildren = $(this).find("span").children().html();
					if(thisMenuHasChildren != null){
						
						// if hovered element has a submenu then change size of menu height to 70px
						$('#menu').height(60);
						
						// hide pre loaded submenu
						$('ul#topnav li#active').children('span').animate(opts.hide, opts.hide_speed);
						
						//add class to currently hover element and show submenu
	                	$(this).addClass(opts.active_class).children(opts.submenu_selector).animate(opts.show, opts.show_speed);
	                       
	                }else{
	                	
	                	//if a preloaded submenu is not detected, keep menu height at 35px else 70px
	                	if(subMenuOpenedOnPageLoad != true){
	                		//$('#menu').height(35);
	                		$('#menu').height(25);
	                	}else{
	                		//$('#menu').height(70);
	                		$('#menu').height(60);
	                	}
	                	
	                }
                }
                
                return false;
                
            }
			
            // Function that is called to hide the submenu
            out = function() {
           
           	//check if hovered element equals the pre loaded submenu/tab to prevent it from disappearing on hover
            if($(this).find("a").html() == opened_tab){}else{	
            	
            	//if no submenu preloaded keep menu height at 35 px and hide any opened submenus
            	if(subMenuOpenedOnPageLoad != true){
            		/*$('#menu').height(35);*/
            		$('#menu').height(25);
                	$(this).removeClass(opts.active_class).children(opts.submenu_selector).animate(opts.hide, opts.hide_speed);
                }else{
                	//if a submenu was preloaded fade in preloaded menu and hide currently opened submenu, maintain menu height of 70px;
                	$(this).removeClass(opts.active_class).children(opts.submenu_selector).animate(opts.hide, opts.hide_speed);
                	$('ul#topnav li#active').children('span').animate(opts.show, opts.show_speed);
                	/* $('#menu').height(70); */	
                	$('#menu').height(60);	
                }
             }
                
                return false;
            }
            
       			
            // Show and hide the sub-menus
            if (opts.trigger == 'click') {
                submenus
                    .toggle(over, out)
                    .children(opts.submenu_selector).hide();
            } else {
                submenus
                    .hover(over, out)
                    .children(opts.submenu_selector).hide();
            }
            
        });
    };

    // Default options
    $.fn.nmcDropDown.defaults = {
        trigger: 'hover',           // Event to show and hide sub-menu - hover or click
        active_class: 'open',       // Class to give open menu items
        submenu_selector: 'span',     // The element immediately below the <li> containing the sub-menu
        show: {opacity: 'show'},    // Effect(s) to use when showing the sub-menu
        //show_speed: 100,            // Speed of the show transition
        show_speed: 5,            // Speed of the show transition
        show_delay: 5,             // Delay before the sub-menu is show (requires HoverIntent)
        hide: {opacity: 'hide'},    // Effect(s) to use when hiding the sub-menu
        //hide_speed: 0,            // Speed of the hide transition
        hide_speed: 1,            // Speed of the hide transition
        //hide_delay: 0,            // Delay before the sub-menu is hidden (requires HoverIntent)
        hide_delay: 1,            // Delay before the sub-menu is hidden (requires HoverIntent)
        fix_IE: true                // IE 6 and 7 have problems with z-indexes. This tries to fix them
    };
    
    
     //function called by nav.php to preload any submenu
     $.fn.loadNavBarForSection = function(){
    	
    	//check nav for active submenu
        var menu = $(this);
        submenus = menu.children('li#active');
        			
        //search active menu element for children to indicate a submenu
    	var hasChildren = $(submenus).find("span").children().html();
    	
    	//if something exists
		if(hasChildren != null){
			
			//set opened tab variable equal to id of element with opened submenu
			opened_tab = $(submenus).find("a").html();
			
			//set initial menu height
			$('#menu').height(60);
			
			//color the element to show selection
			$('ul#topnav li#active').css({'background':'#1376c9 repeat-x'});
			
			//add class to color submenu bg and open it
        	$(submenus).addClass('open')
               .children('span').animate({opacity: 'show'}, 100);
            $('ul#topnav li#active span').find("a").each(function(){
            	var anchor = $(this).attr("href");            	
            	if(anchor == "/?"+jQuery.url.attr("query") || anchor == jQuery.url.attr("path")){
            		$(this).css({'text-decoration':'underline'});
            		//alert("YES");
            	}
            });
            //set preloaded submenu var to true
            subMenuOpenedOnPageLoad = true;
        }else{
        	
        	//if no children detected highlight active element to show selection
        	$('ul#topnav li#active').css({'background':'#1376c9 repeat-x'});
        	
        	//if no submenu has been opened
        	if(subMenuOpenedOnPageLoad != true){
        	$('#menu').height(25);
        	}else{
        		$(submenus).addClass('open')
               .children('span').animate({opacity: 'show'}, 100);
        	}
        }

    };

})(jQuery);

