/*
 * AwesomeSauceMenu - Awesome and crap.
 * By Caige Nichols (http://www.caigenichols.com)
 * Copyright (c) 2011 caige nichols
 * Licensed under the MIT License: http://www.opensource.org/licenses/mit-license.php
*/

(function( $ ){
  
  $.fn.awesomesaucemenu = function( method ) {  

    var settings = {
      'location'         : 'top',
      'background-color' : '',
      'width' : null
    };
    
    var menu = null;
    
    var methods = {
      init : function( options ) { 
        
        return this.each(function() {

          // If options exist, lets merge them with our default settings.
          if ( options ) { 
            $.extend( settings, options );
          }
          
          menu = $(this);
          // Bind mouseenter event to top level li elements.
          $( '#' + this.id + '> ul > li').bind( 'mouseenter', methods.show );
          // Bind mouseleave event to top level li element.
          $( '#' + this.id + '> ul > li').bind( 'mouseleave', methods.hide );
          // Set some panel settings.
          var panel = $( '#' + this.id + '> ul > li > div' );
          panel.css( 'background-color', settings[ 'background-color' ] );
          
        });
      
      },
      
      show : function(e) {
        $this = $(this);
        var panel = $(this).find( '.awesomesaucemenu-panel' );
        if( panel.length > 0 ) {
          // We need to not display the menu if we're entering the currently selected menu.
          panel.css( 'width', settings[ 'width' ] || menu.outerWidth( true ) );
          panel.css( 'top', menu.outerHeight( true ) - 1 );
          panel.stop(true, true).css( 'visibility', 'visible' ).show().html( panel.html() );          
        } else {
          var panel = $(document).find( '.awesomesaucemenu-panel' );
          panel.stop(true, true).hide();
        }
      },
      
      hide : function( ) { 
        
        var panel = $(this).find( '.awesomesaucemenu-panel' );
        panel.stop(true, true).hide();

      }
      
    };
    
    // Method calling logic.
    if ( methods[method] ) {
      return methods[ method ].apply( this, Array.prototype.slice.call( arguments, 1 ) );
    } else if ( typeof method === 'object' || ! method ) {
      return methods.init.apply( this, arguments );
    } else {
      $.error( 'Method ' +  method + ' does not exist on jQuery.awesomesaucemenu' );
    }          

  };
  
})( jQuery );
