//Global JavaScript Document

/********************************

	Notes:
		+ I rely on JQuery v1.2.6
		+ use "jQuery" instead of $ for noConflict() use with other JS libs.

********************************/

// Prevent console commands from throwing errors in IE
try { console.log('init console... done'); } catch(e) { console = { log: function() {} } }

// Global variables
var menuSpeed = 150; //menu animation speed in milliseconds
var hideDelay = 500; //delay to hide menus in milliseconds	


function activateMainNav() {
	//
	// Code for main navigation dropdown menus
	//
	hideDelay = 250; //delay to hide menus in milliseconds

	jQuery('#mainNav > li').each(function(i) {

		if( (jQuery('ul', this).length) > 0 ) {

			var hideDelayTimer = null;
			var beingShown = false;
			var shown = false;
			var trigger = jQuery('>a', this);
			var menu = jQuery('>ul', this);			

			jQuery([trigger.get(0), menu.get(0)]).hover(
				function() {

					if (hideDelayTimer) clearTimeout(hideDelayTimer);
					if (beingShown || shown) {
						// don't trigger the animation again
						return;
					} else {
						// reset position of info box
						beingShown = true;

						trigger.addClass('over');
						menu.slideDown(menuSpeed, function() {
							beingShown = false;
							shown = true;							
						});
					}

					return false;
				},
				function(){

					if (hideDelayTimer) clearTimeout(hideDelayTimer);
					hideDelayTimer = setTimeout(function () {
						hideDelayTimer = null;
						menu.slideUp( menuSpeed, function () {
							shown = false;
							trigger.removeClass('over');							
						});

						}, hideDelay);

						return false;
					}
				);
			}
		});

		jQuery('#mainSearchButton').click(function(){
			jQuery('#searchBox').submit();
		});

		jQuery('#header h1').click(function(){
			window.location = jQuery('a', this).attr('href');								 

		});
	}

// Hide form selects on nav mouseover in IE6
function hideSelects() {
	// Use feature checking to recognize IE6
	if (typeof document.body.style.maxHeight === "undefined") {
		// Collect dropdown nav items
		var menus = jQuery('#mainNavKitchen, #mainNavBath, #mainNavSupport');
		// Collect selects with a class of hideMe or selects within #pageHeader
		var selects = jQuery('form select.hideMe, #pageHeader select');		
		
		// Hide and show the selects on hover/mouseout of dropdowns
		menus.hover(function(){
			selects.css({
				visibility: 'hidden'
			});			
		}, function(){
			selects.css({
				visibility: 'visible'
			});			
		})	
	}	
}


/* Initialization of functions created above */
jQuery(document).ready(function() {
	activateMainNav();
})