// JavaScript Document
/***********************************************
* Switch Menu script- by Martial B of http://getElementById.com/
* Modified by Dynamic Drive for format & NS4/IE4 compatibility
* Visit http://www.dynamicdrive.com/ for full source code
*
* Modifications by David Joynson:
*    Added ability to switch the images between original and menu open states
*    Added function to display the last menu that was open before a page change.
***********************************************/

// String variable to hold previous name of image. - DJ
var prename = new String;
var menu = new String;

if (document.getElementById)
{ //DynamicDrive.com change
  document.write('<style type="text/css">\n')
  document.write('.submenu{display: none;}\n')
  document.write('</style>\n')
}

function SwitchMenu(obj, name)
{
  // If prename is blank then insert name argument. - DJ
  if(prename == "")
  {
    //alert("Name = \""+name+"\" \nPrename = \""+ prename+"\"");
    prename = name;
  }
    
  if(document.getElementById)
  {
    var el = document.getElementById(obj);
    var ar = document.getElementById("masterdiv").getElementsByTagName("span"); //DynamicDrive.com change
    if(el.style.display != "block")
    { //DynamicDrive.com change
      for (var i=0; i<ar.length; i++)
      {
        if (ar[i].className=="submenu") //DynamicDrive.com change
            ar[i].style.display = "none";
      }
      // If the prename is not the same as the name then switch to the closed       
      // state image for the previous menu. - DJ
      //alert("Name = \""+name+"\" \nPrename = \""+ prename+"\"");
      if (prename != name)
      {
        document.images[prename].src = "/images/" + prename + "button.jpg";
        prename = name;
      }
      // Switch to the open state image - DJ
      document.images[name].src = "/images/" + name + "buttonro.jpg";
      menu = el.id
      bakeCookie(menu,name);
      el.style.display = "block";
    }
    else
    {
      // Switch to the closed state image for the menu. - DJ
      document.images[name].src = "/images/" + name + "button.jpg";
      killCookie();
      el.style.display = "none";
    }
  }
}

////////////////////////////////////////////////////////////////////////////////
// Function for opening the menu between pages.
//
// The concept behind this is to utilize a cookie to to tell a page whether 
// or not the slide menu was open, and if so to which section. The page should 
// check on opening to see if there is a cookie with the information. If there 
// is no cookie then the slide menu is closed. If there is a cookie, then the 
// slide menu is open. The page then reads the cookie to see which section of 
// the slide menu is open, then opens it.
// 
// Copyright 2004, David Joynson

function OpenMenu()
{
// Variable to hold what we are looking for
  var menustr = new String;
// Get our main string
  menustr = getCookie();
// If it is not blank, then do something
  if (menustr != "")
  {
  // Variables to hold the parts and index integer
    var menu; // What submenu we want
    var name; // The name of the submenu
    var index = menustr.indexOf("|",0); // Where the divider is
  // Now get the strings we need
    menu = menustr.substring(0,index); // The submenu
    name = menustr.substring(index+1,(menustr.length)); // The submenu name
    prename = name; // The submenu name so we can use it for closing when we go elsewhere
    
    //alert("Menu = \""+menu+"\" \nName = \""+name+"\" \nPrename = \""+prename+"\"");
    var el = document.getElementById(menu);
    var ar = document.getElementById("masterdiv").getElementsByTagName("span"); //DynamicDrive.com change
    for (var i=0; i<ar.length; i++)
    {
      if (ar[i].className=="submenu") //DynamicDrive.com change
          ar[i].style.display = "none";
    }
  // Mark where we are with the cookie
    bakeCookie(menu, name);
  // Get the correct image to show
    document.images[name].src = "/images/" + name + "buttonro.jpg";
  // Show the menu
    el.style.display = "block";
  }
  else
  {
  // Destroy what ever cookie there is.
    killCookie();
  }
}

////////////////////////////////////////////////////////////////////////////////
// Functions for dropping a cookie to keep the menu open between pages.
//
// The concept behind this is to utilize a cookie to to tell a page whether 
// or not the slide menu was open, and if so to which section. The page should 
// check on opening to see if there is a cookie with the information. If there 
// is no cookie then the slide menu is closed. If there is a cookie, then the 
// slide menu is open. The page then reads the cookie to see which section of 
// the slide menu is open, then opens it.
// 
// Copyright 2004, David Joynson

// Define and drop the cookie on the client computer
function bakeCookie(menu, name)
{ 
  // Define the cookie....

  var expireDate;
  var expireDateStr;
  var menustr;
  
  menustr = menu + "|" + name;
  expireDate = new Date();
  expireDate.setMinutes(expireDate.getMinutes() + 20);
  expireDateStr = expireDate.toGMTString();
  
  // Now set the cookie
  // setCookie(cookieName,
  setCookie("openMenu", menustr, expireDateStr);
}
  
// Destroy the cookie on the client computer. This is done by redefining the 
// cookie with a new date.
function killCookie()
{ 
  // Define the cookie....
  var expireDateStr;
    
  // Fill the variables
  expireDate = new Date();
  expireDate.setMonth(expireDate.getMonth() + -1);
  expireDateStr = expireDate.toGMTString();
    
  // Now set the cookie. Since we are providing a null value and an expiration
  // prior to the current date, the cookie will die. However, the cookie.txt 
  // file will NOT be updated with the removed cookie until the end of the 
  // browser session.
  //
  setCookie("openMenu", "", expireDateStr);
}

////////////////////////////////////////////////////////////////////////////////
// Basic cookie funtions: Set and read

// Set the cookie
function setCookie(name, value, expires)
{
  document.cookie = name + "=" + value+ "; expires=" + expires;
}

// Read the cookie information
function getCookie()
{
  var nameSearch = "openMenu=";
    
  offset = document.cookie.indexOf(nameSearch);
  if (offset != -1)
  {
    offset += nameSearch.length ;
    end = document.cookie.indexOf("\r", offset) ;
    if (end == -1)
      end = document.cookie.length;
    return document.cookie.substring(offset, end);
  }
  else
    return "";
}
