var _currentFloatingNavigation = undefined;
var _currentMenuNavigation = undefined;
var _childItemsIdPrefix = "children::item::";

var _parentChildHash = new Object();

function showNavigation(id, obj)
{
    obj.setAttribute("isfocused", "true");
    if ((_currentMenuNavigation && obj.id == _currentMenuNavigation.id) || obj.getAttribute("childrenArePinned"))
    {
        return;
    }	
	if (_currentMenuNavigation)
	{
	    setTitleCssClass(_currentMenuNavigation, false);
		_currentMenuNavigation = undefined;
	}	
	_currentMenuNavigation = obj;	
	setTitleCssClass(obj, true);
	
		    	
    if (_currentFloatingNavigation)
    {
	    _currentFloatingNavigation.removeAttribute("isopen");
	    _currentFloatingNavigation.style.visibility = "hidden";
	    _currentFloatingNavigation = undefined;
    }
	
	if (!obj.getAttribute("isContent"))
	{
	    var childContainer = document.getElementById(_childItemsIdPrefix + id.toString());	
	    if (childContainer.getAttribute("loading"))
	    {
		    return;
	    }	
	    
	    if (!childContainer.getAttribute("loaded"))
	    {
		    childContainer.setAttribute("loading", "true");		
		    GiantEagle.Applications.InternetSites.Web.Services.NavigationService.GetNavigationItems(id, receiveChildItems, receiveCallbackError, obj.id);		
	    }
	    else
	    {
		    childContainer.style.visibility = "visible";	
	    }
    	
	    childContainer.style.position = "absolute";
	    var moveTop = 1;
	    if (_currentBrowser.isIe)
	    {
		    moveTop = 0;
	    }
	    childContainer.style.top = (parseInt(obj.offsetTop) - moveTop).toString() + "px";
	    childContainer.style.left = (parseInt(obj.offsetWidth) - 13).toString() + "px";	
	    _currentFloatingNavigation = childContainer;
	}
	
}

function receiveChildItems(response, senderId)
{   
	// Locate the caller
	var sender = document.getElementById(senderId);
	if (!sender)
	{
		throw "Cannot locate sender so navigation request cannot be processed";
	}
	
	// Locate the child container
	var childNavigationContainer = document.getElementById(_childItemsIdPrefix + sender.getAttribute("contextid"));
	if (!childNavigationContainer)
	{
		throw "Cannot locate child container so navigation request cannot be processed";
	}
		
	if (!response || !response.ClientData || !response.ClientData.length || response.ClientData.length == 0)
	{	    
	    childNavigationContainer.setAttribute("loaded", "true");
		childNavigationContainer.removeAttribute("loading");
		return;	
	}
	
	// Create the Arrow
	var arrowContainer = document.createElement("DIV");
	arrowContainer.className = "navFloatNavArrowContainer";
	var arrow = document.createElement("IMG");
	
	arrow.src = _geApplicationRoot + "/Images/Nav/NavArrowRedPreview.gif";
	//arrow.width = "12";
	//arrow.height = "24";
	arrowContainer.appendChild(arrow);
	
	var items = document.createElement("DIV");
	items.className = "navItemsContainer";
	items.onmouseout = onFireParentMouseOut;
	items.onmouseover = onFireParentMouseOver;
	
	// Add an IFRAME so that the nav floats overtop of dropdowns.
	var navigationFrame = undefined;
	if (_currentBrowser.isIe)
	{
		navigationFrame = document.createElement("IFRAME");
		navigationFrame.src = _geApplicationRoot + "/images/shim.gif";
		navigationFrame.className = "navFloatingFrame";
		navigationFrame.frameBorder = "0";
		navigationFrame.scrolling = "no";		
		childNavigationContainer.appendChild(navigationFrame);
	}
	var length = response.ClientData.length;
	for (var i = 0; i < length; i++)
	{
		var item = document.createElement("DIV");
		item.className = "navItem";		
		item.onmouseout = function (e) { this.style.color = "#64656c";fireParentMouseOut(this); };
		item.onmouseover = function (e) { this.style.color = "#ed1820";fireParentMouseOver(this); };		
		item.innerHTML = response.ClientData[i].Name;
		item.setAttribute("targeturl", response.ClientData[i].Url);
		item.onclick = function (e) { go(this); };
		
		// This will get the height to display properly 
		// in safari.
		if (item.innerHTML.length > 20)
		{
			item.style.height = "auto";
		}
	
		// Removed The arrow because of how the nav functions.
		/*var itemArrow = document.createElement("IMG");
		itemArrow.className = "navItemArrow";
		itemArrow.src = _geApplicationRoot + "Images/Nav/NavArrowGray.png";
		item.appendChild(itemArrow);*/
		items.appendChild(item);
	}

	childNavigationContainer.onmouseover = function (e) { persistShowOnBehalfOfCaller(this); };
	childNavigationContainer.onmouseout = function (e) { persistHideOnBehalfOfCaller(this); };
	childNavigationContainer.appendChild(arrowContainer);
	childNavigationContainer.appendChild(items);	
	childNavigationContainer.setAttribute("callerid", sender.id);
	_parentChildHash[childNavigationContainer.id] = sender;
	childNavigationContainer.setAttribute("loaded", "true");
	childNavigationContainer.removeAttribute("loading");
	
	if (navigationFrame)
	{
		// Change the properties of the frame.
		navigationFrame.style.height = parseInt(items.offsetHeight).toString() + "px";
		navigationFrame.style.width = parseInt(items.offsetWidth).toString() + "px";
	}
	if (sender.getAttribute("isfocused"))
	{
		
		childNavigationContainer.style.visibility = "visible";
	}
}

