// UDMv4.5 // Snapshot Cookie extension v1.03 //
/***************************************************************\

  ULTIMATE DROP DOWN MENU Version 4.5 by Brothercake
  http://www.udm4.com/

\***************************************************************/




//global object
var sc = new Object;


//add receivers for menu open and close events
um.addReceiver(takeSnapshot,'060');
um.addReceiver(takeSnapshot,'070');


//add receiver for navbar ready event
um.addReceiver(applySnapshot,'010');



//take scshot
function takeSnapshot(eventObject)
{
	//scshot data string
	sc.string = '';

	//get all menus
	sc.uls = umTree.getElementsByTagName('ul');
	sc.ulsLen = sc.uls.length;

	//get relevant submenus
	//event object and all ancestors up to navbar root
	sc.count = 0;
	sc.openMenus = [eventObject];
	while(eventObject.parentNode.parentNode.className != 'udm')
	{
		//set object to parent menu
		eventObject = eventObject.parentNode.parentNode;

		//record object
		sc.openMenus[++sc.count] = eventObject;
	}

	//sc.openMenus = umTree.getElementsByTagName('ul');

	//relevant submenus
	sc.menusLen = sc.openMenus.length;

	//for each  revelant menu
	for(var i=0; i<sc.menusLen; i++)
	{
		//for each of all menus
		for(var j=0; j<sc.ulsLen; j++)
		{
			//if menus is this one
			if(sc.uls[j] == sc.openMenus[i])
			{
				//stop to preserve the "j" value
				//which is the menu's index in the array
				break;
			}
		}

		//save its visible state and position to data string
		//cookies are limited to 4K of data; each entry is between 10-30 bytes
		//you'd have to have more than 100 menus before it becomes a problem
		sc.string += ''
			+ j
			+ ':'
			+ ((sc.openMenus[i].style.visibility == 'visible') ? 'y' : 'n')
			+ ':'
			+ sc.openMenus[i].style.left
			+ ':'
			+ sc.openMenus[i].style.top
			+ ':'
			+ sc.openMenus[i].style.marginLeft
			+ ':'
			+ sc.openMenus[i].style.marginTop
			+ '';

		//add separator
		sc.string += (i < (sc.menusLen-1)) ? ',' : '';
	}


	//document.title = (sc.string);

	//save data to sessional cookie
	document.cookie = 'udmSnapshot=' + sc.string + '; path=/';
};




//apply scshot
function applySnapshot()
{
	//check for cookie
	if(document.cookie)
	{
		//split cookie into individuals
		sc.cookie = document.cookie.split(';');
		sc.cookieLen = sc.cookie.length;

		//for each cookie
		for(i=0; i<sc.cookieLen; i++)
		{
			//if this cookie is called 'udmSnapshot'
			if(/udmSnapshot/.test(sc.cookie[i]))
			{

				//document.title = sc.cookie[i];

				//split cookie into menu groups
				sc.sc = sc.cookie[i].split('=');
				sc.sc = sc.sc[1].split(',');
				sc.scLen = sc.sc.length;

				//for each menu group
				//iterate bacwards because array is stored backwards
				//so otherwise child shadows and cover layers won't be created
				for(var j=sc.scLen; j--;)
				{
					//split into values
					sc.sc[j] = sc.sc[j].split(':');

					//if visibility value is 'y'
					if(sc.sc[j][1] == 'y')
					{
						//get menu at index value
						sc.menu = umTree.getElementsByTagName('ul')[ parseInt(sc.sc[j][0],10) ];

						//if it exists
						if(sc.menu)
						{
							//reset offleft positioning properties
							sc.menu.style.height='auto';
							sc.menu.style.overflow='visible';
							sc.menu.style.left='auto';
							//if this is an expanding menu
							if(um.ep)
							{
								//set the menu position to relative
								sc.menu.style.position='relative';
								//sc.menu.style.position='static';
							}

							//pre-display menu
							um.xd(sc.menu);

							//compensate for arrow insertion
							//not for mac/ie5 or when menu arrows are not in use
							if(!um.mie&&um.e[89]!='none')
							{

								//store length of child nodes collection
								um.kl=sc.menu.childNodes.length;

								//for each child node
								i=0;
								do
								{
									//store reference to node
									um.tn=sc.menu.childNodes.item(i);

									//get node name (converted for O7 in XHTML mode)
									um.nn=um.vn(um.tn.nodeName).toLowerCase();

									//if this is a list item
									if(um.nn=='li')
									{
										//get arrow object
										um.ar=um.n.ga(um.gc(um.tn));

										//if arrow exists
										if(um.ar!=null)
										{
											//apply arrow padding
											um.n.wp(um.ar,um.tn,um.e[70],0,0);
										}
									}
									i++;
								}
								while(i<um.kl);
							}

							//position it
							sc.menu.style.left = sc.sc[j][2];
							sc.menu.style.top = sc.sc[j][3];
							sc.menu.style.marginLeft = sc.sc[j][4];
							sc.menu.style.marginTop = sc.sc[j][5];

							//highlight its parent link
							//um.n.lr(um.gc(sc.menu.parentNode),1);

							//create shadow layer
							um.sh=null;
							if(!um.ns&&um.e[58]!='none')
							{
								um.n.hl(sc.menu);
								//show shadow
								um.xd(um.sh);
							}

							//create iframe layer to cover windowed objects for win/ie5.5+
							//if that functionality is in use
							if(um.wie55&&(um.e[13]=='default'||um.e[13]=='iframe'))
							{
								um.n.il(sc.menu);
							}

							//if select-element hiding is being used
							if(um.hz)
							{
								//hide selects
								um.n.ts('hidden');
							}

							//show menu
							um.xv(sc.menu);
						}
					}
				}

				//break this loop
				break;
			}
		}
	}
};

