/* version1_11
 */

// global configuration variables
layerSpacing = 10;               // x&y spacing for layer from mouse pos.
mSecsVis     = 50;             // # mSecs a layer remains shown after mouse out


// global variables
loader = window;
loader.onload = startIt;
isLoaded = areCreated = false;
layerLoc = null;
layerPrefix = "menuLayer";


// startIt is called when the page loads, and gets the
// whole system started

function startIt() {
         isLoaded = true;
         layerLoc = window;
         layerLoc.nav = nav = window;

         if (NS4) layerLoc.document.captureEvents(Event.MOUSEDOWN);
         layerLoc.document.onmousedown = eventPageMouseClick;

         setupLayer();
         
         preloadImages(imageList);

}


// initVars does the variable init

function initVars() {

         if (areCreated) {
            for (i=0; i<topCount; i++) {
                //cur = eval(layerPrefix+i);
                cur = document.getElementById(layerPrefix+i);
                clearTimeout(cur.hideTimer);
                cur.hideTimer = null;
            }
            clearTimeout(allTimer);
         }

         topCount = 0;
         areCreated = false;
         beingCreated = false;

         isOverLayer = false;
         currentLayer = null;
         allTimer = null;

 }

  initVars();

 // setupLayer handles the creation and initialization of layers

 function setupLayer() {
          // set status flag
          beingCreated = true;

          //while(eval(layerPrefix + topCount)) {
          while(document.getElementById(layerPrefix + topCount)) {

              // get the layer to work with
              layerName = layerPrefix + topCount;
              //layer = eval(layerName);
              layer = document.getElementById(layerName);

              // assigning functions to the layer
              layer.eventLayerOver = eventLayerOver;
              layer.eventLayerOut  = eventLayerOut;
              layer.showIt = showIt;
              layer.moveTo = moveTo;
              layer.positionLayer = positionLayer;
              layer.toggleLayer = toggleLayer;
              layer.startHideTimer = startHideTimer;
              layer.clearHideTimer = clearHideTimer;
              layer.timeoutHide = timeoutHide;
              layer.swapActiveImg = swapActiveImg;
              layer.swapInactiveImg = swapInactiveImg;

              // setup layer variables
              layer.layerName = layerName;
              layer.hideTimer = null;
              layer.isStuck   = false;
              layer.mouseOverLayer = false;
              layer.activeImg = null;
              layer.inactiveImg = null;
              layer.imgName = null;

              // setup event handling
              layer.onmouseover = layer.eventLayerOver;
              layer.onmouseout  = layer.eventLayerOut;

              // other setup
              layer.showIt(false);

              topCount++;
          }
          topCount--;
          status = topCount + " layers created";

          // finish up and set status flags
          beingCreated = false;
          areCreated = true;

}


// eventLayerOver is called when the mouse is over the current layer
// and updates status as needed

function eventLayerOver() {
         this.mouseOverLayer = true;
         if (!this.isStuck) {
            currentLayer = this;
            this.clearHideTimer();
         }
 }

 // eventLayerOut is called when the mouse moves out of the current layer
 // and updates status as needed

 function eventLayerOut() {
          this.mouseOverLayer = false;
          if (!this.isStuck) {
             this.startHideTimer();
          }
 }


 // eventPageMouseClick is called when there is a mouse click somewhere
 // on the page.  This makes the layers all dissapear if the mouse
 // was not inside the layer at the time

 function eventPageMouseClick() {
          if (currentLayer != null) {
             if ( (!currentLayer.mouseOverLayer)
                &&(!currentLayer.isStuck) ) {
                currentLayer.toggleLayer(false);
             }
          }
 }



// moveTo actually moves a layer to an arbitrary position

 function moveTo(xPos,yPos) {
    this.style.pixelLeft = xPos;
    this.style.pixelTop = yPos;
}


// startHideTimer inits the timer for hiding this layer.
// Used after the mouse leaves and mouse over link or the layer

