/*
* Supersubs v0.2b-jquery plugin
* Copyright (c) 2008 Joel Birch
*
* 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 in automatically adjusts submenu widths of suckerfish-style menus to that
* Their longest list item children. If you use this, please your CT 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 $ = $ (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 = $ ('<li id = "menu-fontsize" >&# 8212; </LI> 'hangzhou.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 varous 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': '123 ',
'White-space': 'normal'
})
// Update offset position of descendant ul to reflect new width of parent
. Each (function (){
VaR $ childul = $ ('> ul', this );
VaR offsetdirection = Invalid 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