/**
* 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:100,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);



// ////////////////////////////////////////////////////////////////////////////
// Navigation
// ////////////////////////////////////////////////////////////////////////////


// navigation properties and functions
var navigation = new Object();


/**
* 
* 
*/
navigation.initialize = function() {
	this.activeMenuItem = this.getActiveMenuItem();
	this.siteContainer = {
		element: $('#site-container'),
		offsetLeft: $('#site-container').offset().left
	};
	this.highlight = {
		element: $('#head-pri-highlight'),
		width: $('#head-pri-highlight').width(),
		height: '',
		maxHeight: 116,
		minHeight: 0
	};
	// add the highlight to the site-container for relative positioning
	this.siteContainer.element.prepend(this.highlight.element);
	this.maxMenuHeight = parseInt( $('#head-pri').css("height") );
	this.minMenuHeight = parseInt( $('#head-pri-wrapper').css("height") );
	
	var showMenu = function(event){ 
		navigation.openMenu(navigation.maxMenuHeight);	
	};
	
	var hideMenu = function(event){ 
		navigation.closeMenu(navigation.minMenuHeight);
	};
	
	var handleHighlightAnimation = function(event){
		//if mouseout close the highlight
		if(event.type == 'mouseout') {
			navigation.highlight.element.animate({'height': '0px'}, 300);
		}
		
		//if mouseover check to see if the highlight is open, if so, close it, load the new location and open it
		if(event.type == 'mouseover') {	
			//get the anchor
			var anchorElm = $($('a:first',this).get(0));
									 
			//clear the effect queue to be current with the mouse position
			navigation.highlight.element.queue( [ ] ).stop();
			
			//move the highlight to the left
			//if(navigation.highlight.element.height() == 0) navigation.highlight.element.animate( {'height': navigation.highlight.maxHeight}, {queue:true, duration:100} );
			//navigation.highlight.element.animate( {'left': navigation.loadHighlight(anchorElm, {loadFor: 'animation'}).element.css('left')}, {queue:true, duration:50} )
			
			//close the highlight, then load the new location, then open the highlight
			navigation.highlight.element.animate( {height: '0px', opacity:0}, {queue:true, duration:70, complete: function(){ navigation.loadHighlight(anchorElm, {loadFor: 'animation'}); } })
				.animate( {'height': navigation.highlight.maxHeight, opacity:1.00}, {queue:true, duration:200} );
		}
	};

	// hoverIntent attaches event listeners
	$( '#head-pri' ).hoverIntent( showMenu , hideMenu );
	
	// attach event listeners to individual nav items to handle highlight animation only if you're on the homepage
	if(!this.activeMenuItem) {
		$('#head-pri .primary-links > li').hoverIntent( handleHighlightAnimation, handleHighlightAnimation );
	}
	
	// if you're on an interior page, just show the highlight without animation
	if(this.activeMenuItem) this.showHighlight(this.activeMenuItem);
};

navigation.getActiveMenuItem = function(){
	var o = $('#head-pri ul.primary-links > li > a.pri-active');
	return (o.length > 0) ? o : '';
};

navigation.loadHighlight = function(anchorElm, opts){
	
	if(!opts) opts = {};
	
	if(anchorElm) {
		/* FOR MIDDLE ALIGNING THE 3 STRIPES
		 * get the STRONG child to base the positioning of the 3 strips
		 * the A tag is used for offset calculations, so the A has to be passed in
		 * using .css('width') so the position the 3 stripes can be changed via style.css
		 */
		 //var pos = (parseInt($('strong',base).css('width')) - this.highlight.width)  / 2;	
		 //var left = (base.offset().left - this.siteContainer.offset().left) + pos;
		
		/* FOR LEFT ALIGNING THE 3 STRIPES
		 *
		 */
		var left = (anchorElm.offset().left - parseInt(this.siteContainer.element.offset().left));
		var height = (opts.loadFor == 'static') ? this.highlight.maxHeight : this.highlight.element.css('height');
		this.highlight.element.css('height',height);
		this.highlight.element.css('left',left);
	}
	
	return this.highlight;
};

navigation.showHighlight = function(){
	this.loadHighlight(this.activeMenuItem, {loadFor: 'static'});
};

navigation.openMenu = function(maxMenuHeight){ 
	if(!maxMenuHeight) maxMenuHeight = this.maxMenuHeight;
	$('#head-pri-wrapper').addClass('open'); $('#head-pri-wrapper').animate({ height: maxMenuHeight }, 200); 
};

navigation.closeMenu = function(minMenuHeight){
	if(!minMenuHeight) minMenuHeight = this.minMenuHeight;
	$('#head-pri-wrapper').removeClass('open'); $('#head-pri-wrapper').animate({ height: minMenuHeight }, 200);
};

$(document).ready(function(){
	// init primary navigation
	navigation.initialize();
});