function NavBarClick(ix)
{
  var page = NavBar[ix]['hrefs'][0];
  window.location = page;
}

function NavBarOver(ix)
{
  if (ix != CurrentNav) CloseFlyout();
  ClearMouseMove();
  CurrentNav = ix;
  var id='#'+NavBar[ix].id;
  var img=NavBar[ix].img_over;
  $(id).attr('src',img);
  ShowFlyout(ix);
}

function NavBarOut(ix)
{
  CurrentNav = -1;
  var id='#'+NavBar[ix].id;
  var img=NavBar[ix].img_out;
  $(id).attr('src',img);
  // flyout may be longer than navbar, so no hide here
}

var IsBrowserIE = true;
var FlyoutButtonWidth = 200;
var FlyoutButtonHeight = 30;
var MenuOutLeft = -1;
var MenuOutRight = -1;
var MenuOutTop = -1;
var MenuOutBottom = -1;
var CurrentNav = -1;
function ShowFlyout(ix)
{ 
  var fly;
  var menu = "";
  var id;
  var jx;
  var navoffset;
  
//  if (ix == CurrentNav) return; // already showing this one
  if ((ix < 0) || ((ix+1) >= NavBar.length)) return; // telling me something wrong
  if (NavBar[ix]['texts'].length <= 0) return; // no flyout - will have default href, tho!
  
  var id='#'+NavBar[ix].id;

  var navoffset = $(id).offset(); // sets top, left
  navoffset.width = $(id).width(); 
  navoffset.height = $(id).height(); 

  var flyoffset = Object();
  flyoffset.width = FlyoutButtonWidth;
  flyoffset.height = NavBar[ix]['hrefs'].length * FlyoutButtonHeight;
  flyoffset.left = navoffset.left + navoffset.width;
  flyoffset.top = navoffset.top;
  if (flyoffset.height < navoffset.height)
  {
    flyoffset.top += parseInt((navoffset.height-flyoffset.height)/2);
  }

  fly = $("#Flyout");
  fly.css('left',flyoffset.left);
  fly.css('top',flyoffset.top);
  fly.css('width',flyoffset.width);
  fly.css('height',flyoffset.height);

  menu += "<table cellpadding='0' cellspacing='0' style='width:100%;height:"+flyoffset.height+"px;' class='FlyoutBackground'>";
  for (jx = 0; jx < NavBar[ix].texts.length; jx++)
  {
    menu += "  <tr><td align='center' valign='middle' class='FlyoutBackground'> " + 
            "  <a target='" + NavBar[ix]['targets'][jx] + "' class='FlyoutText' href='" + NavBar[ix]['hrefs'][jx] + "'>" + 
            NavBar[ix]['texts'][jx] + 
            "</a></td></tr>";
  }
  menu += "</table>";
  fly.html(menu);
  fly.css('display','block');

  // build rectangular area that includes flyout and navbar item
  MenuOutLeft = navoffset.left;
  MenuOutTop = navoffset.top;
  MenuOutRight = MenuOutLeft + navoffset.width + flyoffset.width;
  if (flyoffset.height >= navoffset.height)
  {
    MenuOutBottom = navoffset.top + flyoffset.height;
  }
  else
  {
    MenuOutBottom = navoffset.top + navoffset.height;
  }
  window.setTimeout(SetMouseMove,500); // otherwise, could close immediately
}

function SetMouseMove()
{
  if (IsBrowserIE)
    document.onmousemove = HideFlyout;
  else
    window.onmousemove = HideFlyout;
}
  
function ClearMouseMove()
{
  if (IsBrowserIE)
    document.onmousemove = null;
  else
    window.onmousemove = null;
}

function HideFlyout(e)
{   
  var evt;
  if (!e)
  {
    evt = window.event; // ie
  }
  else
  {
    evt = e; // mozilla
  }
  if (IsBrowserIE)
  {
    xpos = evt.clientX + document.documentElement.scrollLeft + document.body.scrollLeft;
    ypos = evt.clientY + document.documentElement.scrollTop + document.body.scrollTop;
  }
  else
  {
    xpos = evt.clientX + window.scrollX;
    ypos = evt.clientY + window.scrollY;
  }

//alert('x:' + xpos + ' y:' + ypos + ' outtop:' + MenuOutTop + ' outright:' + MenuOutRight + ' outleft:' + MenuOutLeft + ' outbottom:' + MenuOutBottom);
  if ((xpos <= MenuOutLeft) || (ypos <= MenuOutTop) || (xpos >= MenuOutRight) || (ypos >= MenuOutBottom))
  {
    CloseFlyout();
    ClearMouseMove();
  }
}

function CloseFlyout()
{
  var fly = $("#Flyout");
  if (fly)  fly.css('display','none');
  CurrentNav = -1;
}
    
IsBrowserIE = true;
$(document).ready(function()
{
  if (window.GetBrowserAndVersion)
  // just in case function is not included in file that includes navbar
  {
    GetBrowserAndVersion();
    if (BrowserName != BROWSER_MSIE)
    {
      IsBrowserIE = false;
    }
  }
  else
  {
    if (!document.all)
    {
      IsBrowserIE = false;
    }
  }  
//  $.preLoadImages('','');
});
