(function($){
  $.AccessibleNav = function( element, options ){
    var default_options = {
    };

    this.options  = $.extend( default_options, options );
    this.$element = $( element );
    this.element  = element;
    this.initialize();
  };

  $.AccessibleNav.prototype.extend = $.extend;

  $.AccessibleNav.prototype.extend({ 
    initialize: function(){
      this.show_callback  = this.$element[0].onmouseover;
      this.hide_callback  = this.$element[0].onmouseout;
      this.$trigger       = this.$element.children( 'a' ).first();
      this.$submenu       = this.$element.children( 'div' ).first();

      if ( ! ( this.show_callback && this.hide_callback && this.$submenu ) ){
        return false;
      }

      this.$trigger.focus( $.proxy( this.show, this ) );
      this.$trigger.blur( $.proxy( this.wait, this ) );
      this.$submenu.find( 'a' ).focus( $.proxy( this.clearTimer, this ) );
      this.$submenu.find( 'a' ).blur( $.proxy( this.wait, this ) );
    },


    // show the submenu
    show: function(){
      this.show_callback.call( this.element );
    },


    // set a timer to hide the submenu
    wait: function(){
      this.timeout = window.setTimeout( $.proxy( function(){
        this.hide();
      }, this ), 50 );
    },


    clearTimer: function(){
      if ( this.timeout ){
        window.clearTimeout( this.timeout );
      }
    },


    // close the submenu
    hide: function(){
      this.hide_callback.call( this.element );
    }
  });


  $.fn.accessibleNav = function( options ){
    return this.each( function(){
      ( new $.AccessibleNav( this, options ) );
    });
  };

  $(function(){
    $( '.header .menu > ul > li' ).accessibleNav();
  });
})(jQuery);
