/*
 * xMenuBox.js, 1.8, 2005-06-13
 *
 * Copyright (c) 2002-2005 Roumen Petrov.
 *
 * Roumen Petrov grants you a royalty free license to use, modify or
 * distribute this software provided that this copyright notice appears
 * on all copies.  This software is provided "AS IS," without a
 * warranty of any kind.
 *
 * Notes:
 * - Do not put script in HTML head section - NS4 and frames problem
 * - Works on:
 *   - Netscape 4.7x
 *   - Gecko 0.9.x/1.x (Mozilla, Firefox, Netscape 6x/7x, Galeon)
 *   - IE 5.5/6.0
 *   - Opera 5.1x/6.x/7.x/8.x
 *   - Konqueror 2.2.x/3.x
 */

var menuboxes = new Array;

MenuBox.prototype.minVisibleWidth   = 20;
MenuBox.prototype.minVisibleHeight  = 20;
MenuBox.prototype.status            = 0;
MenuBox.prototype.autoshrink        = undefined;
MenuBox.prototype.autoShrinkTimeOut = undefined;
MenuBox.prototype.onLeaveTimeOut    = undefined;

MenuBox.prototype.imgBaseUrl = 'images/';

MenuBox.prototype.imgGoRight = 'ff.gif';
MenuBox.prototype.imgGoLeft  = 'rew.gif';
MenuBox.prototype.imgGoDown  = 'down.gif';
MenuBox.prototype.imgGoUp    = 'up.gif';

MenuBox.prototype.imgLinkOn  = 'linkOn.gif';
MenuBox.prototype.imgLinkOff = 'linkOff.gif';


MenuBox.prototype.img_src = function (_shrink) {
  if (this.img == undefined) {
    /* img not found - nothing to do */
    return this;
  }
  if (this.shrink_status == _shrink) {
    /*
     * we must change image source only when _shrink is
     * different - work around for IE bug: continuous
     * loading of unchanged image from javascript when
     * is selected "check every visit yo the page"
     * for "check for newer version of stored page"
     */
    return this;
  }

  var img_name;
  if (this.moveTopDown) {
    if (_shrink) {
      img_name = this.imgGoDown;
    } else {
      img_name = this.imgGoUp;
    }
  } else {
    if (this.atRight) {
      /*
        We should(MUST!) avoid this case, because when
        menu shrink browser show horizontal scrollbar,
        page width is changed and as result menu looks ugly.
      */
      img_name = _shrink ? this.imgGoLeft : this.imgGoRight;
    } else {
      img_name = _shrink ? this.imgGoRight : this.imgGoLeft;
    }
  }
  this.img.src = this.imgBaseUrl + img_name;
  this.shrink_status = _shrink;

  return this;
}


function MenuBox(_name, _imgBaseUrl, _imgname, _moveVertical, _atRight) {
  this.id      = _name;
  this.element = new xElement(_name);

  if (_imgBaseUrl != undefined) {
    this.imgBaseUrl = _imgBaseUrl;
  }

  if (_imgname != undefined) {
    this.img = xDoc.getElement(_imgname);
  }

  if (_moveVertical == undefined ) {
    this.moveTopDown = false;
  } else {
    this.moveTopDown = (_moveVertical == true) ? true : false;
  }
  if (_atRight == undefined) {
    this.atRight = false;
  } else {
    this.atRight = (_atRight == true) ? true : false;
  }

  this.img_src(0);

  menuboxes[_name] = this;
}



MenuBox.prototype.init = function () {
  this.Xoffset = this.element.left();
  this.Yoffset = this.element.top ();

  if (this.atRight) {
    this.element.top (this.minVisibleHeight - this.element.height());
  } else {
    this.element.left(this.minVisibleWidth  - this.element.width ());
  }

  return this;
}


MenuBox.prototype.show = function () {
  this.element.show();
  return this;
}


MenuBox.prototype.hide = function () {
  this.element.hide();
  return this;
}


MenuBox.prototype.pointerOn = function (_imgname) {
  var img = xDoc.getElement(_imgname);
  if (img == undefined) {
    alert('MenuBox: link image ' + _imgname + ' not found !');
    return;
  }
  if (this.imgPointer != undefined) {
    this.imgPointer.src = this.imgBaseUrl + this.imgLinkOff;
  }
  this.imgPointer = img;
  this.imgPointer.src = this.imgBaseUrl + this.imgLinkOn;
}


