﻿// Attaching to page load
$(document).ready(function() {
    // Global popups
    $('a.pop-large').popup({
        width: 1060,
        height: 422,
        scrollbars: false
    });

    $('a.pop-small').popup({
        width: 810,
        height: 612,
        scrollbars: false
    });

    // Rebate form drop down
    $(".downloadRebate").click(function() {
        if ($(this).hasClass("over")) {
            var a = $(this);
            $("#downloadEnglish").slideUp(400, function() {
                $(a).removeClass("over");
            });
            $("#downloadSpanish").slideUp(400);
        } else {
            $(this).addClass("over");
            $("#downloadEnglish").slideDown();
            $("#downloadSpanish").slideDown();
        }
        return false;
    });
    $("a.submenu").hover(function() {
        $(this).addClass("hover");
    }, function() {
        $(this).removeClass("hover");
    });

    // Global tracking for eConduit
    $('#maytag-homedepot a').click(function(event) {
        var trackingTag = $(this).attr('rev');

        if (trackingTag) {
            eConduit.trackEvent(trackingTag);
        }
    });

    // Floodlight tracking
    MaytagHomeDepot.floodlight();

    // Init functions
    MaytagHomeDepot.maytagAdvantageTabs();

    // LivePersion variables submission using lpAddVars
    lpAddVars('page', 'ErrorCounter', ''); //All maytag microsite pages
    lpAddVars('page', 'Hierarchy', $('div.bcrumbsmthd').text().replace("You are here:", "")); //All maytag microsite pages
    lpAddVars('page', 'Section', 'Maytag'); //All maytag microsite pages	
});

// 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;
        default:
            tabIndex = 0;
    }

    // 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 = 'home';
	
	// 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('/_res/MaytagAdvantage/swf/hd_advantage.swf', '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('/_res/MaytagAdvantage/swf/hd_advantage.swf', 'alternativeContent', '768', '494', '9', '/assets/expressInstall.swf', flashvars, params, attributes);
	});
}

MaytagHomeDepot.initCashTabIndex = function() {
    $('#main-cash-nav > li > a').click(function(event) {
        event.preventDefault();

        // Remove 'current' class from all maytag advantage tabs
        $('#main-cash-nav > li').each(function(i) {
            $(this).removeClass('current');
        });

        // Highlight correct tab
        $(this).parent('li').addClass('current');

        //switch to correct tab
        if ($(this).attr('href') == '/what-is-cash-appliances/') {
            $('div.cash-box#cash-faq').hide();
            $('div.cash-box#what-is-cash-appliances').show();
        }
        else {
            $('div.cash-box#what-is-cash-appliances').hide();
            $('div.cash-box#cash-faq').show();
        }
    });
}

MaytagHomeDepot.initFAQS = function() {

    $('dl#faqs dd').hide();

    $('dl#faqs dt').hover(function() {
        $(this).addClass('hover');
    }, function() {
        $(this).removeClass('hover');
    });

    $('dl#faqs dt').bind('click', function() {
        if (!$(this).hasClass('open')) {
            $('dl#faqs dd.open').slideUp().removeClass('open').prev('dt').removeClass('open');
            $(this).addClass('open').next('dd').slideDown().addClass('open');
        }
    });

}

// 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() {};
	}
}


// DoubleClick Floodlight tracking hack since Floodlight was not built
// to track click actions to offsite pages or PDFs.
MaytagHomeDepot.floodlight = function() {
    $("a.floodlight").click(function(e) {
        e.preventDefault();
        var href = $(this).attr("href");
        var cat = $(this).attr("rel");
        var t = $(this).attr("target");
        var url = "http://fls.doubleclick.net/activityi;src=2625291;type=mayhdlp;cat=" + cat + ";ord=" + ((Math.random() + "") * 10000000000000);
        $("#flFrame").attr("src", url).one("load", function() {
            if (t == "_blank") {
               window.open(href);
            } else {
                self.location = href;
            }
        });
    });
}

MaytagHomeDepot.floodlightForm = function(form) {
    var cat = $(form).find("input[name='floodlight']").val();
    var url = "http://fls.doubleclick.net/activityi;src=2625291;type=mayhdlp;cat=" + cat + ";ord=" + ((Math.random() + "") * 10000000000000);
    var href = $(form).attr("action");
    var chil = $(form).children("input[type!=submit][name!='floodlight'],select,textarea");
    var q = "";
    $(chil).each(function(i) {
        if (q != "") { q = q + "&"; }
        q = q + $(this).attr("name") + "=" + $(this).attr("value");
    });
    console.log(q);
    $("#flFrame").attr("src", url).one("load", function() {
        self.location = href + "?" + q;
    });
}