﻿// Global Navigation (GN)
var GN = {};

// scope all variables and functions within GN
GN.Context = undefined;
GN.openNav = undefined;
GN.theDiv = undefined;
GN.menuInt = undefined;

GN.swapImg = function(img, name, state)
{
	GN.theDiv = name;
	
	if(GN.openNav && state == "On")
	{
		GN.hideDiv(name);
	}
	
	if(GN.theDiv && state == "On")
	{
		GN.showDiv(img, name);
	}
	else if(GN.theDiv && state == "Off")
	{
		GN.openNav = GN.theDiv;
		GN.startTO(name);
	}
};

GN.showDiv = function(img, buttonName)
{
    $(buttonName, GN.Context).show();
};

GN.hideDiv = function(theType)
{
	if (GN.openNav)
	{
	    $(GN.openNav + " ul li a.default", GN.Context).removeClass("default");
		$(GN.openNav, GN.Context).hide();
		clearTimeout(GN.menuInt);
		GN.openNav = undefined;
	}
	if(theType == "reset")
	{
	    GN.setDefaults();
	}
};

GN.clearTO = function()
{
	clearTimeout(GN.menuInt);
};

GN.startTO = function(name)
{
	clearTimeout(GN.menuInt);
	GN.menuInt = setTimeout(function()
	{
	    GN.hideDiv('reset');
	}, 1000);
};

GN.set2On = function(id, theDrop)
{
    $(theDrop+" ul li a.default", GN.Context).removeClass("default");
    $("a#" + id, GN.Context).addClass("default");
};

GN.set2Off = function(id)
{
    $("a#" + id, GN.Context).removeClass("default");
};

GN.setDefaults = function()
{
    if (GN.Context === undefined)
    {
        GN.Context = $('div.GlobalNavigation:first');
    }
    var sdEls;
    var elems;
    var landingPages = new Array("#productsDrop", "#letItOutDrop", "#promotionsDrop", "#aboutKleenexDrop");
    
    elems = $("div.GlobalNav ul li a", GN.Context).length;
    sdEls = $("div.GlobalNav ul li a", GN.Context).slice();
    var topLength = (landingPages.length < sdEls.length) ? landingPages.length : sdEls.length;
    for (i=0; i<topLength; i++) 
    {
        if (document.location.href.split("?")[0] == sdEls[i]) 
        {
            $(landingPages[i], GN.Context).show();
            GN.openNav = landingPages[i];
        }
        else 
        {
            $(landingPages[i], GN.Context).hide();
        }
    }
    
    $('.DropDownNav', GN.Context).each(function()
    {
        $(this).mouseover(GN.clearTO).mouseout(GN.startTO);
    });
    
    theDrop = "#productsDrop";
    elems = $(theDrop + " ul li a", GN.Context).length;
    sdEls = $(theDrop + " ul li a", GN.Context).slice();
    for (i=0; i<elems; i++)
    {
        if (document.location.href.split("?")[0] == sdEls[i])
        {
            $("a#" + sdEls[i].id, GN.Context).addClass("default");
            $(theDrop, GN.Context).show();
            GN.openNav = theDrop;
        }
    }

    theDrop = "#letItOutDrop";
    elems = $(theDrop + " ul li a", GN.Context).length;
    sdEls = $(theDrop + " ul li a", GN.Context).slice();
    for (i=0; i<elems; i++) 
    {
        if (document.location.href.split("?")[0] == sdEls[i]) 
        {
            $("a#" + sdEls[i].id, GN.Context).addClass("default");
            $(theDrop, GN.Context).show();
            GN.openNav = theDrop;
        }
    }

    theDrop = "#promotionsDrop";
    elems = $(theDrop + " ul li a", GN.Context).length;
    sdEls = $(theDrop + " ul li a", GN.Context).slice();
    for (i=0; i<elems; i++)
    {
        if (document.location.href.split("?")[0] == sdEls[i]) 
        {
            $("a#" + sdEls[i].id, GN.Context).addClass("default");
            $(theDrop, GN.Context).show();
            GN.openNav = theDrop;
        }
    }

    theDrop = "#aboutKleenexDrop";
    elems = $(theDrop + " ul li a", GN.Context).length;
    sdEls = $(theDrop + " ul li a", GN.Context).slice();
    for (i=0; i<elems; i++)
    {
        if (document.location.href.split("?")[0] == sdEls[i]) 
        {
            $("a#" + sdEls[i].id, GN.Context).addClass("default");
            $(theDrop, GN.Context).show();
            GN.openNav = theDrop;
        }
    }
};
