// ---------------------------------------------------------------------------
// (c) 2003 Geotalk, Hans Martin Mohn
// ---------------------------------------------------------------------------
//
// Implement a stack for storing navigation URL's. It is used to keep track of
// previously visited maps in the same manner as the "back" and "forward" buttons
// in Web browsers
//
// Based on StackNavigate.java by Arne Marius Hallum
//
// Dependencies:
//
//

//
//  +---+---+---+---+---+---+
//  |   |   |   |   |   |   |
//  +---+---+---+---+---+---+
//    ^       ^               ^
//    |       |               |
//    0     m_pos        m_stack.length
//
function gtNavigateStack()
{
  this.m_pos   = -1;     // Always containing the *current* URL
  this.m_stack = new Array();


  this.clear = function()
  {
    this.m_pos = -1;
    this.m_stack.length = 0;
  }; // end clear()



  // Check whether or not it is possible to go to next url in the stack
  // Returns true if possible, false otherwise.
  this.nextOk = function()
  {
    return (this.m_stack.length - this.m_pos > 1);
  }; // end nextOk()



  // Check whether or not it is possible/allowed to go to previous url in the stack
  // Returns true if possible, false otherwise.
  this.prevOk = function()
  {
    return (this.m_pos > 0);
  }; // end prevOk()



  // Get the number of elements in the history
  // Returns the number of elements in the history
  this.getSize = function()
  {
    return this.m_stack.length;
  }; // end getSize()



  // Gets the position in the stack
  // Returns position in the history
  this.getHistoryPos = function()
  {
    return this.m_pos;
  }; // end getHistoryPos()



  // Get the url at position pos in history
  //    pos   Position in history
  // Returns url on that position or null if not valid
  this.getHistoryURL = function(pos)
  {
    if (pos < 0 || pos >= this.m_stack.length)
      return null;
    else
      return this.m_stack[pos];
  }; // end getHistoryURL()



  // Get previous url.
  // Returns the url
  this.getPrev = function()
  {
    if (this.prevOk())
    {
      gtTrace("Stack: getPrev returning " + this.m_stack[this.m_pos - 1] + "\n");
      --this.m_pos;
      return this.m_stack[this.m_pos];
    }
    else
    {
      gtTrace("Stack: getPrev returning null (empty)\n");
      return null;
    }
  }; // end getPrev()



  // Get next url.
  // Returns the url
  this.getNext = function()
  {
    if (this.nextOk())
    {
      gtTrace("Stack: getNext returning " + this.m_stack[this.m_pos + 1] + "\n");
      ++this.m_pos;
      return this.m_stack[this.m_pos];
    }
    else
    {
      gtTrace("Stack: getNext returning null (empty)\n");
      return null;
    }
  }; // end getNext()



  // Add a new url to the stack
  this.addUrl = function(url)
  {
    // If m_pos is not the last element in the m_stack, remove all the elements following m_pos
    this.m_stack.length = this.m_pos + 1;

    this.m_stack.push(url);
    this.m_pos++;

//    gtTrace("Stack: added url " + url + ", m_pos=" + m_pos + ", stack size=" + m_stack.length + "\n");
  }; // end addUrl()



  // Replace the current url on the stack
  this.replaceUrl = function(url)
  {
    if (this.m_pos >= 0)
      this.m_stack[this.m_pos] = url;
    else
      this.addURL(url);
  }; // end replaceUrl()

} // end class gtNavigateStack
