/* ===== Common Variables ===== */
var oldIE = false;
var veryOldIE = false;
if ($.browser.msie && $.browser.version.substr(0,1)<9) { oldIE=true; }
if ($.browser.msie && $.browser.version.substr(0,1)<8) { veryOldIE=true; }

/* ===== Functions ===== */
function fancyMenu() {
	menuEl = $(this);
	colorClass = menuEl.attr('colorClass');
	menuDiv = $(this).attr('href');
	if (colorClass != 'white') {
		if (!menuOpen) {
			menuOpen=true;
			showMenu();
		} else {
			animateMenuContent();			
		}

	} else {
		hideMenu();
	}
}
function menuOut() { }
function showMenu() { 
	$('#menu').removeClass('inactive');
	$('#menuContent li.active').removeClass('active');
	$(menuDiv).addClass('active');
	$('#overlay').fadeTo(600, .7);
	$('#subMenu').animate({'height':'157px'}, 600);
	colorShift(false);
}
function hideMenu() {
	clearTimeout(menuLinkTimeout);
	menuOpen=false;
	$('#overlay').fadeTo(500, 0).css('display', 'none');
	$('#subMenu').animate({'height':'0'}, 500, function() { $('#menu a').removeClass('active'); $('#menu').addClass('inactive'); });
}
function colorShift(aniShift) {
	if (aniShift) {
		$('#fadeColor').attr('class', colorClass).fadeIn(colorTime, function() {
			$('#subMenu').attr('class', colorClass).fadeIn(0, function() {
				$('#fadeColor').fadeOut(0);
			});
		});
	} else {
		$('#subMenu').removeAttr('class').attr('class', colorClass);
	}
}
var colorTimeout;
function animateMenuContent() {
	var testDiv = '#' + $('#menuContent li.active').attr('id');
	if (menuAniDone) {
		clearTimeout(menuTimeout);
		if (menuDiv != testDiv) {
			menuAniDone=false;
			var testIndex = $(testDiv).index();
			var menuIndex = $(menuDiv).index();
			if (testIndex > menuIndex) {
				$(menuDiv).addClass('loadLeft').addClass('active').removeAttr('style').animate({'left':0}, 600, 'easeInOutSine').removeClass('loadLeft');
				$(testDiv).animate({'left':'100%'}, 600, 'easeInOutSine', function() {
					$(this).removeClass('active').removeAttr('style');
					menuAniDone=true;
				});
				/* ===== Switch Colors ===== */
				if (oldIE) {
					clearTimeout(colorTimeout);
					colorTimeout =  setTimeout("colorShift(true)",400);
				} else {
					colorShift(true);
				}
			} else {
				$(menuDiv).addClass('loadRight').addClass('active').removeAttr('style').animate({'left':0}, 600, 'easeInOutSine').removeClass('loadRight');
				$(testDiv).animate({'left':'-100%'}, 600, 'easeInOutSine', function() {
					menuAniDone=true;
					$(this).removeClass('active').removeAttr('style');
				});
				/* ===== Switch Colors ===== */
				if (oldIE) {
					clearTimeout(colorTimeout);
					colorTimeout =  setTimeout("colorShift(true)",400);
				} else {
					colorShift(true);
				}
			}
		}
	} else {
		clearTimeout(menuTimeout);
		menuTimeout =  setTimeout("animateMenuContent()",200);
	}
}
function switchMenuClass() {
	menuEl = $(this);
	menuDiv = $(this).attr('href');
	if ($(this).attr('colorClass') == 'white') {
		menuOpen = false;
		hideMenu();
	} else if ($(this).attr('colorClass') == 'gray') {
		menuOpen = false;
		hideMenu();
	} else {
		menuEl.addClass('active');
	}
	menuEl.siblings('a').removeClass('active');
}
function noAction() {}
function removeActive(menuLinkHref) { 
	$('#menu a[href="'+menuLinkHref+'"]').removeClass('active');
}

/* ===== Variables ===== */
var colorTime = 700;
var menuEl = '';
var menuOpen = false;
var menuDiv = '';
var colorClass = '';
var menuAniDone = true;
var menuTimeout = '';
var menuLinkTimeout = '';

/* ===== Init HoverIntent ===== */
var config = { over: fancyMenu, timeout: 2, out: menuOut, sensitivity:1 };
var menuConfig = { over: noAction, timeout: 500, out: hideMenu };
var menuLinkConfig = { over: addHoverLink, out: removeHoverLink };

function addHoverLink() {
	var aniWidth = $(this).outerWidth();
	if (!$('.menu div.activeHoverLink').hasClass('activeClosing')) {
		$('.menu div.activeHoverLink').stop(true, false).animate({'width':0}, 220, function() {
			$(this).remove();
		});
	}
	$(this).before('<div class="hoverLink"><a href="'+$(this).attr('href')+'" style="height:'+$(this).height()+'px; width:'+$(this).width()+'px;">'+$(this).text()+'</a>').fadeIn(0, function() {
		$('div.hoverLink').animate({'width': aniWidth}, 260).addClass('activeHoverLink').removeClass('hoverLink');
	});
}

