/**
* 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)
* @return    The object (aka "this") that called hoverIntent, and the event 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=$.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);};})($j);

function removeBorder() {
	/* Remove bottom border from top anchor of hidden sub-navigation items. */
	$j("ul.nav_main_sub:hidden").parent().each(function(){
		$j(this).children().filter(":first-child").css("border-bottom", "none");
	});
}

function collapseMenus() {
    // hide everything except .always-open menu
    $j('ul.nav_main_sub').not('.always-open').slideUp(500);
    // mark current/show submenu
    $j('.current').removeClass('current');
}

$j(document).ready(function(){

	/* Mark "always open" sub-navigation item on page load (if a sub-navigation anchor id matches the body id, then add attribute class="always-open" to its parent li). */
	var section = $j('body').attr('id');
    var seg_1 = window.location.pathname.split('/')[1];
	$j('.nav_main_sub li a').each(function(){
		var ni_id = $j(this).attr('id');
		if (section == ni_id.substring(3) ||
            seg_1 == $j(this).parent().parent().prev().attr('href').replace(/\//g, '')) {
			$j(this).parent().parent().addClass('always-open');
		}
	});
    
	/* Hide all ul.nav_main_sub except for the one that contains a li with attribute class="always-open". */ 
	$j('ul.nav_main_sub').not('.always-open').hide();

	/* Make sure borders are not doubling or missing after page load. */
	removeBorder();
	$j('#nav_main').css('border-bottom', '1px solid #434e58');
	
    // add event handlers to the <a> prior to the menu
	$j('.accordian-menu > li > a').hoverIntent({
        sensitivity: 1, // number = sensitivity threshold (must be 1 or higher)    
        interval: 200, // number = milliseconds for onmouseover polling interval    
        timeout: 200, // number = milliseconds delay before onmouseout    
        over: function(e){
            // cancel event if the menu's already expanded
            // can happen if the user hovers over children and then returns to main link
            if ($j(this).is('.current')) {
                return false;
            }
            collapseMenus();
            $j(this)
                .addClass('current')
                .next().slideDown(500);
            setTimeout(removeBorder, 1820);
            return false;
        },
        out: function() {
            return false;
        }
    });
    
    // cancel hover events on submenus to prevent menu collapse
    $j('.nav_main_sub li').hover(
        function() {
            // ... but collapse if this isn't a child of the .current menu
            if (!$j(this).parent().prev().is('.current')) {
                collapseMenus();
            }
        },
        function() {return false;}
    );
    
});