// UDMv4.5 // Load XML extension v1.0 //
/***************************************************************\

  ULTIMATE DROP DOWN MENU Version 4.5 by Brothercake
  http://www.udm4.com/
  
\***************************************************************/

/***************************************************************\
 * Set path to menu data document
\***************************************************************/

um.xdata = 'menu/udm-menudata.xml';

/***************************************************************\
\***************************************************************/



//add receiver for pre-initialisation event
um.addReceiver(function()
{
	//request object
	var request = null;
	
	//try to establish a request 
	if(typeof window.ActiveXObject != 'undefined') 
	{
		try { request = new ActiveXObject('Microsoft.XMLHTTP'); }
		catch(err) { request = null; }
	}
	else if(typeof window.XMLHttpRequest != 'undefined') 
	{
		request = new XMLHttpRequest();
	}
	
	//if we're good
	if(request != null)
	{
		//bind readyState change handler 
		request.onreadystatechange = function()
		{
			//if document has loaded and the status is okay or not-modified
			//(or there is no status number, which is opera viewing a page locally)
			if(request.readyState == 4 && /^(0|200|304)$/.test(request.status)) 
			{
				//look for <item> nodes
				var items = request.responseXML.getElementsByTagName('item');
				
				//for each node, if any
				var len = items.length;
				for(var i=0; i<len; i++)
				{
					//look for a menu trigger with the applicable IDREF
					var trigger = document.getElementById(items[i].getAttribute('idref'));
					
					//if it exists
					if(trigger)
					{
						//for IE 
						if(um.ie)
						{
							//copy the serialised menu XML into the the item node
							//because importNode isn't supported 
							//and cloneNode doesn't (and shouldn't) work 
							//this is the only way I could make it happen!
							trigger.innerHTML += um.gm(items[i]).xml;
						}
						//for others 
						else
						{
							//import the UL menu inside the the item node, and append it to the trigger
							trigger.appendChild(document.importNode(um.gm(items[i]), true));
							
							//if this is safari
							if(um.s)
							{
								//parse the trigger HTML and rewrite to trigger
								//to fix the DOM that comes back in responseXML
								//http://www.brothercake.com/Ref/mangleNode.html
								//it's a brutal hack, I know ... but I'm open to suggestions ;)
								trigger.innerHTML = trigger.innerHTML.replace(/(<^(img|br|hr)[^\/]*)\/>/ig, '$1>');
							}
							
						}
					}
				}
				
				//refresh the tree without refiring API events
				um.refresh(false);
			}
		};

		//if overrideMimeType is supported, use it to set the mime-type
		//this is for safety, in case the document is not being served as "text/xml"
		//because if that's the case, moz won't have a response DOM 
		if(typeof request.overrideMimeType != 'undefined')
		{
			request.overrideMimeType('text/xml');
		}
		
		//bind a random number query string to the URI
		//which evens out caching behavior cross-browser
		um.xdata += '?rand=' + (Math.random() * 1000) / 1000;
		
		//make the request
		request.open('GET', um.xdata, true);
		request.send(null);
	}

},'000');



