/**
*	Lowe's Masthead
*/

/* 	moving global nav timer functions outside of Masthead closure for scoping reasons
*	could be done by Wess.expose(), but with the time we have I'm trying to be quick. 
*/

var menuTimeout,
	menuShowTimeout,
	menuTimeoutInterval = 300,
	isNavigating = false;

;(function($){
	// Check to see if we have a Lowes namespace, if not create a container object.
	var Lowes = window.Lowes || {};
	//var isHomepage = true;
	// Masthead Namespace
	var Masthead = {
		init: function(options)
		{
			// When dealing with boolean, option check:
			// Because with the inline || (or) it will always eval incorrectly if option â€˜falseâ€™ is passed.
			// this.boolOption = (typeof options.boolOption !== undefined)? options.boolOption : true;
			// An example of setting up options
			
			options = options || {};
			
			//this.homepageState();
			
			// Main Nav Buttons
			$("#topnav a.trigger").bind({
				mouseenter: function(){
					//Lowes.Masthead.showMainMenu(this);
					
					clearTimeout( menuTimeout );
					
					var self = this;
					
					if ( isNavigating === true )
					{
						Lowes.Masthead.showMainMenu(this);
						
					} else {
						
						menuTimeout = setTimeout(function(){ Lowes.Masthead.showMainMenu(self); }, menuTimeoutInterval);
						
					}
					
				},
				mouseleave: function(){
					//isNavigating = false;
					clearTimeout( menuTimeout );
					menuTimeout = setTimeout(function() { Lowes.Masthead.hideMainMenu(); }, menuTimeoutInterval);
				}
			});
			
			// Tab Labels in Dropdown
			$('#topnav .categories > li > a').bind('mouseenter',function(){				
				clearTimeout(menuTimeout);
					var $this = $(this);
					
					if ( isNavigating ) {
						showMenuItems( $this );
					} else {
						menuShowTimeout = setTimeout( function() { showMenuItems($this); } , menuTimeoutInterval );
					}
			}).bind('mouseleave',function(){ clearTimeout(menuShowTimeout); /*isNavigating = false;this.homepageState();*/ });
			
			
			$('#topnav .sub-level').bind({
				mouseenter: function(){
					isNavigating = true;
					if(menuTimeout != undefined){clearTimeout(menuTimeout);}
				},
				mouseleave: function(){
					isNavigating = false;
					menuTimeout = setTimeout(function() { Lowes.Masthead.hideMainMenu(); }, menuTimeoutInterval);
				}
			});
			
			$('.aux-nav > li').bind({
				mouseenter: function(){
					$(this).addClass('active');
				},
				mouseleave:function(){
					$(this).removeClass('active');
				}
			});
			
			$('.holiday-nav').bind({
				mouseenter: function() {
					$(this).find('.category_name').css({
						'background': 'url(images/delta-gray-right.png) no-repeat 100% 50%',
						'color': '#000'
					});
				},
				mouseleave: function() {
					$(this).find('.category_name').css({
						'background': 'url(images/delta-white-right.png) no-repeat 100% 50%',
						'color': '#fff'
					});
				}				
			});
			
			$.fn.replaceholder();
			
		},
		showMainMenu: function(item) {
			this.hideMainMenu();
			//show the active menu, set timeout
			$item = $(item);
			
			$item.parent().attr('id','active');
			$item.addClass('selected');
			
			isNavigating = true;
			$item.next().find('li.first a').trigger('mouseenter');
		},
		hideMainMenu: function() {
			$('.corner').hide();
			$('.rt-corner').hide();
			$('#topnav #active').attr('id',null);
			$('#topnav a.selected').removeClass('selected');
			$('#topnav .sub-level').removeClass('visible');
			
			isNavigating = false;
			
			if ( menuTimeout !== undefined ) { clearTimeout( menuTimeout ); }
			//this.homepageState();
		},
		homepageState: function() {
			if (isHomepage && !isNavigating) {
				$('#topnav a.trigger:first').addClass('selected').parent().attr('id','active');
			}
		}
     };

     // Add Masthead to our Lowes namespace (object).
     Lowes.Masthead = Masthead;
     window.Lowes = Lowes;     

})(jQuery); 

/* 
 *	A generic set of functions to clear/restore an input field's value.
 *	Use: <input type="text" onblur="restoreValue(this);" onfocus="clearValue(this);" value="Original Value" />
 */
var defaultInput;
function clearValue(i){
	defaultInput = i.value;
	if(i.value == defaultInput) {
		i.value='';
	}
}
function restoreValue(i){
	if(i.value == ''){
		i.value=defaultInput;
	}
	defaultInput='';
}
// End input value functions
function showMenuItems(thisItem){
	isNavigating = false;
	
	var ppos = thisItem.parent().position(), 
		pposTop, finalHtml,
		$showItem = thisItem.attr('rel');
	
	pposTop = (thisItem.parent().hasClass('first')) ?  ppos.top : ppos.top-1;
	
	$('.corner').show().css({top: (pposTop)});
	$('.rt-corner').show().css({top: (pposTop)});
	
	//get item's rel attribute, specifying what flyout content to retrieve
	var filePath = thisItem.attr('rel');
	
	//check for flyout container
	if(thisItem.next('.menu_container').length < 1)
	{
		//append flyout container if it doesn't exist
		thisItem.after($('<div class="menu_container menu-content">loading...</div>'));	
	}
	
	//grab flyout container
	var menuContainer = thisItem.next('.menu_container');	
	
	//content exists, make sure other content at this level is hidden
	thisItem.parent().siblings().find('.menu_container').css({ 'z-index':100 });
	thisItem.parent().siblings().find('.menu_container').css({ 'display': 'none' });
	//check to see if flyout has content
	if(menuContainer.hasClass('fetched'))
	{
		//show this one
		menuContainer.css({ 'z-index':1000 });
		menuContainer.css({ 'display': 'inline' });

	}
	else
	{
		//content does not exist, fetch it; add class and bring to front after ajax load		
		menuContainer.load( filePath, function(response, status, xhr) {
  				if(status != "error") 
				{
	    			$(this).addClass('fetched').css({ 'z-index':1000 });
	  			}
			});		
	}
	
	$submenuDiv = thisItem.parent().parent().parent();

	$submenuDiv.addClass('visible').show();							  	
}

;(function($) {
	$.fn.replaceholder = function(options) {
		settings = jQuery.extend({
			focusColor: '#000',
			blurColor: '#aaa'
		}, options);

		var $placeholder; 
		(this.length > 0) ? $this = $(this) : $this = $('input[placeholder]');
        
		return $this.each(function() {
			var $placeholder = $(this);
            
			if ( $placeholder.length > 0 ) {
				
				var attrPh = $placeholder.attr('placeholder');
				
				$placeholder.attr('value', attrPh).css('color', settings.blurColor).bind('focus', function() {
					
					var $this = $(this);
					
					if ($this.val() === attrPh) {
					
						$this.val('').css('color', settings.focusColor);
					
					}
					
				}).bind('blur', function() {
					
					var $this = $(this);
					
					if ($this.val() === '') {
						
						$this.val(attrPh).css('color', settings.blurColor); 
					
					}
					
				});
			}
		});
	};
})(jQuery);









/*
	Last Modified Date: 10/08/2010
	Modified By: R. Adams
	1.0 Release
*/
$(document).ready(function(){

	Lowes.Masthead.init();

	// Testing async add this load
});





