// UDMv4.21 // You Are Here extension v1.01 //
/***************************************************************\

  ULTIMATE DROP DOWN MENU Version 4.2 by Brothercake
  http://www.udm4.com/
  
\***************************************************************/

/***************************************************************\
 * Set you are here parameters
\***************************************************************/

var youAreHere = [
	"default.htm",		// default page name [eg "index.php", "default.html" etc]
	"You are here: ",	// add text to here-page title ["xxx"|"none"]
	"You're in this branch: ", // add text to here-branch title ["xxx"|"none"]
	"before",		// where to add title text ["before"|"after"]
	];

/***************************************************************\
\***************************************************************/



//global object
var yah = new Object;



//add to title method
yah.addToTitle = function(titleNode,titleText)
{
	//link text value
	yah.iText = '';
	
	//get child nodes
	yah.nodes = titleNode.childNodes;
	yah.nodesLen = yah.nodes.length;
	
	//for each node
	for(i=0; i<yah.nodesLen; i++)
	{
		//if it's a text node
		if(yah.nodes[i].nodeType == 3)
		{
			//store its value and stop
			yah.iText = yah.nodes[i].nodeValue;
			break;
		}
	}
	
	//compile title attribute from existing title if it has one
	//or link text if it doesn't have a title
	yah.title = (titleNode.title == '') ? yah.iText : titleNode.title;
	
	//add title text
	yah.title = (youAreHere[3] == 'before') ? titleText + yah.title : yah.title + titleText;
	
	//write the value to title attribute
	titleNode.title = yah.title;
};



//sort array numerically by first member
function compareNumbers(a,b)
{
	return b[0]-a[0];
};




//add receiver for navbar initialised event
um.addReceiver(findHere,'010');




//navbar initialised receiver function
function findHere()
{
	//store document address
	yah.uri = top.document.location.href;
	
	//remove default page name
	yah.uri = yah.uri.replace(youAreHere[0],'');
		
	//create an array of possible matches
	yah.matches = [];
	
	//get array of navbar links
	yah.links = umTree.getElementsByTagName('a');
	yah.linksLen = yah.links.length;
	
	//for each link
	for(var i=0; i<yah.linksLen; i++)
	{
		//store href of this link
		yah.href = yah.links[i].href;
		
		//remove default page name
		yah.href = yah.href.replace(youAreHere[0],'');
		
		//if the address contains the href of this link
		if(yah.href!=''&&yah.uri.indexOf(yah.href)!=-1)
		{
			//store this link object
			yah.matches[yah.matches.length] = yah.links[i];
		}
	}
	
	//number of matches
	yah.matchesLen = yah.matches.length;
	
	//don't continue if there are no matches 
	if(yah.matchesLen < 1) { return false; }
	
	//create an array of match probabilities
	yah.probs = [];
	
	//for each match
	for(i=0; i<yah.matchesLen; i++)
	{
		//get link href characters
		yah.href = yah.matches[i].href;
		yah.hrefLen = yah.href.length;
		
		//initially zero probability
		//store object as well so we have it at the other end
		yah.probs[i] = [0,yah.href];
		
		//for each character
		for(var j=0; j<yah.hrefLen; j++)
		{
			//if this character is the same as 
			//the same character in the uri
			if(yah.href.charAt(j) == yah.uri.charAt(j))
			{
				//add one to probability
				yah.probs[i][0]++;
			}
		}
	}
	
	//sort the probabilites matrix numerically from item [0]
	yah.probs.sort(compareNumbers);

	//so .. the href we want is now at index [0][1]
	yah.href = yah.probs[0][1];
	
	//get array of links
	yah.links = umTree.getElementsByTagName('a');
	yah.linksLen = yah.links.length;
	
	//for each link
	for(i=0; i<yah.linksLen; i++)
	{
		//if the hrefs are the same
		if(yah.links[i].href == yah.href)
		{
			//if add to title is not "none"
			if(youAreHere[1] != 'none')
			{
				//add to title
				yah.addToTitle(yah.links[i],youAreHere[1]);
			}
			
			//apply here class
			applyHereClass(yah.links[i]);
		}
	}
	
	return true;
};


//apply here class to link and ancestors
function applyHereClass(linkObject)
{
	//increase z-order to bring each highlighted link above the previous
	//this allows for simulated border-collapse using negative top margin
	linkObject.style.zIndex = um.e[6]+=2;
	
	//O7 requires that we check for existing class name before setting it
	//maybe it doesn't allow class=" something"
	(linkObject.className=='') ? linkObject.className = 'udmY' : linkObject.className+= ' udmY';

	//if add to branch title is not "none"
	if(youAreHere[2] != 'none')
	{
		//if this link doesn't already have page-title text
		if(linkObject.title.indexOf(youAreHere[1])==-1)
		{
			//add to title
			yah.addToTitle(linkObject,youAreHere[2]);
		}
	}
	
	//if parent is not the main navbar
	if(um.gp(linkObject).parentNode.className != 'udm')
	{
		//set link object to ancestor
		yah.link = um.gc(um.gp(linkObject).parentNode.parentNode);
		
		//recur
		applyHereClass(yah.link);
	}
};

