// 02003/01/15 Mark Rovendro -->
// Copyright (C)2000/2001/2002/2003 Mark Rovendro -->

// GLOBAL VARS
   var keepOn  = "" ;	// Active Menu Item
   var midiWin = "" ;	// Handler to Midi window
   var menuPath = "images/bg/menu/" ;
   var browser = (((navigator.appName == "Netscape") && (parseInt(navigator.appVersion) >= 3 )) || ((navigator.appName == "Microsoft Internet Explorer") && (parseInt(navigator.appVersion) >= 4 ))) ;
   var ns4 = (navigator.appName == "Netscape") && (parseInt(navigator.appVersion) < 5 ) ;
   
// This page excepts one argument which specifies the active parent menu.
// indexOf() returns -1 for nomatch, therefor the substring function matches
// the entire length of callingURL if no arguments are passed in. We need the +1
// to skip pass the ? that is part of the argument for our new page to load.

   var callingURL = this.document.URL ;
   var cgiString  = callingURL.substring(callingURL.indexOf('?')+1,callingURL.length) ;
   var parentMenu = "" ;

// The parentMenu object is assigned the value here. Since the image object has not been 
// created yet we do another check at the very bottom of the file and actually turn on the image.
   if ( cgiString.length != callingURL.length ) {
     // Check if Parent or Menu item
	 var type = cgiString.substring(0,cgiString.indexOf('=')) ;
	 var item = cgiString.substring(cgiString.indexOf('=')+1,cgiString.length) ;
	 if ( type == "p" ) {
	   parentMenu = item ;
	   ///alert( "parentMenu: " + parentMenu ) ;
	 } else {
	   keepOn = item ;
	 }
   }  //cgiString - we have arguments
 
// LOAD THE HEADER AND MAIN FRAMES.
function load(text,page) {
//  var header = parent.frames["header"].document ; 
  var main   = parent.frames["main"].document ;
  if ( page ) {
    // LOAD PAGE IN MAIN FRAME
    main.location = page ;
  }
} //load

// TURN "ON" THE IMAGE WHEN THE MOUSE MOVES OVER IT
function imageHeaderOn(Image) {
  if ( document.images && Image != "" ) {
    document.images.header.src = Image ;
  }
  // CHANGE WINDOW STATUS IF SECOND VALUE PASSED IN
  if ( arguments.length == 2 ) {
    window.status = arguments[1] ;
  }
  // THIS IS REQUIRED FOR THE WINDOW.STATUS CHANGE TO OCCUR
  return true ;
} //imageHeaderOn

// TURN "OFF" THE IMAGE WHEN THE MOUSE MOVES OFF IT
function imageHeaderOff(Image) {
  if ( document.images && (keepOn != Image && Image != "" && parentMenu != Image) ) {
    document.images.header.src = keepOn ;
  }
  // CLEAR THE WINDOW STATUS
  window.status = '' ;
  // THIS IS REQUIRED FOR THE WINDOW.STATUS CHANGE TO OCCUR
  return true ;
} //imageHeaderOff

// CHANGE IMAGE WHEN MOUSE IS PRESSED DOWN
function imageDown(Image) {
  return true ;
} //imageDown

// CHANGE IMAGE WHEN MOUSE IS RELEASED
function imageUp(Image) {
  return true ;
} //imageUp

function resetCurrent() {
// This is used to reset the active menu choice when the window is resized
  imageOn( keepOn ) ;
  imageOn( parentMenu ) ;
} //resetCurrent

function setCurrent( Image ) {
// This is used by the parent.main documents to set the active menu choice onLoad
  var oldImage = keepOn ;
  keepOn = Image ;
  imageOffGIF( oldImage ) ;
  imageOnGIF( keepOn ) ;
  
  // this pops up the debugger window. having a few problems but this should work.
  // i think IE is a bit confused and needs to be completely restarted.
  // deubugger ;;
  
  // Submenu situation, parent menu stored in second arg
  if ( arguments.length == 2 ) {
    var callingURL = this.document.URL ;
	var newURL = callingURL.substring(0,callingURL.indexOf('?')) ;
	///alert( callingURL + '^' + newURL ) ;
	// check if current menu
	if ( arguments[1] == parentMenu ) {
	   // parent menu currently active. Do nothing.
	   return 0 ;
	}
	// new parent menu - if there where no arguments then the indexOf will not match
	// anything to strip off. Use the callingURL.
	if ( newURL.length == 0 ) { 
	   newURL = callingURL ;
	}
	///alert( newURL + '?p=' + arguments[1] ) ;
	document.location = newURL + '?p=' + arguments[1] ;
	return true ;
  }
  
  if ( parentMenu != "" ) {
	var callingURL = this.document.URL ;
    var newURL = callingURL.substring(0,callingURL.indexOf('?')) ;
	///alert( "newURL: " + newURL + '?m=' + Image ) ;
	document.location = newURL + '?m=' + Image ;
	return true ;
  }
  return true ;
} //setCurrent

function openMidiWindow() {
// Create a separate window for the background sounds. This allows the user
// to listen to the sound while browsing. Additionally the plugin doesn't
// clutter the document.
  if ( midiWin != "" && midiWin.closed == 0 ) {
    midiWin.focus() ;
  } else {
    midiWin = window.open("sound/midi.html","midi","toolbar=no,scrollbars,height=330,width=250,resizable")
  }
} //openMidiWindow

