var undefined;
var _toggling = false;
var _increment = 10;
var _timeIncrement = 5;
var _first = true;
var _isDashboardInitializing = false;
var _frameLagHeight = 10;

function initiateToggleDashboard(obj)
{
	if (_toggling)
	{
		return;
	}
	_toggling = true;
	_first = true;
	if (obj.getAttribute("toggled"))
	{
	    try
	    {
	        GiantEagle.Applications.InternetSites.Web.Services.Dashboard.SetDashboardOpenStatus("false", saveStatusCallback(), saveStatusFailed());
	    } catch (excSave) {}
		initDashboard(false);
		obj.removeAttribute("toggled");
		obj.src = _geApplicationRoot + "/Images/Dashboard/myTools_show.gif";
	}
	else
	{
	    try
	    {
	        GiantEagle.Applications.InternetSites.Web.Services.Dashboard.SetDashboardOpenStatus("true", saveStatusCallback(), saveStatusFailed());
	    } catch (excSave) {}
		initDashboard(true);
		obj.setAttribute("toggled", "true");
		obj.src = _geApplicationRoot + "/Images/Dashboard/myTools_hide.gif";
	}
	_isDashboardInitializing = false;
}

function saveStatusFailed(result)
{

}

function saveStatusCallback(result)
{
    
}

function initDashboard(expand)
{
	var toHeight = 0;
	if (expand)  	
	{
		toHeight = getDashboard().offsetHeight;	
		getDashboard().style.visibility = "visible";
		getDashboard().style.display = "none";
		getDashboard().style.height = "0px";
		getDashboardFrame().style.height = "0px";
		getDashboardFrame().style.display = "none";
	}
	
	toggleDashboard(toHeight, expand);
}

function toggleDashboard(toHeight, expand)
{
	var currentHeight = parseInt(getDashboard().offsetHeight);
	var heightDifference = toHeight - currentHeight; 
	
	var increment = _increment;
	
	if (_isDashboardInitializing)
	{
	    increment = toHeight - 1;
	}
	
	if (!expand)
	{
		increment *= -1;
		heightDifference = currentHeight - toHeight;
	}
	
	if (heightDifference < _increment)
	{
		if (expand)
		{
			getDashboard().style.height = (currentHeight + heightDifference).toString() + "px";
		}
		else
		{
			getDashboard().style.height = (currentHeight - heightDifference).toString() + "px";
		}
	}
	else
	{
		getDashboard().style.height = (currentHeight + increment).toString() + "px";
	}
	getDashboard().scrollTop = getDashboard().scrollHeight;
	
	if (_currentBrowser.isIe)
	{
		var frameHeight = parseInt(getDashboard().offsetHeight) - _frameLagHeight;
		if (frameHeight >= 0)
		{
			getDashboardFrame().style.height = frameHeight.toString() + "px";
		}
		else if (!expand)
		{
			getDashboardFrame().style.height = "0px";
		}
	}
		
	if (parseInt(getDashboard().style.height) == parseInt(toHeight))
	{
		if (!expand)
		{
			getDashboard().style.visibility = "hidden";
			if (_currentBrowser && _currentBrowser.isIe)
			{
				getDashboardFrame().style.display = "none";
			}
		}
		getDashboard().style.height = "auto";
		_toggling = false;
	}
	else
	{
		if (expand)
		{
			getDashboard().style.display = "block";
			if (_currentBrowser && _currentBrowser.isIe)
			{
				getDashboardFrame().style.display = "block";
			}
		}
		setTimeout("toggleDashboard(" + toHeight.toString() + ", " + expand.toString() + ")", _timeIncrement);
	}
}

function toggleSection(sender)
{
	var widgetId = sender.getAttribute("toggles"); 
	if (!widgetId)
	{
		throw "Cannot toggle section when there is no toggle attribute on sender";
	}
	var widget = document.getElementById(widgetId);
	if (sender.getAttribute("isopen"))
	{
		sender.removeAttribute("isopen");
		sender.getElementsByTagName("IMG")[0].src = _geApplicationRoot + "/Images/Dashboard/DSHBRD_CollapsedWidget.gif";
		sender.getElementsByTagName("DIV")[0].style.color = "#64656c";
		widget.style.display = "none";
	}
	else
	{
		sender.setAttribute("isopen", "true");
		sender.getElementsByTagName("IMG")[0].src = _geApplicationRoot + "/Images/Dashboard/DSHBRD_ExpandedWidget.gif";
		sender.getElementsByTagName("DIV")[0].style.color = "#ed1820";
		widget.style.display = "block";
	}
	adjustFrameHeight();
	updateDashboardPostBack(sender);
}

// PROPERTIES these will work as placeholders for the dashboard.

var _dashboard = undefined;
function getDashboard()
{
	if (!_dashboard)
	{
		_dashboard = document.getElementById("Dashboard");
	}
	return _dashboard;
}

