// jNav.js
// jNav.js is Jeremy's Drop-down menu system for web navigation.
// Copyright 2007-2008, Jeremy R. Geerdes
// All rights reserved.
// For more information or a project quote, jgeerdes@mchsi.com

// To use, simply call new jNav(containerElement|id,options)
// Options is an object literal with the following recognized values are:
//  dropDown : boolean, tells opening menu to drop down, rather than open out and down
//  right : boolean, tells menu to open to the right, rather than left
//  up : boolean, tells menu to open upward, rather than downward

  (function(){
  	var isIEblah=navigator.userAgent.match(/MSIE ?(\d)*/i);
  	isIEblah=isIEblah&&parseInt(isIEblah[1])<=6?1:0;
   function jNav(a,options) // Jeremy's Navigation. For info, jgeerdes@mchsi.com
    {
    this.a = (typeof(a) == 'string') ? document.getElementById(a) : a;
    this.options = options;
    this.b = [];
    for (var z=0;z<this.a.childNodes.length;z++)
     {
     var c = this.a.childNodes[z];
     if (c.tagName == 'DIV') this.b.push(new jNavMenu(c,this.options));
     }
    this.redraw();
    }
   jNav.prototype.getContainer = function(){return this.a;}
   jNav.prototype.getMenus = function(){return this.b;}
   jNav.prototype.on = function(){this.getContainer().style.display = 'block';this.getContainer().style.visibility = 'visible';}
   jNav.prototype.off = function(){this.getContainer().style.display = 'none';this.getContainer().style.visibility = 'hidden';}
   jNav.prototype.redraw = function()
    {
    this.off();
    this.on();
    }
   function jNavMenu(a,options)
    {
    var m = this;
    this.occounter=0;
    this.a = (typeof(a) == 'string') ? document.getElementById(a) : a;
    this.a.className = 'jNavMenu';
    this.a.style.position = 'relative';
    this.options = (options) ? options : {};
    this.b = ((this.options) && (this.options['dropDown'])) ? 1 : 0;
    if (this.b) this.a.style.height = '100%';
    this.c = 0;
    this.d = 0;
    for (var z=0;z<this.a.childNodes.length;z++)
     {
     var y = this.a.childNodes[z];
     if (y.tagName == 'A') this.e = y;
     else if (y.tagName == 'DIV') this.f = y;
     }
    this.i = [];
    if (this.f)
     {
     for (var z=0;z<this.f.childNodes.length;z++)
      {
      var y = this.f.childNodes[z];
      var sub_options = clone(this.options);
      sub_options.dropDown = 0;
      if (y.tagName == 'DIV') this.i.push(new jNavMenu(y,sub_options));
      }
     this.f.className = 'jNavSubMenu';
     this.f.style.position = 'absolute';
     this.f.style.left = (((!this.b) && (this.e)) ? this.e.offsetWidth : 0)+'px';
     if (!this.b) this.f.style.top = '0px';
     this.f.style.zIndex = 10000;
     }
    this.redraw();
    if (this.e) this.e.className = 'jNavMenuLabel';
    this.g = (this.f) ? this.f.offsetWidth : 0;
    this.h = (this.f) ? this.f.offsetHeight : 0;
    this.closeMenu();
    if (a.addEventListener)
     {
     a.addEventListener('mouseover',function(){m.openMenu();},false);
     this.a.addEventListener('mouseout',function(e){m.closeMenu(e);},false);
     }
    else if (a.attachEvent)
     {
     a.attachEvent('onmouseover',function(){m.openMenu();});
     this.a.attachEvent('onmouseout',function(e){m.closeMenu(e);});
     }
    else
     {
     a.onmouseover = function(){m.openMenu();};
     this.a.onmouseout = function(e){m.closeMenu(e);};
     }
    }
   jNavMenu.prototype.openMenu = function()
    {
    this.occounter++;
    var m = this;
    this.getContainer().className += ' active';
    this.getLabel().className += ' active';
    if (!this.getMenu()) return false;
    this.getMenu().style.display = 'block';
    if (this.j) clearTimeout(this.j);
    var a = this.getMenu();
    var b = Math.floor(this.height()/5);
    var c = Math.floor(this.width()/5);
    var d = a.offsetHeight - parseInt(currStyle(a,'height'));
    var e = a.offsetWidth - parseInt(currStyle(a,'width'));
    if (a.offsetHeight < this.height()) a.style.height = ((a.offsetHeight+b<this.height()) ? parseInt(currStyle(a,'height'))+b : this.height()-d)+'px';
    if (a.offsetWidth < this.width()) a.style.width = ((a.offsetWidth+c<this.width()) ? parseInt(currStyle(a,'width'))+c : this.width()-e)+'px';
    a.style.left=this.x()+'px';
    a.style.top = this.y()+'px';
    if((a.offsetHeight<this.height()||a.offsetWidth<this.width())&&this.occounter<500){this.j=setTimeout(function(){m.openMenu();},50);}
    else{a.style.overflow='visible';this.occounter=0;}
    };
   jNavMenu.prototype.closeMenu = function(e)
    {
    var m = this;
    if (!e) e = window.event;
//if(e&&navigator.userAgent.indexOf('MSIE')!=-1){alert('e: '+typeof(e)+'\noccounter: '+this.occounter);for(var ee in e){alert(ee+': '+typeof(e[ee]));}}
    if(e&&(e.srcElement||e.target)){


    	var x=e.srcElement?e.srcElement:e.target;
    	try{var y=e.toElement?e.toElement:e.relatedTarget;}catch(e){alert(e);return;}
     while(y){if(y==x||y==this.getContainer()){return false;}try{y=y.parentNode;}catch(z){y=undefined;}}
    }
    this.getContainer().className = this.getContainer().className.replace(/ active/g,'');
    this.getLabel().className = this.getLabel().className.replace(/ active/g,'');
    if (!this.getMenu()) return false;
    var a = this.getMenu();
    this.occounter++;
    if (this.j) clearTimeout(this.j);
    a.style.overflow = 'hidden';
    var b = Math.floor(this.height()/2);
    var c = Math.floor(this.width()/2);
    if (a.offsetHeight > 0) a.style.height = ((parseInt(currStyle(a,'height'))-b>0) ? parseInt(currStyle(a,'height'))-b : 0)+'px';
    if ((!this.b) && (a.offsetWidth > 0)) a.style.width = ((parseInt(currStyle(a,'width'))-c>0) ? parseInt(currStyle(a,'width'))-c : 0)+'px';
    if (parseInt(currStyle(a,'height')) > 0&&this.occounter<500){this.j = setTimeout(function(){m.closeMenu();},50);}
    else{this.getMenu().style.display = 'none';this.occounter=0;}
    };
   jNavMenu.prototype.resizeMenu = function(a,b)
    {
    if ((!a) || (!b)) return false;
    if (this.j) clearTimeout(this.j);
    if (this.rT) clearTimeout(this.rT);
    var m = this;
    var c;
    if (typeof(a) == 'number') c = a;
    else
     {
     c = a.offsetHeight;
     var el = a;
     while (el != this.getMenu())
      {
      c += el.offsetTop;
      el = el.offsetParent;
      }
     }
    var d;
    if (typeof(b) == 'number') d = b;
    else
     {
     d = b.offsetWidth;
     var el = b;
     while (el != this.getMenu())
      {
      d += el.offsetTop;
      el = el.offsetParent;
      }
     }
    if (this.isClosed())
     {
     this.height(c);
     this.width(d);
     return;
     }
    this.getMenu().style.overflow = 'hidden';
    if (this.getMenu().offsetWidth != d)
     {
     var e = parseInt(currStyle(this.getMenu(),'width'));
     var f = this.getMenu().offsetWidth;
     var g = (d-this.width())/5;
     g = (g<0) ? Math.floor(g) : Math.ceil(g);
     var width = ((((f<d) && (f+g>d)) || ((f>d) && (f+g<d))) ? d-(f-e) : e+g)+'px';
     //if(!confirm('d: '+d+'\ne: '+e+'\nf: '+f+'\ng: '+g+'\n'+width)) return false;
     this.getMenu().style.width = width;
     this.getMenu().style.left = this.x(d)+'px';
     }
    if (this.getMenu().offsetHeight != c)
     {
     var h = parseInt(currStyle(this.getMenu(),'height'));
     var i = this.getMenu().offsetHeight;
     var j = (c-this.height())/5;
     j = (j<0) ? Math.floor(j) : Math.ceil(j);
     var height = ((((i<c) && (i+j>c)) || ((i>c) && (i+j<c))) ? c-(i-h) : h+j)+'px';
     //if(!confirm('c: '+c+'\nthis.height(): '+this.height()+'\nh: '+h+'\ni: '+i+'\nj: '+j+'\n'+height)) return false;
     this.getMenu().style.height = height;
     this.getMenu().style.top = this.y(c)+'px';
     }
    //if(!confirm('height: '+c+' : '+this.getMenu().offsetHeight+'\nwidth: '+d+' : '+this.getMenu().offsetWidth)) return false;
    if ((this.getMenu().offsetHeight != c) || (this.getMenu().offsetWidth != d)) this.rT = setTimeout(function(){m.resizeMenu(a,b);},50);
    else
     {
     this.height(c);
     this.width(d);
     this.getMenu().style.overflow = 'visible';
     }
    };
   jNavMenu.prototype.isClosed = function(){return (this.getMenu().style.display == 'none') ? true : false;};
   jNavMenu.prototype.on = function(){this.getContainer().style.display = 'block';};
   jNavMenu.prototype.off = function(){this.getContainer().style.display = 'none';};
   jNavMenu.prototype.redraw = function()
    {
    this.off();
    this.on();
    };
   jNavMenu.prototype.getContainer = function(){return this.a;};
   jNavMenu.prototype.width = function(a){if (a!=undefined){this.g = a;}return this.g;};
   jNavMenu.prototype.height = function(a){if (a!=undefined){this.h = a;}return this.h;};
   jNavMenu.prototype.getLabel = function(){return this.e;};
   jNavMenu.prototype.getMenu = function(){return this.f;};
   jNavMenu.prototype.getSubMenus = function(){return this.i;};
   jNavMenu.prototype.x = function(x)
    {
    var a = 0;
    var b = this.getContainer();
    var c;
    while (b)
     {
     var d = (currStyle(b,'overflow')=='hidden') ? currStyle(b,'overflow') : currStyle(b,'overflowX');
     if (d == 'hidden')
      {
      c = parseInt(currStyle(b,'width'));
      break;
      }
     a += b.offsetLeft;
     b = b.offsetParent;
     }
    if (!c)
     {
     c = (window.innerWidth) ? window.innerWidth : document.body.offsetWidth;
     c += (window.pageXOffset) ? window.pageXOffset : document.body.scrollLeft;
     }
    var f = (this.b) ? this.c : this.getContainer().offsetWidth;
    if (x==undefined) x = this.width();
    var pr=(!isNaN(parseInt(currStyle(this.getContainer(),'paddingRight'))))?parseInt(currStyle(this.getContainer(),'paddingRight')):0;
    if (((a+f+x > c) && (!this.options['suppressXAdjust'])) || ((this.options) && (this.options['right']))) f = -this.getMenu().offsetWidth + ((this.b) ? this.getContainer().offsetWidth : 0);
//    f -= (!isNaN(parseInt(currStyle(this.getContainer(),'paddingLeft'))))?parseInt(currStyle(this.getContainer(),'paddingLeft')):0;
    f -= (!isNaN(parseInt(currStyle(this.getContainer(),'borderLeftWidth'))))?parseInt(currStyle(this.getContainer(),'borderLeftWidth')):0;
    return f;
    };
   jNavMenu.prototype.y = function(y)
    {
    var a = 0;
    var b = this.getContainer();
    var c;
    while (b)
     {
     var d = (currStyle(b,'overflow')=='hidden') ? currStyle(b,'overflow') : currStyle(b,'overflowY');
     a += b.offsetTop;
     if (d == 'hidden')
      {
      c = parseInt(currStyle(b,'height'));
      break;
      }
     b = b.offsetParent;
     }
    if (!c)
     {
     c = (window.innerHeight) ? window.innerHeight : document.body.clientHeight;
     c += (window.pageYOffset) ? window.pageYOffset : document.body.scrollTop;
     }
    var f = (this.b) ? this.getContainer().clientHeight : this.d;
    if (!y) y = (this.getMenu().offsetTop < 0) ? this.getMenu().offsetTop : this.height();
    if (((a+f+y > c) && (!this.options['suppressYAdjust'])) || ((this.options) && (this.options['up']))) f = -this.getMenu().offsetHeight + ((this.b) ? this.getContainer().offsetHeight : 0);
    f -= parseInt(((currStyle(this.getContainer(),'paddingTop')!='')) ? parseInt(currStyle(this.getContainer(),'paddingTop')) : parseInt(currStyle(this.getContainer(),'padding')));
    var g=parseInt(currStyle(this.getContainer(),'borderTopWidth'));
    f-=(!isNaN(g))?g:0;
    return f;
    };
   function clone(a)
    {
    if ((typeof(a) != 'object') || (a == null)) return a;
    var b={};
    for (var z in a) b[z] = clone(a[z]);
    return b;
    }
   function currStyle(a,b)
    {
    var c;
    if (typeof(a) == 'string') a = document.getElementById(a);
    if (a.currentStyle) c = a.currentStyle[b];
    else
     {
     var d = b.replace(/([A-Z])/g, "-$1");
     d = d.toLowerCase();
     try { c = document.defaultView.getComputedStyle(a,null).getPropertyValue(d); }
     catch(e) { c = a.style[b]; }
     }
    return c;
    }
 function ua(a,b,c){var d='on';if(!c){c=window;}if(typeof(a)=='string'){a=[a];}for(var f in a){var e=a[f];if(c.addEventListener){c.addEventListener(e,b,0);}else if(c.attachEvent){c.attachEvent(d+e,b);}else if(typeof(c[d+e])=='function'){var f=c[d+e];c[d+e]=function(){f();b();};}else{c[d+e]=b;}}}
 function ub(a,c,b){if(!b){b=window;}if(b.removeEventListener){b.removeEventListener(a,c,0);}else if(b.detachEvent){b.detachEvent('on'+a,c);}else{b['on'+a]=null;}}
   if(!window.jeremy){window.jeremy={addEvent:ua,removeEvent:ub}};
   jeremy.jNav={};
   jeremy.jNav.jNav=jNav;
  })();
