jQuery(document).ready(function() {
	 //set up to use the $ as a short cut in wp
	var $ = jQuery.noConflict();
	
	//Set custom configurations
	var config = {
		 sensitivity: 10, // number = sensitivity threshold (must be 1 or higher)
		 interval: 100, // number = milliseconds for onMouseOver polling interval
		 over: megaHoverOver, // function = onMouseOver callback (REQUIRED)
		 timeout: 100, // number = milliseconds delay before onMouseOut
		 out: megaHoverOut // function = onMouseOut callback (REQUIRED)
	};
	
	//setup default css
	$('#nav').remove();	//hide the backup menu
	$('#mega').css('display', 'block'); //show the js menu, hidden with css
	//$("ul#topnav li  ul.sub").css({'opacity':'0'}); //Fade sub nav to 0 opacity on default
	$("ul#topnav li").hoverIntent(config); //Trigger Hover intent with custom configurations, got to be bound to the li not the li a
	
	
	//========On Hover Over========//
	function megaHoverOver(){	
		$(this).find('a.trigger').addClass('active');
		//find the parent li then look down the dom tree for the first occance of the ul.sub
		$(this).find("ul.sub").stop().fadeTo('fast', 1).show(); //Find sub and fade it in
	
		//function to get the max ul width from with the ul.sub
		(function($) {
			//Function to calculate total width of all ul's
			$.fn.calcSubWidth = function(){
				rowWidth = 0;
				//Calculate row
				$(this).find("ul.sub li ul").each(function() { //for each ul...
					rowWidth += $(this).outerWidth(); //Add each ul's width together
				});
			};
		})($); 
		//execute the function above on this object
		$(this).calcSubWidth(); 
		
		//make sure size isnt to big for screen
		if (rowWidth > 685) { rowWidth = 685;} 
		//console.log(rowWidth); 
		//apply the css to the fly out menu
		$(this).find("ul.sub").css({'width' : rowWidth}); 	
	}
	
	//========On Focus Over========//
	//bound to the a, so has to work up to the containing list item, then down from there
	function megaFocusOver(currentLink){
		//find the parent li then look down the dom tree for the first occance of the ul.sub
		$(currentLink).parents('li').find("ul.sub").stop().fadeTo('fast', 1).show(); //Find sub and fade it in
	
		//function to get the max ul width from with the ul.sub
		(function($) {
			//Function to calculate total width of all ul's
			$.fn.calcSubWidth = function(){
				rowWidth = 0;
				//Calculate row
				$(currentLink).parents('li').find("ul.sub li ul").each(function() { //for each ul...
					rowWidth += $(this).outerWidth(); //Add each ul's width together
				});
			};
		})($); 
		//execute the function above on this object
		$(currentLink).calcSubWidth(); 
		
		//make sure size isnt to big for screen
		if (rowWidth > 685) { rowWidth = 685;} 
		//apply the css to the fly out menu
		$(currentLink).parents('li').find("ul.sub").css({'width' : rowWidth}); 	
	}
	
	
	
	
	
	//========On Hover Out========//
	function megaHoverOut(){
	 	//face out the menu and hide
	 	 $(this).find(".sub").stop().fadeTo('fast', 0, function(){ $(this).hide(); });
		 //remove active from link class
		 $('ul#topnav li a.trigger').removeClass('active');
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	

	//========bind focus for keyboard tabbing over========//
	//must be bound to link (not list item)
   $("ul#topnav li a").bind("focus", function(){ megaFocusOver(this); });

	//========bind focus for keyboard tabbing out========//
   $("ul#topnav li ul.sub li ul li a:last, ul#topnav li ul.sub li a:last").bind("blur",function(){
		$('ul.sub').hide();
 	});

	//========Internet Explorer bind focus for keyboard tabbing over========//
   if($.browser.msie){
		$("ul#topnav li a").bind("focusin", function(){ megaFocusOver(this); });
		//========Internet Explorer bind focus for keyboard tabbing out========//
		$("ul#topnav li ul.sub li ul li a:last, ul#topnav li ul.sub li a:last").bind("focusout",function(){
			$('ul.sub').hide();
		});
   }
   
   	//========highlight top level  when sub if shown========//
	$('ul.sub').hover(
	  function(){
		  $(this).parent('ul#topnav li').stop().addClass('active');
		}, 
		function(){
			$(this).parent('ul#topnav li').stop().removeClass('active');
	});

	//========give each item a tabindex========//
	$(function(){
		var tabindex = 1;
		$('ul#topnav li').each(function() {
			var $input = $(this);
			$input.attr("tabindex", tabindex);
			tabindex++;
		});
	});
   
}); //document.ready()


//========hover intent plugin========//
/**
* hoverIntent r5 // 2007.03.27 // $ 1.1.2+
*/
(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);};})($);
