  /*
    ================================================================================================
    imageLayerMenu is copyright Elias B. 
		Feel free to use this script as long as you leave this copyright notice

		History:
		--------
    ================================================================================================
  */

	// 
	function jsprintf()
  {
    // v0.0.1, 06/09/01, 01:26:42PM 
    str = arguments[0]; j = 1;
    while ( (idx = str.indexOf("%s")) != -1)
       str = str.substr(0, idx) + arguments[j++] + str.substr(idx+2);
    return str;
  }

  function imageLayerMenu(varName, attachImage, attachLayer, overImage, useImageEvents)
  {
    // find objects
    this.layer = getLayer(attachLayer);
    this.image = getImage(attachImage);
    this.name  = varName;

    if (!overImage)
      this.overimage = '';
    else
      this.overimage = overImage;

    // setup event handlers
    if (useImageEvents)
    {
      this.image.onmouseover = new Function(varName + '.mouseoverimage()');
      this.image.onmouseout  = new Function(varName + '.mouseexitimage()');
    }
    this.layer.onmouseout  = new Function(varName + '.layerout()');
    this.layer.onmouseover = new Function(varName + '.layerin()');

    // setup misc. stuff
    this.image.lastsrc = '';
    this.hidingTimeout = 0;
    this.hideTimeout   = 100;
    this.debugged = false;
    this.inimage = false;
    this.inlayer = false;
    this.alignleft = false; // if 'false' align right is applied
    this.dx = 0;
    this.dy = 0;

    hideLayer(this.layer);
  }

  imageLayerMenu.prototype.debugOut = function (msg)
  {
    if (this.debugged)
      document.title = jsprintf('%s [name:%s inlayer:%s inimage:%s]', msg, this.name, this.inlayer, this.inimage);
  }

  imageLayerMenu.prototype.adjustlayerposition = function ()
  {
    var x, y;

    // position layer
    x = getImagePageLeft(this.image);
    y = getImagePageTop(this.image) + this.image.height + 2;

    if (!this.alignleft)
      x = x - getWidth(this.layer) + this.image.width; // new_x = x - ( (L=Layer.width) - (l=image.width) )
    moveLayerTo(this.layer, x + this.dx, y + this.dy);
  }

  imageLayerMenu.prototype.showmenu = function ()
  {
    showLayer(this.layer);
  }

  imageLayerMenu.prototype.mouseoverimage = function ()
  {
    this.inimage = true;
    if ((this.overimage != '') && (this.image.lastsrc == ''))
    {
      this.image.lastsrc = this.image.src;
      this.image.src     = this.overimage;
    }
    this.debugOut('img:in');
    this.showmenu();
  }

  imageLayerMenu.prototype.mouseexitimage = function ()
  {
    this.inimage = false;
    this.debugOut('img:out');
    this.startHidingLayer();
  }

  imageLayerMenu.prototype.layerout = function ()
  {
    this.inlayer = false;
    this.debugOut('layer:out');
    this.startHidingLayer();
  }

  imageLayerMenu.prototype.layerin = function ()
  {
    this.inlayer = true;
    this.debugOut('layer:in');
  }

  imageLayerMenu.prototype.hideMenu = function ()
  {
    if (this.image.lastsrc != '')
    {
      this.image.src = this.image.lastsrc;
      this.image.lastsrc = '';
    }
    hideLayer(this.layer);
  }

  imageLayerMenu.prototype.hidingLayerTimeout = function ()
  {
    if ( !this.inlayer && !this.inimage )
      this.hideMenu(this.layer);
    else
      this.startHidingLayer();
  }

  imageLayerMenu.prototype.startHidingLayer = function ()
  {
    clearTimeout(this.hidingTimeout);
    this.hidingTimeout = setTimeout(this.name + '.hidingLayerTimeout()', this.hideTimeout);
    this.debugOut('start hiding');
  }
