// vars to enable dynamic content.
var req = null;
var place = null;
var lnk = null;
var currentAnchor = null;

// function to enable dynamic content.
function navigate(location, placement, linkid)
{
    currentAnchor = location;
    req = new XMLHttpRequest();
    var file = 'http://ghostfox.mozdev.org/sandbox/' + location;
    place = placement;
    
    if (linkid == null || linkid == '')
    {
        lnk = '_home';
    }
    else
    {
        lnk = linkid;
    }

    if (req != null)
    {
        req.onreadystatechange = stateChange;
        req.open("POST", file, true);
        req.send('');
    }
    else
    {
        alert("Your browser does not support XMLHTTP.");
    }
}

// XMLHTTPRequest.onreadystatechange handler for dynamic content
function stateChange()
{
    if (req.readyState == 4 && req.status == 200)
    {
        if (req.responseText)
        {
            var cont = document.getElementById(place);

            cont.innerHTML = req.responseText.split('<body>')[1].split('</body>')[0];
        
            document.getElementById(lnk).setAttribute('class', 'currentNav');

            var lnks = document.getElementById('links').getElementsByTagName('span');
        
            for(var i = 0;i < lnks.length; i++)
            {
                if(lnks[i].getAttribute('id') != lnk)
                {
                    lnks[i].setAttribute('class', 'navlink');
                }
            }
        }
    }
}

//Function which checks if there are anchor changes and calls the navigate function
function checkAnchor()
{  
    try
    {
        //Check if it has changes
        if(window.location.hash.length > 0)
        {
            if(currentAnchor != document.location.hash.substring(1, document.location.hash.length) + '.html')
            {
                currentAnchor = document.location.hash.substring(1, document.location.hash.length) + '.html';  
                //if there is not anchor, the loads the default section  
                if(!currentAnchor)
                {
                    if(document.getElementById('_home').getAttribute('class') != 'currentNav')
                    {
                        navigate('home.html', 'totalPage', '_home');
                    }
                }
                else  
                {  
                    navigate(currentAnchor, 'totalPage', "_" + currentAnchor.substring(1, currentAnchor.length - 5));
                }  
            }
        }
        else
        {
            if(currentAnchor != 'home.html')
            {
                navigate('home.html', 'totalPage', '_home');
            }
        }
    }
    catch (ex)
    {
        ex = null;
    }
    window.setTimeout('checkAnchor();', 300);
}

window.setTimeout('checkAnchor();', 300);
