﻿// Attaching to page load
$(document).ready(function()
{
	
	
	MaytagHomeDepot.maytagAdvantageTabs();
	
});

// Creating namespace for Maytag at Home Depot JavaScript
var MaytagHomeDepot = {};

// Creating function to set initial tab highlight on page load for product nav box
MaytagHomeDepot.initTabIndex = function(tabName)
{
	console.log(tabName);
	
	// Setting a default index
	var tabIndex = 0;
	
	// Setting proper index based on tab name
	switch(tabName)
	{
		case 'laundry-appliances': // laundry landing page
			tabIndex = 0;
			break;
		case 'maytag-washers': // laundry washers page
			tabIndex = 1;
			break;
		case 'maytag-dryers': // laundry dryers page
			tabIndex = 2;
			break;
		case 'maytag-laundry-accessories': // laundry accessories page
			tabIndex = 3;
			break;
		case 'kitchen-appliances': // kitchen landing page
			tabIndex = 0;
			break;
		case 'maytag-refrigerators': // kitchen refrigerators page
			tabIndex = 1;
			break;
		case 'maytag-dishwashers': // kitchen dishwashers page
			tabIndex = 3;
			break;
		case 'cooking-appliances': // kitchen cooking page
			tabIndex = 2;
			break;
	}
	
	// Calling product navigation script
	MaytagHomeDepot.productBoxNavigation(tabName, tabIndex);
}

// Creating function to handle product box navigation
MaytagHomeDepot.productBoxNavigation = function(tabName, tabIndex)
{
	// If an index variable was sent in, build the nav
	if(tabIndex !== null)
	{
		// Highlight correct tab by looping over all tabs
		$('#main-product-nav > li > a').each(function(i)
		{
			// If out iterator equals our clickedIndex
			if(i == tabIndex)
			{
				// Check to see if our parent list item has the class 'current' attached
				if($(this).parent('li').hasClass('current') == false)
				{
					// Add class 'current' to parent link list item
					$(this).parent('li').addClass('current');
				}
			}
			else
			{
				// Remove any trace of 'current' class on parent list items
				$(this).parent('li').removeClass('current');
			}
		});
		
		// Show correct product grid index by looping over all grids
		$('#product-tab-box .prod-grid').each(function(j)
		{
			// If our iterator equals clickedIndex
			if(j == tabIndex)
			{
				// Show the corresponding product grid
				$(this).show();
			}
			else
			{
				// Hide all other product grids
				$(this).hide();
			}
		});
	}
	
	// Attach to click event on product navigation
	$('#main-product-nav > li > a').click(function(event)
	{
		// Preventing the browser from following href
		event.preventDefault();
		
		// Get and store current index of tab clicked
		var clickedIndex = $('#main-product-nav > li > a').index(this);
		
		// Highlight correct tab by looping over all tabs
		$('#main-product-nav > li > a').each(function(i)
		{
			// If out iterator equals our clickedIndex
			if(i == clickedIndex)
			{
				// Check to see if our parent list item has the class 'current' attached
				if($(this).parent('li').hasClass('current') == false)
				{
					// Add class 'current' to parent link list item
					$(this).parent('li').addClass('current');
				}
			}
			else
			{
				// Remove any trace of 'current' class on parent list items
				$(this).parent('li').removeClass('current');
			}
		});
		
		// Show correct product grid index by looping over all grids
		$('#product-tab-box .prod-grid').each(function(j)
		{
			// If our iterator equals clickedIndex
			if(j == clickedIndex)
			{
				// Show the corresponding product grid
				$(this).show();
			}
			else
			{
				// Hide all other product grids
				$(this).hide();
			}
		});
	});
	
	// Attach to click event on product sub tab navigation (only on kitchen products)
	$('.prod-grid-nav > li > a').click(function(event)
	{
		// Preventing the browser from following href
		event.preventDefault();
		
		// Get and store current index of tab clicked
		var clickedIndex = $(this).closest('.prod-grid-nav').children('li').children('a').index(this);
		
		// Highlight correct tab by looping over all tabs
		$(this).closest('.prod-grid-nav').children('li').children('a').each(function(i)
		{
			// If out iterator equals our clickedIndex
			if(i == clickedIndex)
			{
				// Check to see if our parent list item has the class 'current' attached
				if($(this).parent('li').hasClass('current') == false)
				{
					// Add class 'current' to parent link list item
					$(this).parent('li').addClass('current');
				}
			}
			else
			{
				// Remove any trace of 'current' class on parent list items
				$(this).parent('li').removeClass('current');
			}
		});
		
		// Show correct product grid index by looping over all grids
		$(this).closest('.prod-grid').children('.prod-sub-grid').each(function(j)
		{
			// If our iterator equals clickedIndex
			if(j == clickedIndex)
			{
				// Show the corresponding product grid
				$(this).show();
			}
			else
			{
				// Hide all other product grids
				$(this).hide();
			}
		});
	});
}

