var jq$ = jQuery.noConflict();  // don't use j$ -- conflicts with jQuery reference on the map pages

function setCurrentDropDownItem() {
    jq$("ul.menuwidth").children(".currentDropDownMenuItem").find("ul:first").css({ visibility: "visible", display: "none" }).slideDown(200).show();  // show the currently selected menu
}

function showSelectedMenu() {
	var anyWhite = false;
	jq$("li.notCurrentDropDownMenuItem a.dir, li.notCurrentDropDownMenuItem a.nondir, li.notCurrentDropDownMenuItem a.nondirlast").each(  // loop through each non-current menu item
		function(idx) {
			//alert(idx + ': ' + jq$(this).text() + ' color: ' + jq$(this).css("color"));  // for troubleshooting
			if (jq$(this).css("color") === "#ffffff"
				|| jq$(this).css("color") === "rgb(255, 255, 255)") {  // examine the color of the menu item - is it white (i.e. user hovering over it)?
				anyWhite = true;  // yes - user is hovering over an item
			}
		}
	);	
	if (anyWhite === false) {
		setCurrentDropDownItem();  // No white "notCurrentDropDownMenuItem" elements (i.e. user is not hovering over any other menu item), so show the currently selected submenu
	}
}

function showMenu() {
	jq$(this).find("ul:first").css({ visibility: "visible", display: "none" }).slideDown(400).show();  // show the submenu for the item you're hovering over
    jq$(this).parent().children(".currentDropDownMenuItem").find("ul:first").css({ visibility: "hidden", display: "none" });  // hide the submenu for the currently selected item
}

function hideMenu() {
    jq$(this).find("ul:first").css({ visibility: "hidden", display: "none" });  // hide the submenu item you were hovering over
	setTimeout(showSelectedMenu, 100);  // slight delay (give ui a chance to color the menu item properly), then execute the function to determine if we need to re-show the currently selected submenu
}

var _hoverConfig = {
     over: showMenu, // function = onMouseOver callback    
     timeout: 500,   // number = milliseconds delay before onMouseOut    
     out: hideMenu   // function = onMouseOut callback
};

function setHovering() {
	jq$("ul.menuwidth li.notCurrentDropDownMenuItem").hoverIntent( _hoverConfig );  // set the hovering behavior of the un-selected menu items
}

function setOnClickBehavior() {
    jq$("ul.menuwidth li.currentDropDownMenuItem").removeClass('currentDropDownMenuItem').addClass('currentDropDownMenuItem');
    jq$("ul.menuwidth a").click(function () {
        jq$("#nav").find("a").css({ color: "#98a3ab" });
        jq$(this).addClass('.currentDropDownMenuItem');
        jq$(this).css({ color: "#ffffff" });
        if(!jq$(this).closest("ul").is("#nav")) {
            jq$(this).parent().parent().parent().find('a:first').css({ color: "#ffffff" });
		}
        jq$(this).unbind('mouseenter mouseleave');
        jq$("ul.menuwidth li").unbind('mouseenter mouseleave');
    });
}

jq$(document).ready(function () {
    setCurrentDropDownItem();
    setHovering();
    setOnClickBehavior();
});

/**
* hoverIntent r5 // 2007.03.27 // jQuery 1.1.2+
* <http://cherne.net/brian/resources/jquery.hoverIntent.html>
* 
* @param  f  onMouseOver function || An object with configuration options
* @param  g  onMouseOut function  || Nothing (use configuration options object)
* @author    Brian Cherne <brian@cherne.net>
*/
(function($){$.fn.hoverIntent=function(f,g){var cfg={sensitivity:7,interval:100,timeout:0};cfg=$.extend(cfg,g?{over:f,out:g}:f);var cX,cY,pX,pY;var track=function(ev){cX=ev.pageX;cY=ev.pageY;};var compare=function(ev,ob){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t);if((Math.abs(pX-cX)+Math.abs(pY-cY))<cfg.sensitivity){$(ob).unbind("mousemove",track);ob.hoverIntent_s=1;return cfg.over.apply(ob,[ev]);}else{pX=cX;pY=cY;ob.hoverIntent_t=setTimeout(function(){compare(ev,ob);},cfg.interval);}};var delay=function(ev,ob){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t);ob.hoverIntent_s=0;return cfg.out.apply(ob,[ev]);};var handleHover=function(e){var p=(e.type=="mouseover"?e.fromElement:e.toElement)||e.relatedTarget;while(p&&p!=this){try{p=p.parentNode;}catch(e){p=this;}}if(p==this){return false;}var ev=jQuery.extend({},e);var ob=this;if(ob.hoverIntent_t){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t);}if(e.type=="mouseover"){pX=ev.pageX;pY=ev.pageY;$(ob).bind("mousemove",track);if(ob.hoverIntent_s!=1){ob.hoverIntent_t=setTimeout(function(){compare(ev,ob);},cfg.interval);}}else{$(ob).unbind("mousemove",track);if(ob.hoverIntent_s==1){ob.hoverIntent_t=setTimeout(function(){delay(ev,ob);},cfg.timeout);}}};return this.mouseover(handleHover).mouseout(handleHover);};})(jQuery);



