//If this window is within a frame the following line will force it to break out into a full window.
if (window != top) top.location.href = location.href;

$(document).ready(function()
{
   var ie7 = ($.browser.msie && $.browser.version=="7.0");
   $("ul.menu li").children("a").each(
      function()
      {
        var hr=$(this).attr("href");
        if (hr==null || hr=="" || hr=="#") { $(this).attr("href","javascript:void(0);"); }
       //if (!ie7) $(this).html('<span>'+$(this).html()+'</span>');
       //Drop-shadow looks like shit on Italics text under IE
      }
   );

   //IE7 dropdown menu items do not display properly when dropshadows are used!!
   //if (ie7) $("ul.menu > li").children("a").each(function() { $(this).html('<span>'+$(this).html()+'</span>'); });
   //Drop-shadow looks like shit on Italics text under IE

   //append down-arrow for dropdown menus
   $("ul.menu ul").parent().children("a").append("<span style='font-size:90%; font-style:normal;'>&#x25bc;</span>");
   
   AdjustNavBar();   //evenly distribute navbar elements & set width of dropdown to widest element.

   $("ul.menu li ul").each(navDropdown);  //initialize all navbar dropdown menus
   
   //Append Footer
   var d=new Date().getFullYear();
   $("body").append('<div id="footer"><span>&copy;'+d+' Rose Hill &amp; Crew &bull; All Rights Reserved | <a href="Contact.htm" >Contact Us</a></span></div>');
   
   // 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.
   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 AdjustNavBar()
{
   var navbar = document.getElementById("menu");
   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++)
   {
      var navbarchild= navbar.children[i];
      navbarchild.style.marginLeft = incr+'px';
      navbarchild.style.marginRight = (i<(navbar.children.length-1) ? incr+'px' : '0px');

      var j;
      var ul = navbarchild.getElementsByTagName('UL');
      if (ul.length==0) continue;  //menu does not have any sub-menu items.
      ul = ul[0];
      var li = navbarchild.getElementsByTagName('LI');
      if (li.length==0) continue; //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(j=0; j<li.length; j++)
      {
         var w = li[j].clientWidth;
         if (w>maxwidth) maxwidth=w;
         maxheight += li[j].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;
      //force width to be the same as the dropdown box so to ensure LI items always wrap to the next line
      for(j=0; j<li.length; j++) { li[j].style.width = (maxwidth-20)+"px"; } //-20 to adjust for CSS "ul.menu ul li" padding
   }
}

function AdjustNavBar2()
{
   var pwidth = $("ul.menu").width();
   var iwidth=0;
   var $children = $("ul.menu > li");
   $children.each(function() { iwidth+= $(this).width(); });
   var incr = (pwidth-iwidth) / ($children.length*2);
   $children.each(
      function(index)
      {
         $(this).css("margin-left",incr);
         $(this).css("margin-right", index<($children.length-1) ? incr : 0);

         var $dropchildren = $(this).find("ul > li");
         if ($dropchildren.length==0) return;
         var $ul = $dropchildren.eq(0).parent();
         var ul = $ul[0];
         var maxwid=0;
         var maxheit=0;
         $dropchildren.each(
            function()
            {
               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 w = $(this).outerWidth(true);
               if (w>maxwid) maxwid=w;
               maxheit += $(this).outerHeight(true);
               ul.style.position=p;
               ul.style.visibility=v;
               ul.style.display=d;
            }
         );
         $dropchildren.each(function() { $(this).width(maxwid-($(this).outerWidth(true)-$(this).width())); } );
         ul.absHeight = maxheit;    //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.height(maxheit);
         $ul.width(maxwid);
         ul.style.width =maxwid+"px";
      }
   );
}