MenuBox.prototype.stopShrink = function () {
  if (this.autoshrink != undefined) {
    window.clearTimeout(this.autoshrink);
    this.autoshrink = undefined;
  }
  return this;
}


MenuBox.prototype.shrink = function () {
  return this.stopShrink().shrink0();
}


MenuBox.prototype.shrink0 = function () {
  this.setXY();
  var dX;
  var dY;
  if (this.atRight) {
    if (this.moveTopDown) {
      dX =-this.minVisibleWidth  - this.element.width ();
      dY = this.minVisibleHeight - this.element.height();
    } else {
      dX =-this.minVisibleWidth  - 15;
      dY = 0;
    }
  } else {
    if (this.moveTopDown) {
      dX = 0;
      dY = this.minVisibleHeight - this.element.height();
    } else {
      dX = this.minVisibleWidth  - this.element.width ();
      dY = 0;
    }
  }

  this.element.moveTo (this.Xpos + dX, this.Ypos + dY);
  this.status = -1;
  this.img_src(1);

  return this;
}


MenuBox.prototype.grow = function () {
  return this.stopShrink().grow0();
}


MenuBox.prototype.grow0 = function () {
  this.setXY();
  var dX;
  var dY;
  if (this.atRight) {
    if (this.moveTopDown) {
      dX =-this.minVisibleWidth  - this.element.width();
      dY = 0;
    } else {
      dX =-this.minVisibleWidth  - this.element.width() + 15;
      dY = 0;
    }
  } else {
    dX = 0;
    dY = 0;
  }

  this.element.moveTo (this.Xpos + dX, this.Ypos + dY);
  this.status = 1;
  this.img_src(0);

  return this;
}


MenuBox.prototype.setXY = function () {
  if (window.pageYOffset != undefined) {
    this.Ypos = window.pageYOffset;
    this.Xpos = window.pageXOffset;
  } else {
    this.Ypos = document.body.scrollTop ;
    this.Xpos = document.body.scrollLeft;
  }

  if (this.atRight) {
    if (window.innerWidth != undefined) {
      this.Xpos += window.innerWidth ;
    } else {
      this.Xpos += window.document.body.clientWidth ;
    }
  }

  this.Ypos += this.Yoffset;
  this.Xpos += this.Xoffset;
}


MenuBox.prototype.adjust = function () {
  if (this.status < 0) {
    this.shrink0();
  } else {
    this.grow0();
  }
}


MenuBox.prototype.setAutoShrink = function (_tmout) {
/*
We must emulate missing feature in IE :-(
IE 5.x/6.x does not support block-level elements with style "position:fixed" (???????)

Gecko 0.9.x/1.x - yes
Konqueror 3.x - yes
Opera 5.x/6.x - yes
Netscape 4.7x - no (too old browser)
*/
  this.stopShrink();
  this.autoShrinkTimeOut = _tmout;
  if (this.autoShrinkTimeOut != undefined) {
    this.autoshrink = setTimeout("menuboxes['" + this.id + "'].shrink()", this.autoShrinkTimeOut);
  }
  return this;
}


MenuBox.prototype.setOnLeaveTimeOut = function (_tmout) {
  this.onLeaveTimeOut = _tmout;
  return this;
}


MenuBox.prototype.action = function () {
  if (this.status >= 0) {
    this.shrink();
  } else {
    this.grow();
  }
}


// Global functions:
function menubox_setAdjustInterval(_name, _time) {
  setInterval("menuboxes['" + _name + "'].adjust()", _time);
}


function menubox_setAutoShrink(_name, _tmout) {
  var o = menuboxes[_name];
  return o.setAutoShrink(_tmout);
}


function menubox_setOnLeaveTimeOut(_name, _tmout) {
  var o = menuboxes[_name];
  return o.setOnLeaveTimeOut(_tmout);
}


function menubox_stopShrink(_name) {
  var o = menuboxes[_name];
  return o.stopShrink();
}


function menubox_pointerOn(_name, _imgname) {
  var o = menuboxes[_name];
  return o.pointerOn(_imgname);
}


function menubox_enter(_name) {
  var o = menuboxes[_name];
  return o.grow();
}


function menubox_leave(_name) {
  var o = menuboxes[_name];
  if (o.onLeaveTimeOut != undefined) {
    o.autoshrink = setTimeout("menuboxes['" + o.id + "'].shrink()", o.onLeaveTimeOut);
  } else {
    o.shrink();
  }
  return o;
}


function menubox_action (_name) {
  menuboxes[_name].action();
}