var _dashboardFrame = undefined;
function getDashboardFrame()
{
	if (!_dashboardFrame)
	{
		_dashboardFrame = document.createElement("IFRAME");
		_dashboardFrame.className = "dashboardContainerFrame";
		_dashboardFrame.frameBorder = 0;
		_dashboardFrame.scrolling = "no";
		_dashboardFrame.style.visibility = "hidden";
		//_dashboardFrame.src = "about:blank";
		_dashboardFrame.src = _geApplicationRoot + "/images/shim.gif";
		// Need to add it to the document to be able to work with the frame
		// http://www.devarticles.com/c/a/JavaScript/Working-with-IFRAME-in-JavaScript/4/
		document.getElementById("DashboardPlaceholder").appendChild(_dashboardFrame);
		/*var frameDocument = _dashboardFrame.contentDocument;
		if (!frameDocument)
		{
			frameDocument = _dashboardFrame.contentWindow.document;
		}
		frameDocument.open();
		frameDocument.write("<html><head><style> body { margin:0px;padding:0px;background:none;background-color:transparent; } img {border:0px;behavior:url(" + _geApplicationRoot + "/Common/pngbehaviorinternal.htc);</style></head><body><table cellspacing='0' cellpadding='0' border='0' height='100%' style='height:100%;width:205px;'><tr><td style='background:#ffffff;height:100%'>&nbsp;</td></tr><tr><td style='text-align:center;background:none;height:26px;'><img src='" + _geApplicationRoot + "/Images/Dashboard/DSHBRD_Bottom.png' width='205' height='26' /></td></tr></table></body></html>");
		frameDocument.close();*/
		_dashboardFrame.style.visibility = "visible";
	}
	return _dashboardFrame;
}

// UTILITY Functions to interact with the dashboard from within the client.

function toggleSectionByName(widgetName)
{
	var sender = document.getElementById("Dashboard::" + widgetName);
	if (!sender)
	{
		throw "Cannot locate given widget named '" + widgetName + "' please check spelling and ensure that the widget exists";
	}
	toggleSection(sender);
}

function hideSectionByName(widgetName)
{
    var sender = document.getElementById("Dashboard::" + widgetName);
	if (!sender)
	{
		throw "Cannot locate given widget named '" + widgetName + "' please check spelling and ensure that the widget exists";
	}
	sender.style.display = "none";
	if (sender.getAttribute("isopen"))
	{
	    toggleSection(sender);
	}	
}

function initializeDashboard()
{
    _isDashboardInitializing = true;
    alterDashboardState();
}

function alterDashboardState()
{
	var dashboardToggleImage = document.getElementById(_dashboardImageClientId);
	if (!dashboardToggleImage)
	{
		throw "Cannot locate the dashboard toggle image to change the dashboard state";
	}
	initiateToggleDashboard(dashboardToggleImage);
}


function adjustFrameHeight()
{
    if (_currentBrowser.isIe)
	{
		getDashboardFrame().style.height = (parseInt(getDashboard().offsetHeight) - _frameLagHeight).toString() + "px";
	}
}


function updateListDashboardDelay()
{
    setTimeout ( "updateDashboardPostBack(document.getElementById('Dashboard::ShoppingList'))", 1500 );
}

function updateOpenListDashboardDelay()
{
    var hackSender = document.getElementById('Dashboard::ShoppingList');
    var hackHide = document.getElementById('topTabs_DashboardActionToggle');
    if(hackSender.getAttribute("isopen"))
    {
        setTimeout ( "updateDashboardPostBack(document.getElementById('Dashboard::ShoppingList'))", 1500 );
    }
    else
    {
        toggleSection(hackSender);
    }
    if(!hackHide.getAttribute("toggled"))
    {
        initiateToggleDashboard(hackHide);
    }
    
}

function updateDashboardPostBack(sender)
{
    var widgetId = sender.getAttribute("toggles");
	if (!widgetId)
	{
		throw "Cannot toggle section when there is no toggle attribute on sender";
	}
	var widget = document.getElementById(widgetId);
	var bIsLoaded =  sender.getAttribute("isLoaded");
	
	switch(widgetId)
    {
        case "FuelPerksWidget":
            if(!bIsLoaded && document.getElementById('dashboard_MyFuelperks_LoadFuelperksLink') != null)
            {
                sender.setAttribute("isLoaded", "true");
                __doPostBack('dashboard$MyFuelperks$LoadFuelperksLink','');
            }
            break;   
        case "RecipesWidget":
            if(!bIsLoaded && document.getElementById('dashboard_MyRecipesTest_LoadRecipeLink') != null)
            {
                sender.setAttribute("isLoaded", "true");
                __doPostBack('dashboard$MyRecipesTest$LoadRecipeLink','');
            }
            break;
        case "ShoppingListWidget":
            if(sender.getAttribute("isopen") && document.getElementById('dashboard_ManageShopList1_updateLink') != null)    
            {
                __doPostBack('dashboard$ManageShopList1$updateLink','');        
            }       
            break;
        case "ShoppingHistoryWidget":
            if(!bIsLoaded && document.getElementById('dashboard_MyShoppingHistory1_LoadHistory') != null)
            {
                sender.setAttribute("isLoaded", "true");
                __doPostBack('dashboard$MyShoppingHistory1$LoadHistory','');
            }
            break;
        case "MyStoresWidget":
            if(!bIsLoaded && document.getElementById('dashboard_MyStoresUcl_LoadMyStore') != null)
            {
                sender.setAttribute("isLoaded", "true");
                __doPostBack('dashboard$MyStoresUcl$LoadMyStore','');
            }
            break;
        default:
            break;
        }
    }

function tabSwitch(new_tab, new_content)
{
    document.getElementById('content_1').style.display = 'none';
    document.getElementById('content_2').style.display = 'none';
    document.getElementById('content_3').style.display = 'none';
    document.getElementById(new_content).style.display = 'block';

    document.getElementById('tab_1').className = '';
    document.getElementById('tab_2').className = '';
    document.getElementById('tab_3').className = '';
    document.getElementById(new_tab).className = 'on';

    if (new_content == "content_3")
    {
        loadLinksForYou();
    }
}

/*
function showTab(tabId)
{
    document.getElementById("rod").style.display = 'none';
    document.getElementById("newatge").style.display = 'none';
    document.getElementById("linksforyou").style.display = 'none';
    document.getElementById(tabId).style.display = 'block';
}
*/
