// $Id: old.menubar.js,v 1.11 2011-05-05 13:03:15 horus Exp $

// menubar code; derived from old flymenu.js and textmenu.js
// this is a legacy version, for sites unconverted to the new code
// Copyright © 2003-2011, Horus Web Engineering Ltd. All rights reserved.

if (!window.horus)
  document.writeln
    ('<script type="text/javascript" src="/common/js/horus.js"></script>');


function MenuBar(prefix, head, popup, link) {
  if (!prefix) prefix='menu';
  this.prefix=prefix;
  this.headSuffix=head || 'h';
  this.popupSuffix=popup || 'm';
  this.linkSuffix=link || 'l';
  this.init=false;
}


MenuBar.prototype._init=function() {
  var prefix=this.prefix;
  this.base=document.getElementById(prefix);
  this.bar=document.getElementById(prefix+'bar');
  this.pad=document.getElementById(prefix+'pad');
  this.offset=document.getElementById(prefix+'offset');
  var offset=horus.getPosition(this.offset, this.pad);
  this.topOffset=offset.top;

  if (this.topOffset>10)
    if (horus.iewin)
      this.topOffset+=4;
    else if (horus.gecko)
      this.topOffset+=3;
    else if (horus.iemac)
      this.topOffset-=4;
    else if (horus.opera)
      this.topOffset+=6;
    else if (horus.khtml)
      this.topOffset+=2;

  this.leftOffset=offset.left;
  this.bagZindex=this.offset.offsetWidth;
  if (!this.bagZindex) this.bagZindex=500;
  this.isover=this.link=this.head=this.popup=null;
  this.positioned=new Array();
  horus.onResize([ this, 'resetposition' ]);

  if (horus.iewin && horus.ie<7) {
    var linkhack=document.createElement('img');
    linkhack.src='/common/resources/1x1.gif';
    linkhack.alt='';

    for (var themenu=this.bar.firstChild; themenu; themenu=themenu.nextSibling) {
      if (themenu.nodeName.toLowerCase()!='span') continue;
      var thelink=themenu.firstChild.firstChild.firstChild;
      thelink.insertBefore(linkhack.cloneNode(false), thelink.firstChild);
    }

    for (themenu=this.bar.nextSibling; themenu; themenu=themenu.nextSibling) {
      if (themenu.className!='menubag') continue;
      thelink=themenu.firstChild;

      while (thelink && thelink.nodeName.toLowerCase()!='div')
	thelink=thelink.nextSibling;

      if (!thelink) continue;
      thelink=thelink.firstChild;

      for (;;) {
	while (thelink && thelink.nodeName.toLowerCase()!='a')
	  thelink=thelink.nextSibling;

	if (!thelink) break;
	thelink.insertBefore(linkhack.cloneNode(false), thelink.firstChild);
	thelink=thelink.nextSibling;
      }
    }
  }

  this.init=true;
}


MenuBar.prototype.iehack=function(show) {
  if (!horus.iewin || horus.ie>=7) return;

  if (show) {
    var select=document.getElementsByTagName('SELECT');

    for (var i=0; i<select.length; i++)
      if (select[i].style.visibility!='hidden') {
	if (!this.select) this.select=new Array();
	this.select.push([select[i], select[i].style.visibility]);
	select[i].style.visibility='hidden';
      }

  } else if (this.select) {
    for (i=0; i<this.select.length; i++)
      this.select[i][0].style.visibility=this.select[i][1];

    this.select=false;
  }
}


function geckohack(id) {
  horus.visibility(id, true);
  window.menutimerhack=null;
}


MenuBar.prototype.resetposition=function() {
  if (this.positioned && this.positioned.length)
    while (this.positioned.length) {
      var id=this.prefix+this.positioned.pop()+this.popupSuffix;
      document.getElementById(id).positioned=false;
    }

}


MenuBar.prototype.show=function(id) {
  if (!this.init) this._init();
  if (this.isover) return;

  if (this.link) {
    this.link.className='';
    this.head.style.zIndex=400;
    this.pad.style.visibility='hidden';
    this.link=this.head=null;

    if (this.popup) {
      if (window.menutimerhack) {
	clearTimeout(window.menutimerhack);
	window.menutimerhack=null;
      }

      this.popup.style.visibility='hidden';
      this.popup=null;
      if (horus.iewin) this.iehack(false);
    }
  }

  if (id) {
    var prefix=this.prefix+id;
    this.link=document.getElementById(prefix+this.linkSuffix);
    this.head=document.getElementById(prefix+this.headSuffix);
    this.popup=document.getElementById(prefix+this.popupSuffix);
    this.pad.style.zIndex=100;
    this.base.style.zIndex=200;
    this.bar.style.zIndex=300;
    this.head.style.zIndex=400;

    if (this.popup) {
      var popupStyle=this.popup.style;
      popupStyle.zIndex=this.bagZindex;

      if (this.popup.positioned)
	popupStyle.visibility='visible';
      else {
	this.popup.positioned=true;
	this.positioned.push(id);
	var windowPos=horus.windowPos();
	var basePos=horus.getPosition(this.base);
	var offset=horus.getPosition(this.head, this.base);
	var popupTop=offset.top+this.topOffset;
	var vadjust=basePos.top+popupTop+this.popup.offsetHeight-windowPos.bottom;

	if (vadjust>0) popupTop-=vadjust;
	popupStyle.top=popupTop+'px';
	var popupLeft=offset.left+this.leftOffset;
	var hadjust=basePos.left+popupLeft+this.popup.offsetWidth-windowPos.right;
	if (hadjust>0) popupLeft-=hadjust;
	popupStyle.left=popupLeft+'px';
	if (!horus.iewin || horus.ie>=7) popupStyle.backgroundColor='transparent';
	// need a delay for Gecko
	window.menutimerhack=setTimeout("geckohack('"+prefix+this.popupSuffix+"')", 2);
      }

      if (horus.iewin) this.iehack(true);
    }

    this.pad.style.visibility='visible';
    this.link.className='hover';
  }
}

MenuBar.prototype.over=function(toggle) {
  this.isover=toggle;
  if (this.isover && this.link) this.link.className='active';
}

window.themenu=new MenuBar();
window.menubar=window.themenu;

