/**
 * Check Current Page
 * jQuery Plugin
 * 
 * This function will test all links on the page or in the entire document, looking for
 * links that point to the current page.  If they are found, a class is applied to them
 * 
 * If a link points to a directory (ends with a /), then if it shows up on any page within
 * that directory it will have the class applied to it
 * 
 * Usage:
 * 
 * $.checkCurrentPage();
 * 		Will check all links in the entire document and apply the default class 'onCurrentPage'
 * 		to the links that happen to point to the page the user is currently viewing.
 * 
 * $('#elementId').checkCurrentPage('customClassName');
 * 		Will check all links within the element with id="elementId" and apply the class
 * 		'customClassName' to those links that point to the current page.
 * 
 * VERSION INFO
 * 
 * 1.0		30 June 2008		Briley Hooper
 * 			Initial version
 */
jQuery.fn.checkCurrentPage = function(className) {
	if (!className) className = 'onCurrentPage';
	var loc = window.location.href;
	
	return this.find('a').each(function() {
		var href = this.getAttribute('href');
		var srch;
		if (href.search(/\/$/) > -1) {
			// Link is pointing to a directory, highlight if location is within that directory
			var dir = href.split(/\/(?=[a-zA-Z_])/g).pop();
			srch = new RegExp('/'+dir);
		}
		else {
			// Link is pointing to a file, highlight if location is that file
			var file = href.split(/[?#]/)[0];
			if (loc.search(/\/$/) >= 0) loc += 'index.php';
			srch = new RegExp(file);
			
		}
		
		if (srch.test(loc)) {
			jQuery(this).addClass(className);
		}
	}).end();
}
