
/*
 * Supersubs v1 - jQuery plugin
 * Copyright (c) 2011 Georgi Nachev
 *
 * Dual licensed under the MIT and GPL licenses:
 * 	http://www.opensource.org/licenses/mit-license.php
 * 	http://www.gnu.org/licenses/gpl.html
 *
 *
 * This plugin automatically adjusts submenu widths of suckerfish-style menus to that of
 * their longest list item children. If you use this, please expect bugs and report them
 * to the jQuery Google Group with the word 'Superfish' in the subject line.
 *
 */

;(function($){ // $ will refer to jQuery within this closure

	$.fn.supersubs = function(options){
		var opts = $.extend({}, $.fn.supersubs.defaults, options);
		// return original object to support chaining
		return this.each(function() {
			// cache selections
			var $holder = $(this);
			// support metadata
			var o = $.meta ? $.extend({}, opts, $holder.data()) : opts;
			// get the font size of menu.
			
			$('ul',$holder).each(function(){
				$(this).css({visibility: "hidden", display: "block"});
				var el_width = 0;
				$('> li a',this)
				.css({'white-space':'nowrap', 'width'	: 'auto'})
				.each(function(){
					var element = $(this);//.css({'white-space':'nowrap', 'width'	: 'auto'});
					el_width = el_width >= element.outerWidth() ? el_width : element.outerWidth();
				});
				
				el_width = el_width + (el_width/4);
				
				if(o.maxWidth && el_width > o.maxWidth) {
					el_width = o.maxWidth;
				} else if(o.minWidth && el_width < o.minWidth) {
					el_width = o.minWidth;
				}
				
				$(this).css({visibility: "", display: "none", width: el_width});
				$('> li a', this).width(el_width);
				
			});
			
		});
	};
	// expose defaults
	$.fn.supersubs.defaults = {
		minWidth		: false,		// requires px unit.
		maxWidth		: false			// requires px unit.
	};
	
})(jQuery); // plugin code ends