function persistShowOnBehalfOfCaller(child)
{	
	var caller = _parentChildHash[child.id];
	if (!caller)
	{
		throw "Could not locate caller object for child with id '" + child.id + "'";
	}
	//showNavigation(caller.getAttribute("contextid"), caller);
	caller.setAttribute("isfocused", "true");
}

function persistHideOnBehalfOfCaller(child)
{	
	var caller = _parentChildHash[child.id];
	if (!caller)
	{
		throw "Could not locate caller object for child with id '" + child.id + "'";
	}
	//caller.removeAttribute("isfocused");
	hideNavigation(caller.getAttribute("contextid"), caller);
}

function hideNavigation(id, obj)
{
    if (obj.getAttribute("childrenArePinned"))
    {
        return;
    }
	obj.removeAttribute("isfocused");
	setTimeout("closeNavigation(" + id + ", '" + obj.id + "');", 300);
}

function closeNavigation(id, callerId)
{
	var caller = document.getElementById(callerId);
	if (caller && !caller.getAttribute("isfocused"))
	{
	    if (id)
	    {
		    var floatingNav = document.getElementById(_childItemsIdPrefix + id);
		    floatingNav.style.visibility = "hidden";
		    if (_currentFloatingNavigation && _currentFloatingNavigation.id == floatingNav.id)
		    {
			    _currentFloatingNavigation = undefined;
		    }
		}
				
		setTitleCssClass(caller, false);
		if (_currentMenuNavigation && _currentMenuNavigation.id == caller.id)
		{
			_currentMenuNavigation = undefined;
		}
	}
	else
	{
		setTimeout("closeNavigation(" + id + ", '" + callerId + "');", 300);
	}
}

function go(caller)
{    
    if (!caller || !caller.getAttribute("targeturl"))
    {  
        throw "Either the caller was not defined or the target url was not supplied";
    }    
    var targetUrl = caller.getAttribute("targeturl");
    if (targetUrl == "{HOME}")
    {
        window.location = _geApplicationRoot + "/";
    }
    else
    {
        window.location = targetUrl;
    }
    return true;
}

// Will locate the first child Div in the container element 
// and assume it is the title.
function setTitleCssClass(container, focused)
{
    if (!container || !container.childNodes || container.childNodes.length == 0)
    {
        throw "Cannot set CSS Class on object when it either does not exist nor has child nodes";
    }
    var title = undefined;
    var length = container.childNodes.length;
    var i = 0;
    while (!title && i < length)
    {
        if (container.childNodes[i] && 
            container.childNodes[i].tagName &&
            container.childNodes[i].tagName.toUpperCase() == "DIV" && 
            container.childNodes[i].innerHTML && 
            container.childNodes[i].innerHTML.length > 0)
        {
            title = container.childNodes[i];
        }
        i++;
    }    
    
    if (!title)
    {
        throw "Could not locate title container in parent object '" + container.id + "'";
    }
    if (container.getAttribute("isChild"))
    {
        if (focused)
        {
            title.className = "navChildItemTitleHover";  
        }
        else
        {
            title.className = "navChildItemTitle";
        }
    }
    else
    {
        if (focused)
        {
            title.className = "navInactiveTitleHover";
        }
        else
        {
            title.className = "navInactiveTitle";
        }
    }    
}

// Will handle the firing of the parent mouseout event.
function onFireParentMouseOut(e)
{
	fireParentMouseOut(this);	
}
function fireParentMouseOut(obj)
{
    if (obj.parentNode.onmouseout)
    {
        obj.parentNode.onmouseout();
    }	
}

// Will handle the firing of the parent on mouseover event.
function onFireParentMouseOver(e)
{
	fireParentMouseOver(this);	
}
function fireParentMouseOver(obj)
{
    if (obj.parentNode.onmouseover)
    {
        obj.parentNode.onmouseover();
    }
}

function receiveCallbackError(error)
{
    alert(error.get_message());
}