
/*########################################################*/
/*-------------- topnav --------------------*/
(function($)
{
    $.fn.initTopNav = function()
    {
		$.gtaaTopNav = this;
		
		$navWrapper = $("div.navigation"); //ul.site-sections li.nav-item").hover($.openFlyout, closeFlyout);
		$tabsWrapper = $navWrapper.find("ul.site-sections");
		$navItems = $tabsWrapper.find("li.nav-item");
	
		//init display
		this.useCufon = $.browser.msie ? false : true;
		//set the font size to defeat wrapping
		var maxHeight = 135; //135 - height when all nav items fit on one line (can't use width because blueprint constrains it) 
		var minFontSize = 11;
		var curFontSize = parseInt($navItems.css("font-size"));
		var maxLoops = curFontSize - minFontSize;
		var loopCount = 0;
		while (($navWrapper.outerHeight() > maxHeight) && (loopCount < maxLoops)) {
			this.useCufon = false; //kill cufon since we're shrinking the fonts
			$navItems.css("font-size", (--curFontSize + "px"));
			loopCount++;
		}
		if (this.useCufon == true) {
			Cufon.replace($navItems.find('div.item-middle a'), {
				hover: true
			});
		}
		
		//events
		$navItems.hoverIntent({
			sensitivity: 9, // number = sensitivity threshold (must be 1 or higher)
			interval: 25,   // number = milliseconds of polling interval
			over: function() { $(this).openFlyout(); },  // function = onMouseOver callback (required)
			timeout: 200,   // number = milliseconds delay before onMouseOut function call
			out: function() { $(this).closeFlyout(); }  // function = onMouseOut callback (required)
		});
		
		
		//for menus with promo images
		$tabsWrapper.find("div.flyout").each(
			function () {
				$this = $(this);
				if ($this.find("li.promo-image").length > 0) {
					$this.addClass("promo-image-flyout");
				}
			}
		);
		
		//ie7 - set width manually on colored lines under nav items (CSS doesn't work)
		if ($.isIe7()) {
			$tabsWrapper.find("div.line").each(
				function () {
					$(this).width($(this).parent().width());
				}
			);
		}

		//bounds to nav items
		$.fn.openFlyout = function() {
			$navItems.each(function() {
				$(this).closeFlyout();
			});
			
			var $elem = $(this);
			
			//determine if we place it left/right
			var topnavElem = $("div.topnav");
			var flyout = $elem.find("div.flyout");
			var targetTop = topnavElem.offset().top + topnavElem.outerHeight();
			var targetLeft = $elem.offset().left;
			var topnavRight = topnavElem.offset().left + topnavElem.outerWidth();
			var isPastEdge = (targetLeft + flyout.outerWidth()) > topnavRight;
			if (isPastEdge) {
				targetLeft -= (flyout.outerWidth() - $elem.outerWidth());
			}
			flyout.css("top", targetTop);
			flyout.css("left", targetLeft);
			
			//show
			$elem.addClass('nav-item-flyout');
			if ($.gtaaTopNav.useCufon == true) {
				Cufon.replace('div.item-middle a');
			}
		}
		
		$.fn.closeFlyout = function() {
			$(this).removeClass('nav-item-flyout');
			if ($.gtaaTopNav.useCufon == true) {
				Cufon.replace('div.item-middle a');
			}
		}
    }
})(jQuery);

$(document).ready(function () {
	$.fn.initTopNav();
});
