(function($){
  $.FakeSelect = function( element, options ){
    var default_options = {
      headingClass: 'title',
      bodyClass: 'list'
    };

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

  $.FakeSelect.prototype.extend = $.extend;

  $.FakeSelect.prototype.extend({ 
    initialize: function(){
      this.$heading = this.$element.find( '.' + this.options.headingClass ).first();
      this.$list    = this.$element.find( '.' + this.options.bodyClass ).first();
      this.opened   = false;
      this.timeout  = null;

      this.$list.hide();

      this.$heading.click( $.proxy( this.clicked, this ));

      this.$list.hover( $.proxy( function(){
        window.clearTimeout( this.timeout );
      }, this ),
      
      $.proxy( function(){
        this.timeout = window.setTimeout( $.proxy( this.close, this ), 1000 );
      }, this ));
    },


    clicked: function( event ){
      event.stopPropagation();
      event.preventDefault();

      if ( this.opened ){
        this.$list.slideUp();
        this.opened = false;
      }

      else {
        this.$list.slideDown();
        this.timeout = window.setTimeout( $.proxy( this.close, this ), 3000 );
        this.opened = true;
      }
    },


    close: function(){
      this.$list.slideUp();
      this.opened = false;
    }
  });


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

  // dom ready
  $( function(){
    $( '.fakeSelect' ).fakeSelect();
  });
})(jQuery);