// Creating function to set initial tab highlight and flash content on Maytag Advantage page
MaytagHomeDepot.initAdvantageTabs = function(tabName)
{
	console.log(tabName);
	
	// Setting a default index
	var tabIndex = 0;
	
	// Setting default key
	var categoryKey = 'laundry';
	
	// Setting proper index based on tab name
	switch(tabName)
	{
		case 'maytag-advantage-laundry': // laundry
			categoryKey = 'laundry';
			tabIndex = 0;
			break;
		case 'maytag-advantage-cooking': // cooking
			categoryKey = 'kitchen';	
			tabIndex = 1;
			break;
		case 'maytag-advantage-dishwashers': // dishwashers
			categoryKey = 'cleaning';
			tabIndex = 3;
			break;
		case 'maytag-advantage-refrigeration': // refrigeration
			categoryKey = 'refrigeration';	
			tabIndex = 2;
			break;
	}
	
	MaytagHomeDepot.maytagAdvantage(categoryKey, tabIndex);
}

// Creating function to handle page load of Maytag Advantage page
MaytagHomeDepot.maytagAdvantage = function(categoryKey, tabIndex)
{
	// Highlight correct tab by looping over all tabs
	$('#main-advantage-nav > li > a').each(function(i)
	{
		// If out iterator equals our clickedIndex
		if(i == tabIndex)
		{
			// Check to see if our parent list item has the class 'current' attached
			if($(this).parent('li').hasClass('current') == false)
			{
				// Add class 'current' to parent link list item
				$(this).parent('li').addClass('current');
			}
		}
		else
		{
			// Remove any trace of 'current' class on parent list items
			$(this).parent('li').removeClass('current');
		}
	});
	
	// Create a new dom element to inject flash into
	var alternativeContent = document.createElement('div');
	alternativeContent.id = 'alternativeContent';
	
	// Remove old flash container from the page and insert a new one
	$('#explore-maytag').append(alternativeContent);
	
	// Create flash vars object to send to flash
	var flashvars = {
    	categoryKey: categoryKey
	};
	
	// Create params object to send to flash
	var params = {
	    menu: false,
	    base: '.',
	    quality: 'high',
	    salign: 't',
	    scale: 'exactfit',
	    wmode: 'opaque',
	    allowscriptaccess: 'always'
	};
	
	// Create attributes object to send to javascript
	var attributes = {
	    id: 'maytag-advantage-flash',
	    name: 'maytag-advantage-flash'
	};

		swfobject.embedSWF(hd_advantage, 'alternativeContent', '768', '494', '9', '/assets/expressInstall.swf', flashvars, params, attributes);
}

// Creating function for managing flash content on Maytag Advantage page
MaytagHomeDepot.maytagAdvantageTabs = function()
{
	$('#main-advantage-nav > li > a').click(function(event)
	{
		// Preventing the browser from following href
		event.preventDefault();
		
		// Remove 'current' class from all maytag advantage tabs
		$('#main-advantage-nav > li').each(function(i)
		{
			$(this).removeClass('current');
		});
		
		// Highlight correct tab
		$(this).parent('li').addClass('current');
		
		// Create a flash data object to hold embed properties
		var categoryKey = $(this).attr('href');
		
		// Create a new dom element to inject flash into
		var alternativeContent = document.createElement('div');
		alternativeContent.id = 'alternativeContent';
		
		// Remove old flash container from the page and insert a new one
		swfobject.removeSWF('maytag-advantage-flash');
		$('#explore-maytag').append(alternativeContent);
		
		// Create flash vars object to send to flash
		var flashvars = {
	    	categoryKey: categoryKey
		};
		
		// Create params object to send to flash
		var params = {
		    menu: false,
		    base: '.',
		    quality: 'high',
		    salign: 't',
		    scale: 'exactfit',
		    wmode: 'opaque',
		    allowscriptaccess: 'always'
		};
		
		// Create attributes object to send to javascript
		var attributes = {
		    id: 'maytag-advantage-flash',
		    name: 'maytag-advantage-flash'
		};
		
		swfobject.embedSWF(hd_advantage, 'alternativeContent', '768', '494', '9', '/assets/expressInstall.swf', flashvars, params, attributes);
	});
}

// Silences console commands in browsers without firebug
if(!window.console || !console.firebug)
{
	var names = ["log", "debug", "info", "warn", "error", "assert", "dir", "dirxml", "group", "groupEnd", "time", "timeEnd", "count", "trace", "profile", "profileEnd"];
	
	window.console = {};
	
	for(var i = 0; i < names.length; ++i)
	{
		window.console[names[i]] = function() {};
	}
}
