// Copyright (c) 2005 CommonTown Pte Ltd. All rights reserved. 
// 2006-02-16 joel loo 
// ------- menu object -------

ctmenu={
  count:0,
  curmenu:false,

  show: function(menu,ohoming,where,delay,noie6fix) {
    if (typeof delay=="undefined") delay=200;
    // vvvv does not work, ff has no window.event?
    // if (typeof ohoming=="undefined") ohoming=window.event.srcElement;

    omenu=document.getElementById(menu);
    if (!omenu) return; 

    if (omenu.style.visibility=='visible') {
      this._clear_hide(omenu);
      return;
    }

    omenu.menu_data={delay: delay, homing:ohoming, otimeout:'none'}
    omenu.onmouseover=this._onmouseover;
    omenu.onmouseout=this._onmouseout;

    pos=this._get_edges(ohoming);

    switch (where) {
      case 'right': kl=omenu.style.left=pos.right; kt=omenu.style.top=pos.top; break;
      case 'top': kl=omenu.style.left=pos.left; kt=omenu.style.top=pos.top-omenu.offsetHeight; break;
      case 'left': kl=omenu.style.left=pos.left-omenu.offsetWidth; kt=omenu.style.top=pos.top; break;
      default: kl=omenu.style.left=pos.left; kt=omenu.style.top=pos.bottom; 
    }
//    omenu.style.left=0; omenu.style.top=0;

    omenu.style.visibility='visible';
    this.count++;
    this.curmenu=omenu;
    if (typeof noie6fix=="undefined") this._ie6_fix_input();
		else if (!noie6fix) this._ie6_fix_input();

    // 2008-06-03 joel: shadow 
    if (this._browser_type=='ie') {
      oshade=document.getElementById("shade_img_ie");
      kt+=0;
      kl+=0;
      kw=omenu.offsetWidth-0;
      kh=omenu.offsetHeight-0;
    }
    else {
      oshade=document.getElementById("shade_img_ff");
      kt+=2;
      kw=Math.max(Math.round(omenu.offsetWidth*1.18),omenu.offsetWidth+8);
      kh=Math.max(Math.round(omenu.offsetHeight*1.18),omenu.offsetHeight+8);
    }
    if (oshade) {
      omenu.parentNode.appendChild(oshade); // ensure offset calc is correct
      oshade.style.top=kt;
      oshade.style.left=kl;
      oshade.style.width=kw;
      oshade.style.height=kh;
      oshade.style.visibility='visible';
    }
    // alert('omenu:'+omenu.style.left+' oshade:'+oshade.style.left);
  },

  hide: function (menu) {
    omenu=document.getElementById(menu);
    if (!omenu) return; 
    omenu.menu_data.otimeout=
      setTimeout("ctmenu._really_hide('"+menu+"')",omenu.menu_data.delay);

  },

  // 2006-04-28 joel: float menu to a corner ('ne' only now)
  float: function (menu,where) {
    omenu=document.getElementById(menu);
    if (!omenu) return;
    switch (where) {
      case 'NE':
      default:
	omenu.style.left=document.body.scrollLeft+document.body.clientWidth - omenu.offsetWidth - 5;
	omenu.style.top=document.body.scrollTop+2;
	break;
    }
  },

  dispatch_key: function (e) {
    if (typeof e!='String') {
      if (ctmenu._browser_type=='ie') e=window.event;
      if (!e.ctrlKey) return 0;
      ch=String.fromCharCode(e.keyCode);
    }
    else {
      ch=e;
    }
    ch=ch.toLowerCase();

    arr=document.getElementsByName('ctrl-'+ch);
    if (arr[0]) {
      // a tag has href use href, else run the onclick
      if (cmd=arr[0].href) document.location=cmd;
      else arr[0].onclick();
    }
  },

  // ----- internals ---------
  _really_hide: function (menu) {
    omenu=document.getElementById(menu);
    this._clear_hide(omenu);
    omenu.style.visibility='hidden';
    this.count--;
    this._ie6_fix_input('visible');

    // 2008-06-03 joel: shadow 
    oshade=document.getElementById((this._browser_type=='ie')?"shade_img_ie":"shade_img_ff");
    if (oshade && omenu==this.curmenu /* this.count==0 */) {
      oshade.style.visibility='hidden';
    }
  },

  _dynamic_hide: function (omenu,e){
    if ((this._browser_type=='ie')&&!omenu.contains(e.toElement)) 
      this.hide(omenu.id);
    else  {
      if ((this._browser_type=='ff')&&e.currentTarget!= e.relatedTarget&& !this._contains_ff(e.currentTarget, e.relatedTarget)) 
	this.hide(omenu.id);
    }
  },

  _clear_hide: function (omenu) {
    if (omenu.menu_data.otimeout!="none") {
      clearTimeout(omenu.menu_data.otimeout);
      omenu.menu_data.otimeout='none';
    }
//    this._ie6_fix_input('hidden');
  },

  _browser_type:
   (document.all?'ie':(document.getElementById&&!document.all?'ff':null)),

  _browser_ver:
   navigator.appVersion.substr(navigator.appVersion.search(/MSIE /)+5,1),

  _get_edges: function(what){
    var pos={left:what.offsetLeft, top:what.offsetTop}
    var parent=what.offsetParent;
    var buf='';
    // 2007-01-15 joel: add offsetParent positioning check 
    while (parent && parent.style.position!='absolute'){

      // fix a bug displaying elm which has relative parent or 'netcontent' (DE)
      dofix=true;
      granny=parent.offsetParent;
      if (granny) dofix=(granny.style.position!='relative' && granny.id!='netcontent');
      dofix&=(parent.style.position!='relative' && parent.id!='netcontent');

      // 2008-02-07  joel: fixed pages which has netbar which is position:relative
      if (dofix) {
	pos.top+=parent.offsetTop;
      }
      if (dofix||this._browser_type=='ff') {
	pos.left+=parent.offsetLeft; // 2008-08-18 joel: moved out of if-cond, ff need to fix always
      }
      
      //buf+=parent.tagName+'#'+parent.id+'/'+parent.style.position+(dofix?'':'[ignore]')+'('+parent.offsetLeft+','+parent.offsetTop+')'+':'+pos.left+','+pos.top+' | ';
      parent=parent.offsetParent;
    }
    //alert(buf);
    pos.right=pos.left+what.offsetWidth;
    pos.bottom=pos.top+what.offsetHeight;
    return pos;
  },

  _contains_ff: function(a, b) {
    while (b && b.parentNode)
      if ((b = b.parentNode) == a) return true;
    return false;
  },

  // 2008-05-30 joel: get round ie6 input overlaps bug
  _ie6_fix_input: function(vis) {
    if (this._browser_type=='ie' && this._browser_ver=='6') {
      if (this.count>0) vis='hidden';
      else vis='visible';

      iarr=document.getElementsByTagName('INPUT');
      for(var i=0; i<iarr.length; i++) iarr[i].style.visibility=vis;
      iarr=document.getElementsByTagName('SELECT');
      for(var i=0; i<iarr.length; i++) iarr[i].style.visibility=vis;
    }
  },

  // handlers for menu
  _onmouseout: function(e){
    if (ctmenu._browser_type=='ie') e=window.event;
    ctmenu._dynamic_hide(this,e);
  },
  _onmouseover: function(){ctmenu._clear_hide(this);}
}


