//Global JavaScript Document

/********************************

	Notes:
		+ I rely on JQuery v1.2.6

********************************/

jQuery.noConflict();
	
// Global variables
var menuSpeed = 150; //menu animation speed in milliseconds
var menuSpeedMedium = 350; //menu animation speed in milliseconds
var menuSpeedSlow = 550; //menu animation speed in milliseconds
var hideDelay = 500; //delay to hide menus in milliseconds


// Prevent console commands from throwing errors in IE
//try { console.log('init console... done'); } catch(e) { console = { log: function() {} } }


// Add classes to form inputs for css use
function addFormClasses() {
	if(jQuery('input').length) {
		jQuery('input').each(function(){
			jQuery(this).addClass(this.type);
		})
	}
}

function initMenus() {

	jQuery('#mainNav > li').each(function(){
		
		//set variables to reference main nav link and subnav menu so that we only traverse the DOM once
		var $trigger = jQuery('> .mainNavItem', this);
		var $menu = jQuery('> ul.subnav', this);
		
	
		//alert($menu.length);
		
		if($menu.length > 0) { //test if subnav menu exists for this item
		
			//declare variables to track menu status and timer for menu closing
			var menuHideTimer = null;
			var isAnimating = false;
			var isShown = false;
			
			var menuHeight = $menu.height();
			
			$menu.hide();
		
			jQuery([$trigger.get(0), $menu.get(0)]).hover(
				function() {
				
					if (menuHideTimer) clearTimeout(menuHideTimer);
					
					if (isAnimating || isShown) {
						// don't trigger the animation again
						return;
					} else {
						
						$parentID = $trigger.parent().attr('id');
						if($parentID == "mainNavProducts") {
							jQuery("#searchGranularity").addClass("hideFromIE");
						}else if($parentID == "mainNavStory") {
							jQuery("#lidropdown").addClass("hideFromIE");
						};
						
						// reset position of info box
						isAnimating = true;
						
						var delay = 100; // variable to stagger fade of menu links

						$trigger.addClass('hover');
						jQuery('a', $menu).css({ opacity: 0 });
						$menu
						.css({
							height: '0px'
						}).show()
						.animate({
							height: menuHeight
						}, 250, 'easeOutQuad', function() {
							isAnimating = false;
							isShown = true;
							
						});

						jQuery('a', $menu).each(function(){
							jQuery(this).animate({
								opacity: 0
							}, delay);
							jQuery(this).animate({
								opacity: 1
							}, 200);
							
							delay += 50;
						});

					}

					return false;
					
				},
				function(){
				
					if (menuHideTimer) clearTimeout(menuHideTimer);
					
					menuHideTimer = setTimeout(function () {
						menuHideTimer = null;
						
						$menuID = $menu.parent().attr('id');
						if($menuID == "mainNavProducts") {
							jQuery("#searchGranularity").removeClass("hideFromIE");
						}else if($menuID == "mainNavStory") {
							jQuery("#lidropdown").removeClass("hideFromIE");
						};
						
						$menu.animate(
							{ 
								height: '0px'
							}, 
							menuSpeed, 
							function () {
								$menu.hide();
								isShown = false;
								$trigger.removeClass('hover');							
							});
					}, hideDelay);

					return false;
				}
			
			);
		}
		
	});
	
}


// Initialize JS functions
jQuery(document).ready(function(){
	addFormClasses();
	initMenus();
})