var docked;

if (window.addEventListener)
{
    window.addEventListener ('scroll', glwndexScroll, false);
    window.addEventListener ('resize', glwndexScroll, false);
    window.addEventListener ('load', redock, false);
}
else
{
    window.attachEvent ("onscroll", glwndexScroll);
    window.attachEvent ("onresize", glwndexScroll);
    window.attachEvent ("onload", redock);
}

function DockNode (nodeName, dockType)
{
    if (theForm != null)
    {
        var globalState = document.getElementById ("global_state");
        var node = document.getElementById (nodeName);
        var parentNodeElement = document.getElementById (node.parentNode.id);
        var state = GetAttribute (document.getElementById(node.id), "state");

        state = document.getElementById(state);
        if (GetAttribute (node, "dockType") == "")
        {
            SetAttribute (node, "oldParent", node.parentNode.id, state);
            SetAttribute (node, "oldPositionDock", node.style.position, state);
            
            parentNodeElement.style.visibility = "hidden";
            parentNodeElement.style.position = "absolute";
           
            node.parentNode.removeChild (node);
            node.style.position = "absolute";

            theForm.appendChild (node);
            SetStateValue (globalState, node.id, "AV");
            
            if (dockType == "top")
            {
                node.style.top = document.documentElement.scrollTop + 'px';
            }
            else if (dockType == "bottom")
            {
                node.style.top = (getWindowHeight () - node.offsetHeight) + 'px';
            }
            
            SetAttribute (node, "dockType", dockType, state);
        }
        else
        {
            var oldParent = document.getElementById (node.id + "_anchor");
            node.style.position = GetAttribute (node, "oldPositionDock");
            
            theForm.removeChild (node);
            oldParent.appendChild (node);
            oldParent.style.visibility = "visible";
            oldParent.style.position = "";
            
            RemoveStateValue (globalState, node.id);
            SetAttribute (node, "dockType", "", state);
        }
    }
}

function redock ()
{
    var dockType;
    var node;
    
    var globalState = document.getElementById ("global_state");
    if (globalState == null)
        return;
        
    var alwaysVisibles = globalState.value.split (";");

    for (var i = 0; i < alwaysVisibles.length; i ++)
    {
        if (alwaysVisibles [i].indexOf (":AV") != -1)
        {
            node = document.getElementById (alwaysVisibles [i].split (":")[0]);
            dockType = GetAttribute (node, "dockType");

            SetAttribute (node, "dockType", "");
            node.style.position = GetAttribute (node, "oldPositionDock");
            DockNode (node.id, dockType);
        }
    }
}

function glwndexScroll ()
{
    var dockType;
    var node;
    
    var globalState = document.getElementById ("global_state");
    
    if (globalState == null)
        return;

    var alwaysVisibles = globalState.value.split (";");

    for (var i = 0; i < alwaysVisibles.length; i ++)
    {
        if (alwaysVisibles [i].indexOf (":AV") != -1)
        {
            node = document.getElementById (alwaysVisibles [i].split (":")[0]);
            
            dockType = GetAttribute (node, "dockType");
            
            if (dockType == "top")
                node.style.top = document.documentElement.scrollTop + 'px';
            else
                node.style.top = (getWindowHeight () - node.offsetHeight) + document.documentElement.scrollTop + 'px';
       }
    }
}
