var arryPrevStack = [];
var arryNextStack = [];
var boolPerformHistoryNav = false;

function trackHistory(pStrOperation)
{
    var strStackItem = objMap.getCenter().lat + "," + objMap.getCenter().lon + "," + objMap.getZoom() + "," + objMap.getProjection() + "|" + objMap.getUnits() + "|" + objMap.getMaxExtent().left + "~" + objMap.getMaxExtent().bottom + "~" + objMap.getMaxExtent().right + "~" + objMap.getMaxExtent().top;

    // Track map history.
    if (pStrOperation.toUpperCase() == "PREV")
        addToPreviousStack(strStackItem.trim());
    else if (pStrOperation.toUpperCase() == "NEXT")
        addToNextStack(strStackItem.trim());
}

function doNavHistory(pObjDiv, pStrOperation)
{
    // Get the navigation history control from the map.
    var objNavigationHistory = objMap.getControlsByClass("OpenLayers.Control.NavigationHistory")[0]

    var strItem = null;
    var arryItem = null;

    // Perform the associated operation.
    if (pStrOperation.toUpperCase() == "PREV")
    {
        strItem = arryPrevStack.pop();

        // Check to see if there is history to go back to.
        if (strItem != undefined)
        {
            trackHistory("next");

            // Get the map information.
            arryItem = strItem.split(",");

            // Reposition the map.
            boolPerformHistoryNav = true;

            // Change projection if necessary.
            if (objMap.getProjection().toUpperCase() != arryItem[3].split("|")[0].toUpperCase())
                doProjection(arryItem[3].replace(/~/ig, ","));

            doSetCenterZoom(arryItem[1], arryItem[0], arryItem[2]);

            boolPerformHistoryNav = false;
        }
    }
    else if (pStrOperation.toUpperCase() == "NEXT")
    {
        strItem = arryNextStack.pop();

        // Check to see if there is history to go forward to.
        if (strItem != undefined)
        {
            trackHistory("prev");

            // Get the map information.
            arryItem = strItem.split(",");

            // Reposition the map.
            boolPerformHistoryNav = true;

            // Change projection if necessary.
            if (objMap.getProjection().toUpperCase() != arryItem[3].split("|")[0].toUpperCase())
                doProjection(arryItem[3].replace(/~/ig, ","));

            doSetCenterZoom(arryItem[1], arryItem[0], arryItem[2]);

            boolPerformHistoryNav = false;
        }
    }

    // Update the navigation history buttons.
    refreshNavHistoryButtons(pObjDiv, objNavigationHistory, pStrOperation)
}

function refreshNavHistoryButtons(pObjDiv, pObjNav, pStrOperation)
{
    // Enable/disable the previous and next history buttons..
    if (pStrOperation.toUpperCase() == "PREV")
    {
        // Check to see if the control is active.
        if (arryPrevStack.length > 0)
        {
            // Change the button image.
            toggleImage(pObjDiv, "on", "./images/back_enabled.gif", "./images/back_disabled.gif", null);
        }
        else
        {
            // Change the button image.
            toggleImage(pObjDiv, "off", "./images/back_enabled.gif", "./images/back_disabled.gif", null);
        }
    }
    else if (pStrOperation.toUpperCase() == "NEXT")
    {
        // Check to see if the control is active.
        if (arryNextStack.length > 0)
        {
            // Change the button image.
            toggleImage(pObjDiv, "on", "./images/next_enabled.gif", "./images/next_disabled.gif", null);
        }
        else
        {
            // Change the button image.
            toggleImage(pObjDiv, "off", "./images/next_enabled.gif", "./images/next_disabled.gif", null);
        }
    }
}

function addToPreviousStack(pStrItem)
{
    arryPrevStack.push(pStrItem);
}

function addToNextStack(pStrItem)
{
    arryNextStack.push(pStrItem);
}

function getPreviousStack()
{
    return arryPrevStack;
}

function getNextStack()
{
    return arryNextStack;
}

function clearPreviousStack()
{
    arryPrevStack = [];
}

function clearNextStack()
{
    arryNextStack = [];
}
