// 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.
//
// By Rob Watts, http://tractionsys.com
// Version 1.1, March 30, 2011
// Provided under the MIT license. Feel free to use it however you like.
//
// Example:
// $("ul.nav a").currentPage();
//
// To override defaults, pass in a defaults object:
// $("ul.nav a").currentPage({
//    attr: "href",
//    defaultClass: "selected",
//    indexPage: ""
// });

function getPage() {
  var href = window.location.href;
  var host = window.location.host;
  var pos = href.indexOf(host);
  var url = href.substring(pos + host.length);


  if ( url.charAt(0) == "/" )
    url = url.substring(1, url.length);

  return url;
}


jQuery.fn.currentPage = function(options) {
  var currentPage = "";
  var thisClass = "";
  var thisHref = "";
  var settings = jQuery.extend({
    defaultClass: "current",       // Default class to add to current link
	attr: "href",              // Default attribute to compare with current page URL
	appendToFirstClass: false, // Append the defaultClass to the first class on the element.
	// E.g. class="monkey" becomes class="monkey monkeycurrent"
	indexPage: "/",            // Default index page (to make current when page is not shown in URL)
	anyPath: false             // Ignore the path (supports pages in subfolders, and triggers on filename alone
	}, options);


  currentPage = getPage();

  if (currentPage == "")
    currentPage = settings.indexPage;

  this.each(function() {
      thisHref = $(this).attr(settings.attr);

      if ( (thisHref == currentPage) || ( settings.anyPath && ( thisHref.indexOf(currentPage) > -1) ) ) {

	if (settings.appendToFirstClass) {
	  thisClass = $(this).attr("class").split(" ")[0];  // Choose the first class (if multiple classes defined)
	  thisClass = thisClass + settings.defaultClass;    // Append the default current class to the first class class
	}
	else
	  thisClass = settings.defaultClass;

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

