
// 
// The panelHeight and loadHeightDynamic vars should prob be deprecated
// (this would remove the panelHeight param also)
// 

var menus = new Array();

window.onunload = SetReturnCookie;
function SetReturnCookie()
{
  if (ReadCookie("ReturnVisit") != "true") {
    SaveCookie("ReturnVisit", "true", "Thu, 01-Jan-2099 00:00:01 GMT", "/");
  }
}

//********* Collapsable Menu Set ************//

function CollapsableMenuSet()
{
  this.openPanel = null;
}


//********* Collapsable Menu Panel **********//

function CollapsableMenuPanel(panelID, menuSet, titleID, closeTitleHTML, openTitleHTML, saveState, defaultOpen)
{
  // Object Methods
  this.TogglePanel = TogglePanel;
  this.TogglePanelOnKey = TogglePanelOnKey;
  this.OpenPanel = OpenPanel;
  this.ClosePanel = ClosePanel;
  this._SetState = _SetState;
  this._GetState = _GetState;
  
  // Set up menu panel
  this.panelID = panelID;
  this.objTitle = document.getElementById(titleID);
  this.objPane = document.getElementById(panelID);
  if (this.objTitle)
  {
    if (closeTitleHTML != null && openTitleHTML != null)
    {
      this.closeTitleHTML = closeTitleHTML;
      this.openTitleHTML = openTitleHTML;
    }
  }
  // Set add menu to menu set
  this.menuSet = menuSet;
  menus[menus.length] = this;
  
  // Set saveState
  if (saveState != null)
  {
    this.saveState = saveState;
  }
  else
  {
    this.saveState = true;
  }
  // Set defaultOpen
  if (defaultOpen != null)
  {
    this.defaultOpen = defaultOpen;
  }
  else
  {
    this.defaultOpen = false;
  }
  
  // Load State
  if (ReadCookie("ReturnVisit") == "true" && this.saveState) {
    this._GetState();
  }
  else
  {
    if (this.defaultOpen)
    {
      this.OpenPanel();
    }
    else
    {
      this.ClosePanel();
    }
  }
}

//
// Toggle, open and close methods
//

function TogglePanel()
{
  if (this.objPane.style.display == "none")
  {
    this.OpenPanel();
  }
  else
  {
    this.ClosePanel();
  }
  
	// first child is the button image
  this.objTitle.firstChild.focus();
}

function TogglePanelOnKey(e)
{
	if (!e) return false;
	
	// Ignore enter events
	if (e.type == 'click') return false;
	
	// Check if key pressed was a tab, (ascii 9), or modifier. If so ignore.
	if (e.keyCode != 13 && e.keyCode >= 9 && e.keyCode <= 31) return true;
	    
	// Open or close panel
	if (this.objPane.style.display == "none")
	{
		this.OpenPanel();
	}
	else
	{
		this.ClosePanel();
	}
  
	// first child is the button image
  this.objTitle.firstChild.focus();
  
	return true;
}

function OpenPanel()
{
  // Check if this menu is in a set of menus that only opens one at a time
  if (this.menuSet)
  {
    if (this.menuSet.openPanel != null && this.menuSet.openPanel != this)
    {
      this.menuSet.openPanel.ClosePanel();
    }
    this.menuSet.openPanel = this;
  }
  // Set the text in the title, open the menu, and save the state
  if (this.openTitleHTML)
  {
    this.objTitle.innerHTML = this.openTitleHTML;
  }
  this.objPane.style.display = "block";
  this._SetState();
}

function ClosePanel()
{
  // Check if this menu is in a set of menus that only opens one at a time
  if (this.menuSet)
  {
    if (this.menuSet.openPanel == this)
    {
      this.menuSet.openPanel = null;
    }
  }
  // Set the text in the title, close the menu, and save the state
  if (this.closeTitleHTML)
  {
    this.objTitle.innerHTML = this.closeTitleHTML;
  }
  this.objPane.style.display = "none";
  this._SetState();
}

//
// The next two functions are used for saving panel state between pages
//
function _SetState()
{
  if (this.saveState)
  {
    DeleteCookie(this.panelID);
    if (this.defaultOpen && this.objPane.style.display == "none")
    {
      SaveCookie(this.panelID, "closed", "Thu, 01-Jan-2099 00:00:01 GMT", "/");
    }
    else if (!this.defaultOpen && this.objPane.style.display != "none")
    {
      SaveCookie(this.panelID, "open", "Thu, 01-Jan-2099 00:00:01 GMT", "/");
    }
  }
}

function _GetState()
{
  if (this.saveState)
  {
    var state = ReadCookie(this.panelID);
    var open = (state != null && state == "open") || (state == null && this.defaultOpen);

    if (open)
    {
      this.OpenPanel();
    }
    else
    {
      this.ClosePanel();
    }
  }
}


//
// Cookie Handling Functions
//

function SaveCookie(cookieName, cookieValue, expireDate, path)
{
  var cookieString = "";
  if (cookieName)
  {
    cookieString = cookieName + "=" + cookieValue + "; ";
    if (expireDate)
    {
      cookieString = cookieString + "expires=" + expireDate + "; ";
    }
    if (path)
    {
      cookieString = cookieString + "path=" + path + "; ";
    }
    document.cookie = cookieString;
  }
}

function DeleteCookie(cookieName)
{
  var cookieString = "";
  if (cookieName)
  {
    document.cookie = cookieName + "= ; expires= Thu, 01-Jan-1999 00:00:01 GMT;path=/;";
  }
}

function ReadCookie(cookieName)
{
    // Get strings
    var dc = document.cookie;
    var prefix = cookieName + "=";

    // Beginning of cookie substring
    var begin = dc.indexOf("; " + prefix);
    if (begin == -1) {
      begin = dc.indexOf(prefix);
      if (begin != 0) return null;
    }
    else
    {
      begin += 2;
    }

    // End of cookie substring
    var end = document.cookie.indexOf(";", begin);
    if (end == -1)
    {
      end = dc.length;
    }

    return unescape(dc.substring(begin + prefix.length, end));
}