function removeHoverLink() {
	/* nada */
}
$('.menu div.activeHoverLink, .menu div.hoverLink').live('mouseleave', function() {
	$(this).addClass('activeHoverLink').addClass('activeClosing').removeClass('hoverLink').animate({'width':0}, 260, function() {
		$(this).remove();
	});
});

$("#menu").hoverIntent( menuConfig );
$("#menu > a").hoverIntent( config );
if (veryOldIE) {
	/* nada */
} else {
	$('#subMenu ul.unit a').hoverIntent( menuLinkConfig );
}
$('#menu > a').mouseenter(function() {
	$(this).addClass('active');
	$(this).siblings('a').removeClass('active');
});
$('#menuContent').mouseenter(function() {
	clearTimeout(menuLinkTimeout);
	var activeMenu = $('#menu > a.active').attr('href');
	if (activeMenu != menuDiv) {
		$('#menu a.active').removeClass('active');
		$('#menu a[href="'+menuDiv+'"]').addClass('active');
	}
});
$('#menu.inactive > a').live("mouseenter", function() {
	var colorClass= $(this).attr('colorClass');
	$('#subMenu').removeAttr('class').addClass(colorClass);
}).live('mouseleave', function() { 
	menuLinkHref = $(this).attr('href');
	menuLinkTimeout =  setTimeout("removeActive('"+menuLinkHref+"')",600); 
});

/* ===== Menu Click Function ===== */
$('#menu a.subLink').click(function() { return false; });

/* ===== Back To Top ===== */
$('#backToTop a').click(function() {
	$('body, html').animate({ 'scrollTop' : '0px'}, 650);
	return false;
});

/* ===== Set Active Left Nav Item ===== */
var winLoc = window.location.href;
var pos = winLoc.indexOf('.com') + 4;
winLoc = winLoc.substr(pos);
var pos = winLoc.indexOf('.asp');
var activeIndex; // for use in breadcrumb
$('.nav li a[href="'+ winLoc + '"]').addClass('active').fadeIn(0, function() {
	activeIndex = $(this).closest('li').index();
});

/* ===== MainRight Div Hover States and Links ===== */
var arrowArray = new Array('blueArrow.gif', 'brownArrow.gif', 'grayArrow.gif', 'greenArrow.gif', 'orangeArrow.gif', 'purpleArrow.gif', 'redArrow.gif');
var winLoc = window.location.href;
if (winLoc.indexOf('brownshoe') > -1) { /* this should stop if from happening on investor pages */
	if (!oldIE) {	
		$('.mainRight > .unit').mouseenter(function() {
			if (!$(this).hasClass('solid') && !$(this).hasClass('noHover')) {
				var thisHtml = $(this).html();
				$(arrowArray).each(function(index, value) {
					thisHtml = thisHtml.replace(value, 'whiteArrow.png');
				});
				var thisClass = $(this).attr('class');
				$(this).prepend('<div class="'+thisClass + ' solid hover">' + thisHtml + '</div>').fadeIn(0, function() {
					$(this).children('.hover').fadeIn(250);
				});
			}
		}).mouseleave(function() { $(this).children('.hover').stop().fadeOut(250, function() { $(this).remove(); });  });
	} else {
		$('.mainRight .bordered').mouseenter(function() {
			if (!$(this).hasClass('noHover')) {
				$(this).addClass('solid');
			}
		}).mouseleave(function() {
			$(this).removeClass('solid');
		});
	
	}
	$('.mainRight div.hover, .bordered.solid').live('click', function() {
		var thisHtml = $(this).html();
		var linkLocation = $(thisHtml).find('a:first').attr('href');
		window.location = linkLocation;
	});
}

/* ===== New Click Event on Main Menu ===== */
$('#menu > a').click(function() {
	var thisHref = $(this).attr('href');
	var pos = thisHref.indexOf('#');
	if (pos > -1) {
		var length = thisHref.length;
		length = length - pos;
		thisHref = thisHref.substr(pos, length);
		var newLink = $('#subMenu '+thisHref+' a:first').attr('href');
		window.location = newLink;
	}
});

/* ===== Breadcrumbs ===== */
var breadCrumbs = '';
var mainPage = $('.mainLeft div.nav h2').text();
var mainLink = $('.mainLeft div.nav li a:first').attr('href');
var pos = winLoc.indexOf('.com');
if (pos > 1) {
	if (activeIndex > 0) {
		var linkTwoText = $('.mainLeft div.nav li a.active').text();
		var linkTwoHref = $('.mainLeft div.nav li a.active').attr('href');
		breadCrumbs = '<a href="'+mainLink+'" title="'+mainPage+'">'+mainPage+'</a>&nbsp;&gt;&nbsp;<a href="'+linkTwoHref+'" title="'+linkTwoText+'">'+linkTwoText+'</a>';
	} else {
		breadCrumbs = '<a href="'+mainLink+'" title="'+mainPage+'">'+mainPage+'</a>';
	}
	$('#breadcrumbs').html(breadCrumbs);
}
