/*
*/
;(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 $$ = $(this);
// support metadata
var o = $.meta ? $.extend({}, opts, $$.data()) : opts;
// get the font size of menu.
// .css('fontsize') returns various results cross-browser, so measure an em dash instead
var fontsize = $('
').css({
'padding' : 0,
'position' : 'absolute',
'top' : '-999em',
'width' : 'auto'
}).appendto($$).width(); //clientwidth is faster, but was incorrect here
// remove em dash
$('#menu-fontsize').remove();
// cache all ul elements
$uls = $$.find('ul');
// loop through each ul in menu
$uls.each(function(i) {
// cache this ul
var $ul = $uls.eq(i);
// get all (li) children of this ul
var $lis = $ul.children();
// get all anchor grand-children
var $as = $lis.children('a');
// force content to one line and save current float property
var lifloat = $lis.css('white-space','nowrap').css('float');
// remove width restrictions and floats so elements remain vertically stacked
var emwidth = $ul.add($lis).add($as).css({
'float' : 'none',
'width' : 'auto'
})
// this ul will now be shrink-wrapped to longest li due to position:absolute
// so save its width as ems. clientwidth is 2 times faster than .width() - thanks dan switzer
.end().end()[0].clientwidth / fontsize;
// add more width to ensure lines don't turn over at certain sizes in various browsers
emwidth += o.extrawidth;
// restrict to at least minwidth and at most maxwidth
if (emwidth > o.maxwidth) { emwidth = o.maxwidth; }
else if (emwidth < o.minwidth) { emwidth = o.minwidth; }
emwidth += 'em';
// set ul to width in ems
$ul.css('width',emwidth);
// restore li floats to avoid ie bugs
// set li width to full width of this ul
// revert white-space to normal
$lis.css({
'float' : lifloat,
'width' : '100%',
'white-space' : 'normal'
})
// update offset position of descendant ul to reflect new width of parent
.each(function(){
var $childul = $('>ul',this);
var offsetdirection = $childul.css('left')!==undefined ? 'left' : 'right';
$childul.css(offsetdirection,emwidth);
});
});
});
};
// expose defaults
$.fn.supersubs.defaults = {
minwidth : 9, // requires em unit.
maxwidth : 25, // requires em unit.
extrawidth : 0 // extra width can ensure lines don't sometimes turn over due to slight browser differences in how they round-off values
};
})(jquery); // plugin code ends