/*
 * 
 * This drop down menu was build by Dennis Hoppe ( http://DennisHoppe.de )
 *
 * 
 * Usage example:
 * 
 * $(document).ready(function(){
 *   jQuery('ul#navigation').hoppe_dropdown();
 * });
 *  
 */

(function($){

  $.fn.hoppe_dropdown = function(options){
  
    // Default settings
    settings = {
      'sub_menu_width' : '180px', // Default width of the submenus, values in em/pt/px
      'has_sub_menu_class' : 'has-sub-menu', // This class name will added to list items with sub menus.
      'lowest_z_index' : 1000, // the z-Index of the top menu. All sub menues get a higher z-Index.
      'current_open_class' : 'current-open'
    };
    
    // Merge User options
    if (options) $.extend(settings, options);
    
    // let the Menu drop...
    this
    .css({
      'margin': '0',
      'padding': '0',
      'list-style-type': 'none',
      'position': 'relative',
      'z-index': settings.lowest_z_index
    })

    .find('ul')
      .css({
        'list-style-type': 'none',
        'position': 'absolute',
        'width': settings.sub_menu_width
      })
      .hide()
      .parent('li')
        .addClass('has-sub-menu')
      .end()
    .end()

    .find('a')
      .css({
        'display': 'block'
      })
      .removeAttr('title')
    .end()

    .find('li ul a')
      .css({
        'width': settings.sub_menu_width,
        'float': 'left'
      })
    .end()

    .find('li ul ul')
      .css({
        'left': settings.sub_menu_width,
        'margin-right': '10px'
      })
    .end()

    .find('li')
      .css({
        'float': 'left',
        'position': 'relative'
      })
      .hover(function(){
        $(this)
        .find('ul:first')
          .css({
            'z-index': ( parseInt($(this).parent('ul').css('z-index')) + 10 )
          })
          .show()
          .parent('li')
            .addClass(settings.current_open_class);
      }, function(){
        $(this)
        .removeClass(settings.current_open_class)
        .find('ul:first')
          /*
          .css({
            'visibility': 'hidden',
            'z-index': settings.lowest_z_index
          })
          */
          .hide()
          .parent('li')
            .removeClass(settings.current_open_class);
      })
    .end();
    
    // Support chainability
    return this;
  };
  
})(jQuery);

