$(document).ready(function()
{
   var navbar = document.getElementById("menu");
   var a = navbar.getElementsByTagName('A');
   var i;
   for (i=0; i<a.length; i++)
   {
      obj = a[i];   //handy shortcut name
      if (obj.href=="" || obj.href=="#") { obj.href="javascript:void(0);"; }
   }

   $("ul.menu ul").parent().append("<span style='font-size:12px'> &#x25bc;</span>"); //append down-arrow for dropdown menus
   
   //evenly distribute navbar elements
   var parentWidth = navbar.clientWidth;
   var width=0;
   for(i=0; i<navbar.children.length; i++)
   {
      width += navbar.children[i].clientWidth;
   }
   var incr = (parentWidth-width) / (navbar.children.length*2);
   for(i=0; i<navbar.children.length; i++)
   {
      navbar.children[i].style.marginLeft = incr+'px';
      navbar.children[i].style.marginRight = (i<(navbar.children.length-1) ? incr+'px' : '0px');
      subMenuInit(navbar.children[i]); //set width & height of dropdown menus
   }

   $("ul.menu li ul").each(navDropdown);  //initialize all navbar dropdown menus
   
   // Google Analytics - std code ONLY works at end of <body>
   //var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www."); 
   //document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
   //try { var pageTracker = _gat._getTracker("UA-11851965-2"); pageTracker._trackPageview(); } catch(err) {}
   $.geekGaTrackPage('UA-11851965-2');    //SEE: jquery.geekga.js - jQuery plugin for Google Analytics v1.1 (http://www.geekology.co.za/blog/2009/08/speeding-up-google-analytics-load-times-with-jquery-plugin/)
});

function navDropdown() { $(this).parent().eq(0).hoverIntent({ sensitivity:7, interval:200, over:navHoverOver, timeout:500, out:navHoverOut }); }
function navHoverOver()
{
   var current = $('ul:eq(0)', this);
   var h = current[0].absHeight;    //ul.absHeight defined and set in subMenuInit()
   var dur = (1000*h)/120;          //dynamically adjust drop speed relative to height so all dropdowns take the same time to complete.
   current.height(h);               //moving too fast between dropdowns causes property to not be reset to default value.
   current[0].style.opacity = 1.0;  //moving too fast between dropdowns causes property to not be reset to default value.
   1000/114*current[0].absHeight
   current.stop(true).animate({ height: 'show' }, {queue:false, duration:dur, easing: 'easeOutBounce'});
}
function navHoverOut() { $('ul:eq(0)',this).stop(true).animate({height: "hide", opacity: 'hide'}, 600); }

function subMenuInit(subLI)
{
   var i;
   var ul = subLI.getElementsByTagName('UL');
   if (ul.length==0) return;  //menu does not have any sub-menu items.
   ul = ul[0];
   var li = subLI.getElementsByTagName('LI');
   if (li.length==0) return; //UL submenu does not have any LI items. this should never occur.

   //determine maximum width of all the sub-menu items to determine the optimum submenu box width
   var p = ul.style.position;        //submenu LI items do not have dimensions because of property display:none 
   var v = ul.style.visibility;      //so we temporarily apply display:block to force items to have dimension.
   var d = ul.style.display          //we also temporily set visibility:hidden to avoid flicker.
   ul.style.position='absolute';
   ul.style.visibility='hidden';
   ul.style.display='block';
   var maxwidth=0;
   var maxheight=0;
   for(i=0; i<li.length; i++)
   {
      var w = li[i].clientWidth;
      if (w>maxwidth) maxwidth=w;
      maxheight += li[i].clientHeight;
   }
   //ul.style.height = maxheight;  //not settable
   ul.height = maxheight;
   ul.absHeight = maxheight;    //moving too fast between dropdowns causes property to not be reset to default value. Therfore we must keep a private copy so we can manually reset it.
   ul.style.width = maxwidth+"px";
   ul.style.position=p;
   ul.style.visibility=v;
   ul.style.display=d;
   for(i=0; i<li.length; i++)   //force width to be the same as the dropdown box so to ensure LI items always wrap to the next line
   {
      li[i].style.width = (maxwidth-20)+"px";  //-20 to adjust for CSS "ul.menu ul li" padding
   }
}

