// UDMv4.43 // Auto-Sizing Menus extension v1.0 //
/***************************************************************\

  ULTIMATE DROP DOWN MENU Version 4.43 by Brothercake
  http://www.udm4.com/
  
\***************************************************************/

/***************************************************************\
* Set autosizing parameters
\***************************************************************/

um.autosizing = [
	'7px', 		// leeway space ["n" pixels]
	'auto', 	// max-width ["px"|"auto"]
	'auto', 	// min-width ["px"|"auto"]
	'no', 		// size first-level submenus to parent link ["yes"|"no"]
	];

/***************************************************************\
\***************************************************************/




//global object
var ats=new Object;


//add receiver for menu-opening-before-positioning event
um.addReceiver(autoSizeMenu,'058');


//sort array numerically
ats.compare=function(a,b)
{
	return b-a;
};


//get related layer
ats.getRelatedLayer=function(menuNode,menuClass)
{
	//look for node
	ats.node=menuNode.parentNode.lastChild;
	
	//if this is ie55 and iframe is in use
	//and we're looking for a shadow node
	//or if the last child is a text node
	if((um.wie55&&(um.e[13]=='default'||um.e[13]=='iframe')&&menuClass=='udmS')||ats.node.nodeName=='#text')
	{
		//we want the previous sibling
		ats.node=ats.node.previousSibling;
	}
	
	//store node classname, converting null reference for kde's benefit
	ats.noc=um.es(ats.node.className);

	//if there's a node with a classname there - ie, one we added
	if(ats.noc!='')
	{
		//and it has the right class name
		if(ats.noc.indexOf(menuClass)!=-1)
		{
			//return the node
			return ats.node;
		}
		else{return null;}
	}
	else{return null;}
};


//auto-size menu
function autoSizeMenu(menu)
{
	//hide the menu so you don't see it visibly flicker as it resizes
	menu.style.visibility='hidden';
	
	//if a shadow layer exists
	menu.layer=ats.getRelatedLayer(menu,'udmS');
	if(menu.layer!=null)
	{
		//hide the shadow, for the same reason
		menu.layer.style.visibility='hidden';
	}
	
	//get child nodes within this menu
	ats.nodes=menu.childNodes;
	ats.nodesLen=ats.nodes.length;
	
	//store box-model difference 
	//which is menu left/right padding and border widths 
	ats.boxdiff=um.e[51]*2+um.e[55]*2;

	//store classname, converting null reference for kde's benefit
	ats.ppc=um.es(menu.parentNode.parentNode.className);

	//if we're sizing first-level from parent item, and this is a first-level menu 
	if(um.autosizing[3]=='yes'&&ats.ppc=='udm')
	{
		//store the longest width value to the width of the parent item, 
		//plus leeway space, minus box-model difference if we're mac/ie5 or not in quirks mode
		ats.longest=menu.parentNode.offsetWidth+um.pi(um.autosizing[0])-((um.q&&!um.mie)?0:ats.boxdiff);
	}
	
	//or if we're autosizing all the way, or it's not a first-level menu
	else
	{
		//array of link widths
		ats.widths=[];

		//for each node in this menu
		for(var i=0;i<ats.nodesLen;i++)
		{
			//if it's not a text node
			if(ats.nodes[i].nodeName != '#text') 
			{
				//get the link object
				ats.link=um.gc(ats.nodes[i]);

				//if this is win/ie5.0 (which doesn't support white-space:nowrap)
				if(um.wie50)
				{
					//we can't set links to inline display, because it breaks the structure
					//so we're relying on win/ie's behavior to expand elements which have too much content
					//this means that elements can only expand from their current width, not contract
					//so we have to set the link to have a very small initial width
					ats.link.style.width='1em';
					
					//get the link inner HTML
					ats.html=ats.link.innerHTML;
					
					//write it back with a surrounding <nobr> tag
					//which has nullified letter-spacing 
					//so it takes up no more space than the original text
					ats.link.innerHTML='<nobr style="letter-spacing:0;">'+ats.html+'</nobr>';
					
					//add its width to the link widths array
					ats.widths[ats.widths.length]=ats.link.offsetWidth;

					//write it back as it was
					ats.link.innerHTML=ats.html;
					
					//restore its previous width
					ats.link.style.width='100%';
				}
				
				//for other browsers
				else
				{
					//set it to inline display, so the element can expand
					//and disallow white-space wrapping so that it does
					ats.link.style.display='inline';
					ats.link.style.whiteSpace='nowrap';

					//add its width to the link widths array
					ats.widths[ats.widths.length]=ats.link.offsetWidth;

					//set it back to block-level display and normal white-space wrapping
					ats.link.style.display='block';
					ats.link.style.whiteSpace='normal';
				}
			}
		}

		//sort the widths array numerically
		ats.widths=ats.widths.sort(ats.compare);

		//store the longest width, plus leeway space
		//plus box-model difference if we're in quirks mode
		//plus item left/right margins 
		//ats.longest=ats.widths[0]+um.pi(um.autosizing[0])+(um.q?ats.boxdiff:0)+um.e[61]*2;
		//plus menu arrow width, if menu arrows are in use
		ats.longest=ats.widths[0]+um.pi(um.autosizing[0])+(um.q?ats.boxdiff:0)+um.e[61]*2+(um.e[89]!='none'?um.e[91]:0);
	
		//if autosizing is not automatic
		//and width is greater than max-width 
		//allowing for box-model difference if we're not in quirks mode
		if(um.autosizing[1]!='auto'&&(ats.longest-(um.q?0:ats.boxdiff))>um.pi(um.autosizing[1]))
		{
			//constrain to that width
			//minus box-model difference if we're not in quirks mode
			ats.longest=um.pi(um.autosizing[1])-(um.q?0:ats.boxdiff);
		}
	
		//if autosizing is not automatic
		//and width is less than min-width
		//allowing for box-model difference if we're not in quirks mode
		if(um.autosizing[2]!='auto'&&(ats.longest-(um.q?0:ats.boxdiff))<um.pi(um.autosizing[2]))
		{
			//constrain to that width
			//minus box-model difference if we're not in quirks mode
			ats.longest=um.pi(um.autosizing[2])-(um.q?0:ats.boxdiff);
		}
	}
	
	//set the menu to the stored longest width
	menu.style.width=ats.longest+'px';

	//for each node in this menu
	for(i=0;i<ats.nodesLen;i++)
	{
		//if it's not a text node
		if(ats.nodes[i].nodeName!='#text') 
		{
			//look for an arrow object
			//not for mac/ie5, or if arrow function doesn't exist
			//which will happen when using a server-side config with no arrows
			ats.ar=(!um.mie&&typeof um.n.ga=='function')?um.n.ga(um.gc(ats.nodes[i])):null;

			//if there is one
			if(ats.ar!=null)
			{
				//re-apply arrow padding, so the positions take account of new menu size
				//because the first time this happened the menus hadn't been sized yet
				um.n.wp(ats.ar,ats.nodes[i],um.e[70],um.e[62],0);
			}
		}
	}
	
	//look for shadow and iframe cover layers
	menu.layers=['udmS','udmC'];
	for(i=0;i<2;i++)
	{
		//if layer exists
		menu.layer=ats.getRelatedLayer(menu,menu.layers[i]);
		if(menu.layer!=null)
		{
			//set the width and height to the new dimensions of the menu
			menu.layer.style.width=menu.offsetWidth + 'px';
			menu.layer.style.height=menu.offsetHeight + 'px';

			//show the layer (because shadow will be hidden)
			menu.layer.style.visibility='visible';
		}
	}

	//show the menu
	menu.style.visibility='visible';
};







