var _folderElement = null;
var _folderId = null;
var _floatNav = false;
var _currentNav = undefined;
var _createRoot = false;


function getNavigation(folderId, parentElement, forFloat)
{        
    if (!parentElement.parentNode.getAttribute("toggled") && !parentElement.getAttribute("isRoot"))
    {
        // This will abort the call to further navigation if the node is not toggled.
        return;
    }    
    var childContainer = document.getElementById("fc" + folderId);    
    // If the item is currently toggled (displayed vertically) just
    // return. If this is for a click (not for Float) then hide it.    
    if (childContainer.getAttribute("toggled")) 
    {
        if (!forFloat)
        {
            childContainer.removeAttribute("toggled")            
            childContainer.style.display = "none";
        }
        return;
    }
        
    var loading = false;
    if (!childContainer.getAttribute("loaded"))
    {                
        loading = true;                
        childContainer.setAttribute("loaded", "true");
        GiantEagle.Applications.InternetSites.Web.Services.NavigationService.GetNavigationItems(folderId, receiveChildNavigationItems, undefined, folderId);
              
    }    
    if (forFloat)
    {                      
        if (_currentNav && _currentNav.getAttribute("toggled") != "true")
        {
            _currentNav.style.display = "none";       
        }
        
        childContainer.className = "subnavFloat";                
        childContainer.style.position = "absolute";        
        positionElement(childContainer, parentElement);   
        parentContainer.setAttribute("focused", "true");             
        _currentNav = childContainer;   
    }
    else
    {
        childContainer.setAttribute("toggled", "true");
        childContainer.removeAttribute("holdNavigation");
        childContainer.style.position = "relative";         
        childContainer.style.top = 0;
        childContainer.style.left = 0;
               
        childContainer.className = "subnavInline";
    }            
    
    if (!loading && childContainer.getAttribute("display"))
    {        
        childContainer.style.display = "block";
    }
    
}

function receiveChildNavigationItems(result, context)
{    
    if(context != null)
    {        
        _folderId = context;
    }

    if (!result.OperationSucceeded)
    {
        alert("There was an error retrieving the navigation.\n\n" + result.ClientErrorMessage);
        //TODO we should redirect them to a page that shows the nav since their browser doesn't support something
        return;
    }
        
    var items = result.ClientData;
    var size = items.length; 
    
    var parentContainer = document.getElementById("fc" + _folderId);  
    parentContainer.removeAttribute("loading");
    if (size == 0)
    {                
        return;
    }    
    parentContainer.setAttribute("display", "true");
    for (var i = 0; i < size; i++)
    {       
        var item = items[i];
        var isFolder = (item.ListItemType == 'Assortment Folder');
        
        var menuItem = document.createElement("DIV");            
        menuItem.className = "leftSubnav";
        menuItem.id = "Float" + item.FolderId.Value;        
        var childContainer = undefined;
        
        if (isFolder)
        {                   
            //menuItem.onclick = navigationMenuItemClick;
            //menuItem.onmouseover = navigationMenuItemMouseOver;
            menuItem.setAttribute("folderId", item.FolderId.Value);
            
            var text = document.createElement("DIV");
            text.innerHTML = item.FolderName;
            text.className = "subnavItemName";
            
            var arrowContainer = document.createElement("DIV");
            arrowContainer.className = "subnavArrow";
            
            var arrow = document.createElement("IMG");
            arrow.src = _geApplicationRoot + "/Images/Nav/NavArrowGray.gif";            
            arrowContainer.appendChild(arrow);
            
            menuItem.appendChild(text);
            menuItem.appendChild(arrowContainer);
            
            var childContainer = document.createElement("DIV");
            //childContainer.className = "subnavFloat";
            childContainer.id = "fc" + item.FolderId.Value;
            childContainer.style.display = "none";                     
            childContainer.style.position = "absolute";
            childContainer.style.top = 0;
            childContainer.style.left = 0;            
          }
          else
          {            
            var text = document.createElement("DIV");
            text.style.borderStyle = "solid";
            text.style.borderWidth = "0px";
            text.style.height = "20px";
            text.innerHTML = item.Name;
            
            menuItem.appendChild(text);
          
          /*
            if (_createRoot)
            {
                html += "<div id=\"GEItemNode_" + folder.ContentId.Value + "\" style=\"border:solid silver 1px;height:20px;cursor:pointer;width: 193px;\" onclick=\"ItemNav('"+ folder.ContentId.Value +"', this);\">"  
                html += "<div style=\"float:left;cursor:pointer;\">C:" +folder.Name+ "</div></div>"
            }
            else
            {            
                html += "<div id=\"Float"+folder.FolderId.Value+"\" style=\"border:solid silver 1px;height:20px;cursor:pointer;\" onclick=\"ArrangeNav('"+ folder.FolderId.Value +"', this, _currentNav);\" >";
                html += "<div style=\"float:left;cursor:pointer;\">" + folder.Name + "</div></div>";xc
            }*/
          }            
          
          parentContainer.appendChild(menuItem);
          if (isFolder)
          {
            parentContainer.appendChild(childContainer);
          }
    }
    
    parentContainer.style.display = "block";                    
}

function navigationMenuItemClick(e) 
{ 
        getNavigation(this.getAttribute("folderId"), this, false);
}

function navigationMenuItemMouseOver(e)
{
    getNavigation(this.getAttribute("folderId"), this, true);
}

function hideNavigation(folderId)
{    
    setTimeout("closeNavigation(" + folderId + ")", 300);
}

function closeNavigation(folderId)
{
    
    document.getElementById("fc" + folderId).style.display = "none";    
}

function HideNav(folderId, folderElement)
{
       /* 
    if (_currentNav != null && _currentNav.getAttribute("toggled") != "true" && _currentNav.getAttribute("frozen"))
    {
        _currentNav.style.display = "none";
    }
    
    _currentNav.setAttribute("frozen", "true");
    if (_currentNav.getAttribute("frozen"))
    {
        writeLn("set");
    }
    
    _currentNav.removeAttribute("frozen");
    if (_currentNav.getAttribute("frozen"))
    {
        writeLn("still set");
    }*/
    
    
}
 function hideit()
 {
    
 }
 
function positionElement(obj, nextToObj)
{               
    obj.style.top = parseInt(nextToObj.offsetTop) - 2;
    obj.style.left = parseInt(nextToObj.offsetWidth);    
}


/*======================================
Logger functions */

var _lgr = undefined;
function writeLn(msg) {
    if (!_lgr) {
        _lgr = document.createElement("DIV");
        _lgr.style.height = "300px";
        _lgr.style.width = "100%";
        _lgr.style.overflow = "auto";
        _lgr.style.fontFamily = "courier new";
        //document.getElementsByTagName("FORM")[0].appendChild(_lgr);
        document.getElementById("mainBody").appendChild(_lgr);
    }
    _lgr.innerHTML += msg + "<br />";
}
