/*
	Calling this function:
	
	extendNavigation({nav: 'topnav',					<-- Which element (by prototype selectors) to process links inside of
					 applyToTag: 'li',					<-- What tag to apply the classes to
					 addFirst: true, 					<-- Add a 'first' class to the first link
					 addLast: true, 					<-- Add a 'last' class to the last link
					 includePages: true, 				<-- Add clases to links that point to the current page
					 includeParents: true,				<-- Add classes to links that point to parents of the current page
					 includeHomePage: true, 			<-- Add classes to links that point to the home page of the site if they match one of the 2 above conditions
					 pageClass: 'selected', 			<-- The name of the class to add to current pages
					 parentClass: 'selectedparent' 		<-- The name of the class to add to parent pages
					 });
*/


/* 
	Adds 'first' & 'last' classes to appropriate links inside any ID'd element.
	Finds current pages and parent pages, applying class names you specify for applying selected states
*/
function extendNavigation(args)
{
	
	// If the container doesn't exist, or doesn't contain any tags specified, quit
	if (args.nav == null) return;
	if (args.nav.select(args.applyToTag)[0] == undefined) return; 
	
	// If [addFirst] is true, applies [class='first'] to first element it finds
	if (args.addFirst)
		args.nav.select(args.applyToTag)[0].addClassName('first');
	
	// If [addLast] is true, applies [class='last'] to last link it finds
	if (args.addLast)
	{
		var a = args.nav.select(args.applyToTag);
		a[a.size() - 1].addClassName('last');
	}
	
	args.nav.select(args.applyToTag).each(function(a){
					
		var linkRef;
		// If [applyToTag] is an anchor, use it's HREF attribute to compare links
		if (args.applyToTag == 'a') linkRef = a;
		// Else use [applyToTag]'s first child link's HREF attribute to compare links
		else linkRef = a.select('a')[0];		
		// If the element contains no links, skip it
		if (linkRef == undefined) return;
		if (linkRef.rel.indexOf('noindicator') != -1) return;
		
		// If [includePages] is true, and the link points to the current page,
		// Apply the specified class name
		if (args.includePages && isCurrentPage(linkRef))
			a.addClassName(args.pageClass);

		// Else, If [includeParents] is true and the link points to a parent of the current page,
		// Apply the specified class name
		else if (args.includeParents && isParentPage(linkRef) && !isCurrentPage(linkRef))
			if (args.includeHomePage || getSiteRootUrl(linkRef.href) != filterUrlString(linkRef.href))
				a.addClassName(args.parentClass);
	
	});

}


function initExternalLinks()
{
	
	$$('a[rel*=external]').each(function(s) {
		s.onclick = function()
			{
				window.open(s.href);
				return false;
			}
		
	});

}


/* 
	Helps ensure consistency when comparing two URLs
	by formatting and stripping uneeded parts of the URL String
*/


function filterUrlString(url)
{
	
	/* 
		From Left to Right:
		- Strips any leading or trailing whitespace
		- Converts all character to lowercase
		- Strips any anchor links or '#' links
		- Stips any query strings
		- Strips any references to the index page
	*/
	
	return url.strip().toLowerCase().split('#')[0].split('?')[0].split('index.')[0];

}


/* 
	Finds the actual site root of a passed URL string
	used for comparing a link to the site's home page
*/
function getSiteRootUrl(url)
{
	
	/* 
		From Left to Right:
		- Re-adds 'http://' when returning the new string
		- Removes 'http://' initially to prevent interference when stripping subfolders
		- Strips subfolders
		- Re-adds trailing '/' When returning the string
	*/
	return 'http://' + filterUrlString(url).split('http://')[1].split('/')[0] + '/';	
	
}


/*
	Checks if the current link points to the current page
*/
function isCurrentPage(linkRef)
{

	return filterUrlString(linkRef.href) == filterUrlString(document.URL) && linkRef.readAttribute('href') != '#';

}


/*
	Checks if the current link points to a parent of the current page
*/
function isParentPage(linkRef)
{

	return filterUrlString(document.URL).lastIndexOf(filterUrlString(linkRef.href)) != -1 && linkRef.readAttribute('href') != '#';

}