function randomLoad() {
  // drop down selection in the menu form.
  var list = document.menu.homepage ;
  var origIndex = list.selectedIndex ;
  // The list starts with element 0 and ends with length-1. The first (0) element of the list
  // is the "--Previous Pages--" so we don't want to process that element.
  // 2002.07.23 The list has been changed to have the most recent pages starting at 1 and working down.
  // Changed the direction of the + and - operators.
  switch( arguments[0] ) {
	 case '+' : if ( list.selectedIndex > 1 ) list.selectedIndex--; break;
	 case '-' : if ( list.selectedIndex < (list.length-1) ) list.selectedIndex++; break;
	 default  : list.selectedIndex = Math.floor(Math.random()*(list.length-2)) + 1; break ;
  } //switch arguments
  if ( origIndex != list.selectedIndex )
  {
    load('',list.options[list.selectedIndex].value) ;
  }
} //randomLoad

function writeMenuItem( image, url, text, parent ) {
// Output the source html for the menu item. Check if there is an active parent.
  if ( !parent || parent == '' || parent == parentMenu ) {
    //document.writeln('<A HREF="javascript:load(\'' + image + '\',\'' + url + '\')"') ;
	document.writeln('<A HREF="' + url + '" ') ;
    document.writeln('onMouseOver="return imageOnGIF(\'' + image + '\',\'' + text + '\')"') ;
    document.writeln('onMouseOut="return imageOffGIF(\'' + image + '\')">') ;
    //document.writeln('<IMG SRC="images/bg/menu/' + image + '.gif" NAME="' + image + '" BORDER=0></A><BR>') ;
	document.writeln('<IMG SRC="' + menuPath + image + '.gif" NAME="' + image + '" BORDER=0></A><BR>') ;
  }
} //writeMenuItem

// TURN "ON" THE IMAGE WHEN THE MOUSE MOVES OVER IT
function imageOnGIF(Image) {
  if ( document.images && Image != "" ) {
    //document.images[Image].src = "images/bg/menu/" + Image + "_on.gif" ;
	document.images[Image].src = menuPath + Image + "_on.gif" ;
  }
  // CHANGE WINDOW STATUS IF SECOND VALUE PASSED IN
  if ( arguments.length == 2 ) {
    window.status = arguments[1] ;
  }
  // THIS IS REQUIRED FOR THE WINDOW.STATUS CHANGE TO OCCUR
  return true ;
} //imageOnGIF

// TURN "OFF" THE IMAGE WHEN THE MOUSE MOVES OFF IT
function imageOffGIF(Image) {
  if ( document.images && (keepOn != Image && Image != "" && parentMenu != Image) ) {
    //document.images[Image].src = "images/bg/menu/" + Image + ".gif" ;
	document.images[Image].src = menuPath + Image + ".gif" ;
  }
  // CLEAR THE WINDOW STATUS
  window.status = '' ;
  // THIS IS REQUIRED FOR THE WINDOW.STATUS CHANGE TO OCCUR
  return true ;
} //imageOffGIF

// TURN "ON" THE IMAGE WHEN THE MOUSE MOVES OVER IT
function imageOn(ID,Image) {
  if ( document.images && Image != "" ) {
	document.images[ID].src = Image ;
  }
  // CHANGE WINDOW STATUS IF SECOND VALUE PASSED IN
  if ( arguments.length == 3 ) {
    window.status = arguments[2] ;
  }
  // THIS IS REQUIRED FOR THE WINDOW.STATUS CHANGE TO OCCUR
  return true ;
} //imageOn

// TURN "OFF" THE IMAGE WHEN THE MOUSE MOVES OFF IT
function imageOff(ID,Image) {
  if ( document.images && Image != "" ) {
	document.images[ID].src = Image ;
  }
  // CLEAR THE WINDOW STATUS
  window.status = '' ;
  // THIS IS REQUIRED FOR THE WINDOW.STATUS CHANGE TO OCCUR
  return true ;
} //imageOff

function cacheMenuImages() {
// CACHE IMAGES FOR MENU
// By adding this code at the bottom of the page we can take advantage of the
// document.images array being populated by the static html. Since we have coded
// this to use the name attribute as the graphic name we can just step through
// the array and cache the images. This allows us to add new menu graphics without
// having to update this code.
//
// Note: all "off" menu graphics should already be cached since they had to be loaded
// to display the page. We should only have to cache the "on" images. I'm not sure, but
// I expect the browser checks the cache first and if it finds the name it just assigns
// it and moves on, not reloading the graphic. Albeit it may be a big assumption.
// 11.08.00 MJR - We only want to cache when this page is loaded without any arguments.
   var menuImg = new Array() ;
   var menuImgOn = new Array() ;
   var i=0, x=0 ;
   if ( document.images && cgiString.length == callingURL.length ) {
     for ( i=0; i < document.images.length; i++ ) {
	    with ( document.images[i] ) {
	       menuImg[name] = new Image() ;
	       menuImg[name].src = menuPath + name + ".gif" ;
	       menuImgOn[name] = new Image() ;
	       menuImgOn[name].src = menuPath + name + "_on.gif" ;
	    } //with
     } //for
   } //if
} // cacheMenuImages
