jQuery(window).bind("load", initHorizontalSubFadeNav);

var currentSubNav = null;
var lastNavItem = null;
var subNavPadding = null;
var buffer = null;
var leftBuffer, rightBuffer;
var navWidth = null;
var navOffset = null;

function initHorizontalSubFadeNav() {
	styleLastElement('#pmenu li.main', 'mainLast');
	//
	currentSubNav = jQuery('#pmenu li a.current').siblings('ul').eq(0);
	//
	extendHoverState();
	//
	subNavPadding = parseInt(jQuery('#pmenu li.sub').eq(0).css('padding-left'));
	buffer = parseInt(jQuery('#pmenu a.mainAnchor').eq(0).css('padding-left'));
	/**
	 * leftBuffer and rightBuffer can be over-ridden in the index.jsp if the design has elements that 
	 * affect subnav positioning (ex: Procyon's GM Tools).
	 */
	leftBuffer = (leftBuffer == undefined) ? buffer : leftBuffer;
	rightBuffer = (rightBuffer == undefined) ? buffer : rightBuffer;
	//
	navWidth = jQuery('#navWrapper').width();
	navOffset = jQuery('#navWrapper').offset().left;
	//
	jQuery('#pmenu li.main').each( function() { addHover(this); });
	jQuery('#pmenu li.main').each( function() { centerSubNav(this); });
	//
	showCurrentNav();
	jQuery(window).trigger('navigationInit', [currentSubNav.outerHeight()]);
}

/**
 * Add hover functionality with jQuery since CSS :hover is poorly supported
 */
function extendHoverState() {
	jQuery('#pmenu li.main').hover(
		function() { jQuery(this).children('a.mainAnchor').addClass('over'); },
		function() { jQuery(this).children('a.mainAnchor').removeClass('over'); }
	);
	jQuery('#pmenu li.sub').hover(
		function() { jQuery(this).children('a.subAnchor').addClass('over'); },
		function() { jQuery(this).children('a.subAnchor').removeClass('over'); }
	);
	//
	jQuery('#navWrapper').hover(
		function() {},
		function() { 
			if(getLastNavItem()) {
				if (getLastNavItem().parent().attr('id') != currentSubNav.parent().attr('id')) {
					getLastNavItem().stop().fadeOut(2000, function() {
						currentSubNav.stop().css("opacity","").fadeIn(250);
						jQuery(window).trigger('navigation.reset', [currentSubNav.outerHeight()]);
					});
				}
			} else {
				currentSubNav.stop().css("opacity","").fadeIn(250);
			}
		}
	);
	//
}

/**
 * Set current vars, and show subnav for current main nav item
 */
function showCurrentNav() {
	currentSubNav.css('display', 'block');
	//
	setLastNavItem(currentSubNav); 
}

function addHover(element) {
	var dropul = jQuery(element).children('ul').eq(0);
	var height = dropul.show().outerHeight();
				dropul.hide();
	jQuery(element).hover(
		function() {
			
			if (getLastNavItem()) {
				getLastNavItem().stop().fadeOut(100);
			}
			
			currentSubNav.stop().fadeOut(100);

			if (hasSub(this)) {
				dropul.stop().css("opacity","").fadeIn(250);
				jQuery(window).trigger('navigation.reset', [height]);
			} 
			else {
				jQuery(window).trigger('navigation.reset', [0]);
			}
		},
		function() { 
			if (hasSub(this)) {
				setLastNavItem(dropul);
			}
			else {
				setLastNavItem(null);
			}
		}
	);
}

function hasSub(elem) {
	if (jQuery(elem).children('ul').length == 0) { return false; }
	return true;
}

function styleLastElement(selector, className) {
	var lastIndex = jQuery(selector).size() - 1;
	jQuery(selector).eq(lastIndex).each( 
		function() { jQuery(this).addClass(className); 
	});
}

/**
 * 
 */
function centerSubNav(parent) {
	var parent = jQuery(parent);
	var child = jQuery(parent).children('ul').eq(0);
	if (child.length > 0) {
		var parentLeft = jQuery(parent).offset().left - navOffset;
		var parentCenter = Math.floor(parentLeft + (jQuery(parent).width() / 2)); // relative to navWrapper offset
		var childWidth = getChildWidth(child);
		//
		var newChildLeft = parentCenter - (childWidth/2);
		if ((newChildLeft + childWidth) > (navWidth - rightBuffer)) {
			newChildLeft = navWidth - rightBuffer - childWidth;
		}
		if (newChildLeft < leftBuffer) { newChildLeft = leftBuffer; }
		// child node is absolutely positioned relative to parent, so its left offset is 0
		// We must compensate for this...
		newChildLeft -= parentLeft;
		//
		jQuery(child).css('margin-left', newChildLeft + "px");
		//
		jQuery(child).css('width', childWidth + "px");
		//if(getIsIE()) child.css("width",(childOffset + navWidth) + "px");
		//else child.css("width",(navWidth - childOffset) + "px");
	}
}

function getChildWidth(node) {
	var width = new Number();
	jQuery(node).show();
	jQuery(node).children('li.sub').each( function() {
		width += jQuery(this).width() + (subNavPadding * 2); // add padding
	});
	jQuery(node).hide();
	return width + 1; // Add 1 to address Mac/FF issue
}

/**
 * Getters and Setters
 */
function getLastNavItem() {
	return lastNavItem;
}
function setLastNavItem(value) {
	lastNavItem = value;
}
