$(document).ready(function() {

if ($("#snav").find("ul.defaultMenu").length){
 var GBL_bNoDefaultMenu  = false;
 $("#snav").show(); 
} else {
 var GBL_bNoDefaultMenu  = true;
}

$("#buttons li, div#snav ul").hover (function (zEvent) {MenuOpenCloseErgoTimer (zEvent); } );

function MenuOpenCloseErgoTimer (zEvent)
{
    var dDelay
    var ActionFunction = null;

    if (zEvent.type == 'mouseenter')
    {
        if (zEvent.currentTarget.nodeName  == 'LI')        //-- Main nav.
        {
            dDelay          = 300;
            ActionFunction  = function (node) {
                //--- The first class is a special tie to a submenu, if it exists
                var subnav = 'ul.snav-' + $(node).attr ('class').split (' ')[0];
                $("#snav ul").hide ();
                $("#snav").find (subnav).show ();

                if (GBL_bNoDefaultMenu){
                    if($("#snav " + subnav).length){
                    $("#snav").stop (true, true).slideDown ('fast');
                    }
                }
            };
        }
        else //-- zEvent.currentTarget.nodeName  == 'UL'   //-- Sub nav.
        {
            dDelay          = 0;
            ActionFunction  = function (node) {
                $(node).show ();

                if (GBL_bNoDefaultMenu)
                    $("#snav").stop (true, true).slideDown ('fast');
            };
        }
    }
    else //-- zEvent.type == 'mouseleave'
    {
        //--- Actions for main nav and sub nav are the same.
        dDelay          = 200;
        ActionFunction  = function (node) {
            if (GBL_bNoDefaultMenu)
                $("#snav").stop (true, true).slideUp ('fast');
            $("#snav ul").hide ();
            $("#snav ul.defaultMenu").show ();
        }
    }

    if (typeof this.delayTimer == "number")
    {
        clearTimeout (this.delayTimer);
    }
    this.delayTimer     = setTimeout (function() { ActionFunction (zEvent.currentTarget); }, dDelay);
}

   $("a.tb").click(function(e){
    e.preventDefault();
    var el = "#" + $(this).attr("rel");
    
    if($(el).length){
     $(el).modal({
      autoResize: true,
      onShow: function(d){
       d.container.css({height:d.data.outerHeight(true),width:d.data.outerWidth(true)});
       $.modal.setContainerDimensions();
      }
    });
    }
   });

   $(".gb").click(function(e){
    e.preventDefault();
    var url = $(this).attr("href");

    $.get(url, function(data){
     $(data).modal({
      containerId: 'simplemodal-container-egb',
      onOpen: function(dialog){
       dialog.overlay.fadeIn('slow', function(){
        dialog.container.fadeIn('slow', function(){
         dialog.data.fadeIn('slow');
        });
       });
      }
     });
    });
   });
 
});
