var lCurrentNav = null;
var lOverNav = false;
var mx;
var myl;
var my;

var x1, x2, y1, y2;

$(document).ready(function() {

    /*Bind mouse events*/
    $(".btn-whatwedo").hover(function() { toggleTopLevelNav("#whatwedo", $(this)); }, function() { toggleOff("#whatwedo", $(this)); });
    $(".btn-findasite").hover(function() { toggleTopLevelNav("#findasite", $(this)); }, function() { toggleOff("#findasite", $(this)); });
    $(".btn-getfinancing").hover(function() { toggleTopLevelNav("#getfinancing", $(this)); }, function() { toggleOff("#getfinancing", $(this)); });
    $(".btn-demographics").hover(function() { toggleTopLevelNav("#demographics", $(this)); }, function() { toggleOff("#demographics", $(this)); });
    $(".btn-livingandworking").hover(function() { toggleTopLevelNav("#livingandworking", $(this)); }, function() { toggleOff("#livingandworking", $(this)); });
    $(".btn-resources").hover(function() { toggleTopLevelNav("#resources", $(this)); }, function() { toggleOff("#resources", $(this)); });
    $(".btn-joinus").hover(function() { toggleTopLevelNav("#joinus", $(this)); }, function() { toggleOff("#joinus", $(this)); });
    $(".btn-iama").hover(function() { toggleTopLevelNav("#iama", $(this)); }, function() { toggleOff("#iama", $(this)); });

}).mousemove(function(e) {
    mx = e.pageX; myl = my; my = e.pageY;

    if (lOverNav)
    {
        if (mx <= x2 && mx >= x1 && my <= y2 && my >= y1) { /*Leave it*/ }
        else {lOverNav = false; closeTopLevelNav(); /*Hide it*/}
    }
});


function toggleTopLevelNav(pvThisNav, pvjQNav) {
    lOverNav = false;
    
    if (lCurrentNav != null)
    {
        if (lCurrentNav != pvThisNav) { /*Hide the last nav, show new nav*/ closeTopLevelNav(); }
        else { /*Same nav is being requested, it will slide hidden below*/return; }
    }

    lCurrentNav = pvThisNav;

    /*Get height and width of the menu*/
    var h = $(pvThisNav).height();
    var w = $(pvThisNav).width();

    $(pvThisNav).slideToggle("fast");
    $(pvThisNav).css("top", (pvjQNav.innerHeight() + 133) + "px");

    /*Save parameters regarkding current navigation being hovered*/    
    var pos = $(pvThisNav).offset();
    x1 = pos.left;
    y1 = pos.top - 10;
    x2 = x1 + w;
    y2 = y1 + h + 5;

    return;
}

function toggleOff(pvThisNav, pvjQNav) {
   // JavaScriptDebug("(" + mx + "," + my + ") : (" + x1 + "," + y1 + ") by (" + x2 + "," + y2 + ")", false);
    if (mx <= x2 && mx >= x1 && my <= y2 && my >= y1)
    {/*Over the menu shown, can only be hidden when mouse leaves the current nav*/
        lOverNav = true;
    }
    else
    {/*Not over the menu shown*/
        $(pvThisNav).stop().hide().css("height", "");
	lCurrentNav = null;
    }
    
    return;
}

function closeTopLevelNav() {
    if (lCurrentNav != null) { $(lCurrentNav).stop().hide().css("height", ""); }
    lCurrentNav = null;
}
