// jQuery plugin for setting the class of a particular element to "current" based on the current page.
// Useful for navigation menus that are part of a server side include for the whole site.

function getPage(url, q) {
  var u = [];

  // Remove the query string if q is false.
  if (!q) {
    url = url.split('?')[0];
  }
  
  // Remove parent folders from path, leaving just current page file
  u = url.split('/');
  url = u[u.length - 1];

  return url;
};


jQuery.fn.currentPage = function(options) {
  var path = window.location.href;
  var currentPage = "";
  var thisClass = "";
  

  settings = jQuery.extend({
    defaultClass: "current",     // Default class to add to current link
	attr: "href",            // Default attribute to compare with current page URL
	useQuery: false,         // Whether or not to strip the query string from the URL
	indexPage: "index.shtml" // Default index page (to highlight when page is not shown in URL)
	}, options);

  currentPage = getPage(path, settings.useQuery);
  if (currentPage == "")
    currentPage = settings.indexPage;
  
  this.each(function() {
      if ($(this).attr(settings.attr) == currentPage) {
	thisClass = $(this).attr("class").split(" ")[0];  // Choose the first class (if multiple classes defined)

	thisClass = thisClass + settings.defaultClass;  // Append the current class to any existing class

	$(this).addClass(thisClass);
      }
    });
};

