/*
 *  Menubar code; derived from old flymenu.js and textmenu.js
 *  © 2003-2011, Horus Web Engineering Ltd
 *
 *  $Id: menubar.js,v 1.46 2011-10-18 16:31:35 horus Exp $
 *
 *  licensed under the terms of the GNU Lesser General Public License:
 *    http://www.opensource.org/licenses/lgpl-license.php
 *
 *  needs horus.js
 *
 */

MenuBar=
  function ( 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;
  };


// config parameters from #menuoffset
//
MenuBar.FLAGS         = 'height'; // x10
MenuBar.BAG_ZINDEX    = 'width';  // default 500

// control flags derived from MenuBar.FLAGS above
//
MenuBar.VARIABLESIZE  = 1;  // allow for varying interpretation of voffset in "em"s
MenuBar.FIXEDSIZE     = 2;  // allow for varying interpretation of voffset in pixels
MenuBar.GRAPHICBUTTON = 4;  // workarounds for IE7 graphic menu positioning weirdness
MenuBar.DUMMY         = 8;  // zero is not an acceptable height in some browsers
MenuBar.VERTICAL      = 16; // vertical (left) menu
MenuBar.CENTREHACK    = 32; // centred top menu (IE<7 hack)
MenuBar.NOIMAGEHACK   = 64; // suppress IE<7 image insertion hack


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');
    var offset=horus.getPosition(prefix+'offset', this.pad);
    this.topOffset=offset.top;
    this.leftOffset=offset.left;
    this.layout=Math.floor(offset[MenuBar.FLAGS]/10);
    this.centrehack=0;

    if (this.layout&MenuBar.VARIABLESIZE) {
      if (horus.iewin)
	this.topOffset+=horus.ie>7 ? 6 : 8;
      else if (horus.iemac)
	this.topOffset-=3;
      else if (horus.khtml)
	this.topOffset+=2;
      else if (horus.opera)
	this.topOffset+=5;
      else
	this.topOffset+=2;

    } else if (this.layout&MenuBar.FIXEDSIZE) {
      if (horus.iewin)
	this.topOffset+=2;
      else if (horus.opera)
	this.topOffset+=3;

    } else {
      if (horus.iewin)
	this.topOffset-=horus.ie<7 ? 1 : 2;
      else if (horus.opera)
	this.topOffset+=2;

    }

    this.bagZindex=offset[MenuBar.BAG_ZINDEX];
    if (!this.bagZindex) this.bagZindex=500;
    this.isover=this.link=this.head=this.popup=null;
    this.positioned=new Array();
    horus.onResize(this, this.resetposition);
    MenuBar.timerhack=null;

    if (horus.iewin) {
      var themenu;
      var theitem;

      if (horus.ie<7 && !(this.layout&MenuBar.NOIMAGEHACK)) {
	var thelink;
	var linkhack=document.createElement('img');
	linkhack.src='/common/resources/1x1.gif';
	linkhack.alt='';

	for (themenu=this.bar.firstChild; themenu; themenu=themenu.nextSibling) {
	  if (themenu.nodeName.toLowerCase()!='li') continue;

	  if (horus.ieold && this.layout&MenuBar.CENTREHACK && !this.centrehack)
	    this.centrehack=horus.getPosition(themenu, this.base).left;

	  thelink=themenu.firstChild;
	  thelink.insertBefore(linkhack.cloneNode(false), thelink.firstChild);
	  theitem=themenu.firstChild;

	  while (theitem && theitem.className!='menubag')
	    theitem=theitem.nextSibling;

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

	  while (theitem && theitem.nodeName.toLowerCase()!='ul')
	    theitem=theitem.nextSibling;

	  if (!theitem) continue;

	  for (theitem=theitem.firstChild; theitem; theitem=theitem.nextSibling) {
	    if (theitem.nodeName.toLowerCase()!='li') continue;
	    thelink=theitem.firstChild;

	    if (thelink.nodeName.toLowerCase()=='a')
	      thelink.insertBefore(linkhack.cloneNode(false), thelink.firstChild);

	  }
	}
      } else if (horus.ie<7 && this.layout&MenuBar.VERTICAL ||
		 this.layout&MenuBar.GRAPHICBUTTON)
	for (themenu=this.bar.firstChild; themenu; themenu=themenu.nextSibling) {
	  if (themenu.nodeName.toLowerCase()!='li') continue;
	  theitem=themenu.firstChild;

	  while (theitem && theitem.className!='menubag')
	    theitem=theitem.nextSibling;

	  if (!theitem) continue;
	  this.base.appendChild(theitem);
	}

    }

    this.init=true;
  };


MenuBar.prototype.iehack=
  function ( show ) {
    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;
    }
  };


MenuBar.prototype.geckohack=
  function () {
    if (this.popup) this.popup.style.visibility='visible';
  };


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.hide=
  function () {
    if (!this.link) return;
    this.link.className='menutitle';
    this.head.style.zIndex=400;
    this.pad.style.visibility='hidden';
    this.link=this.head=null;

    if (this.popup) {
      if (MenuBar.timerhack) {
	clearTimeout(MenuBar.timerhack[0]);
	MenuBar.timerhack=null;
      }

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


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

    if (id!=null) {
      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=this.layout&MenuBar.GRAPHICBUTTON && !(horus.iewin && horus.ie>=7) ?
	    { top: 0, left: 0 } :
	    horus.getPosition(horus.iewin ? this.link : this.head, this.base);

	  var popupTop=offset.top+this.topOffset;
	  var vadjust=basePos.top+popupTop+this.popup.offsetHeight+10-windowPos.bottom;
//	  horus.alert(vadjust, basePos.top, popupTop, this.popup.offsetHeight, windowPos.bottom);
	  if (vadjust>0) popupTop-=vadjust;

	  if (basePos.top+popupTop<10) {
	    popupTop=10-basePos.top;
	    popupStyle.height=(windowPos.height-20)+'px';
	    popupStyle.overflow='auto';
	  } else {
	    popupStyle.height=null;
	    popupStyle.overflow='visible';
	  }

	  var popupLeft=offset.left+this.leftOffset-this.centrehack;
	  var hadjust=basePos.left+popupLeft+this.popup.offsetWidth-windowPos.right;
//	  horus.alert(hadjust, basePos.left, popupLeft, this.popup.offsetWidth, windowPos.right);
	  if (hadjust>0) popupLeft-=hadjust;
	  popupStyle.top=popupTop+'px';
	  popupStyle.left=popupLeft+'px';
	  if (!horus.ieold) popupStyle.backgroundColor='transparent';

	  if (horus.gecko) // need a delay for Gecko
	    horus.setTimeout(this, this.geckohack, 2);
	  else
	    popupStyle.visibility='visible';

	}

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

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


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


window.themenu=new MenuBar;
horus.script.loaded('menubar');