function startHideTimer() {
         tempLayer = this;
         this.hideTimer = setTimeout("tempLayer.timeoutHide()", mSecsVis);
 }

 // clearHideTimer clears out the timer

 function clearHideTimer() {
          if (this.hideTimer) clearTimeout(this.hideTimer);
 }


 // timeoutHide executes the hiding operation after the timer has fired

 function timeoutHide() {

          // clear timer after it fires
          this.hideTimer = null;

          // turn off this layer if we're not using it
          if (!this.mouseOverLayer) {
             this.toggleLayer(false);
          }
 }


 // closeLayer is a wrapper for toggleLayer(false)
 // put in to give an easier to use interface, since this function
 // is used by links on the webpage

 function closeLayer(layerName) {
          //layer = eval(layerName);
          layer = document.getElementById(layerName);
          layer.toggleLayer(false);
          layer.isStuck = false;
 }


 // stickLayer is also used by links in the webpage.  It turns off
 // the timer for the layer.  The layer stays put until closeLayer()
 // is called

 function stickLayer(layerName) {
          //layer = eval(layerName);
          layer = document.getElementById(layerName);
          layer.clearHideTimer();
          layer.isStuck = true;
 }



 // toggleLayer handles turning on/off a layer and making
 // visible/hidden

 function toggleLayer(toggle) {
          if ( (toggle != true) && (toggle != false) )
             return;
          if (toggle == false) {
//             currentLayer = null;
             layer.clearHideTimer();
             this.swapInactiveImg();
          }
          this.showIt(toggle);
 }



// showIt toggles the visibility of a layer.

  function showIt(on) {
          if (NS4) {
             this.visibility = (on) ? "show" : "hide";
          }
          else {
               this.style.visibility = (on) ? "visible" : "hidden";
          }
 }


 // positionLayer(e) moves a layer to the location of the mouse
 // at the time of the triggering event

 function positionLayer(e) {
          xPos = layerSpacing + ((NS4) ? e.pageX : event.clientX);
          yPos = layerSpacing + ((NS4) ? e.pageY : event.clientY);
          this.moveTo(xPos, yPos);
 }




// popUp is called when the mouse goes over a link
// This handles showing the relevant layer

function popUp(linkName, imageName, imageFileInactive, imageFileActive) {
         // check initial conditions first
         if (!isLoaded) return;
         if (!beingCreated && !areCreated) startIt();

         // deal with null currentLayer (first shown layer)
         if (currentLayer == null) {
            //currentLayer = eval(linkName);
            currentLayer = document.getElementById(linkName);
         }
         
         // and show this new layer
         if (!currentLayer.isStuck) {

            // hide current layer if it's not this one
            if (currentLayer.layerName != linkName) {
               currentLayer.toggleLayer(false);
            }
            //currentLayer = eval(linkName);
            currentLayer = document.getElementById(linkName);
            currentLayer.clearHideTimer();
            currentLayer.toggleLayer(true);
            //currentLayer.positionLayer(e);
            
            // swap images
            currentLayer.activeImg = imageFileActive;
            currentLayer.inactiveImg = imageFileInactive;
            currentLayer.imgName = imageName;
            currentLayer.swapActiveImg();
            
         }
      
 }


 // popDown is called when the mouse leaves a link
 // This initiates hiding the relevant layer

 function popDown(linkName) {
          //layer = eval(linkName);
          layer = document.getElementById(linkName);
          if (!layer.isStuck) {
             layer.startHideTimer();
          }
 }
 
 function swapActiveImg() {
 	rotateImg(this.imgName, this.activeImg);
 }
 	
 function swapInactiveImg() {
 	rotateImg(this.imgName, this.inactiveImg);
 }
 
 function rotateImg(objName, imgSrc) {

	document[objName].src = imgSrc;

}
 
 function preloadImages(imgArray) { 
  	var d=document; 
  	if(d.images){ 
  		if(!d.myImageList) { d.myImageList=new Array(); }
  		var i
  		for (i=0; i < imgArray.length; i++) {
  			d.myImageList[i] = new Image;
  			d.myImageList[i].src=imgArray[i];
  		}
  	}
 }