$(document).ready(function(){

  $(document).pngFix();

  $.fn.extend({
    has: function( target ) { 
      var targets = jQuery(target); 
      return this.filter(function() { 
            for( var i=0, j=targets.length; i<j; i++ ) 
                try{ if(jQuery.contains( this, targets[i] )) return true; }catch(e){}
      }); 
    }
  });

  $('body').click(function(event){
    if ($('#nav li, #subnavs .subnav').has(event.target).length == 0) {
      $('#subnavs .subnav').fadeOut('slow');
      $('ul#nav li').removeClass('selected');
      $('ul#nav li').find('a').each(function(){
        if (!$(this).parent().is('.current')) {
          $(this).css({
            backgroundColor: 'transparent',
            color: '#000099'
          });
        }
      });
    }
  });
  
  $('ul#nav > li').hover(
    function(){
      if (!$(this).is('.normalHover')) {
        $('ul#nav li').find('a').each(function(){
          if (!$(this).parent().is('.current')) {
            $(this).css({
              backgroundColor: 'transparent',
              color: '#000099'
            });
          }
          });
      
        $(this).find('a').css({
          backgroundColor: '#0098F0',
          color: '#FFF'
        });
        $('#subnavs .subnav').fadeOut('slow');
        $('ul#nav li').removeClass('selected');
      }
    },
    function(){
      if (!$(this).is('.normalHover')) {
        var tab = $(this).attr('nav');
        if (!$('#subnavs .subnav.' + tab).is(':visible') && !$(this).is('.current')) {
          $(this).find('a').css({
            backgroundColor: 'transparent',
            color: '#000099'
          });
        }
      }
    }
  );
  
  $('ul#nav li').click(function(event){
    if (!$(this).is('.normalClick')) {
      var tab = $(this).attr('nav'),
          pos = $(this).position();

      $(this).addClass('selected');
      $('#subnavs .subnav').slideUp('slow');
      $('#subnavs .subnav.' + tab).css({
        left: ($(this).is('.last') ? pos.left - 106 : pos.left+5),
        top: pos.top + 20
      }).fadeIn('fast').show();
  
      return false;
    }
  });
  
});