var reIndexes = /index\.(htm|html|php|py)$/i; // matches index.html, index.php, etc.
var reLastSlash = /\/$/; // matches the slash at the end of a URL
var reSubDomains = /^www./;

// resolveUrl by Sean Kelly via StackOverflow at
// http://stackoverflow.com/questions/2836174/generate-canonical-real-url-based-on-base-href-or-location
function resolveUrl(url) {
   if (!url) {
      throw new Error("url is undefined or empty");
   }

   var reParent = /[\-\w]+\/\.\.\//; // matches a foo/../ expression
   var reDoubleSlash = /([^:])\/\//g; // matches // anywhere but in the protocol

   // replace all // except the one in proto with /
   url = url.replace(reDoubleSlash, "$1/");

   var base = (document.getElementsByTagName('BASE')[0] &&
      document.getElementsByTagName('BASE')[0].href) || "";

   // If the url is a valid url we do nothing
   if (!url.match(/^(http||https):\/\//)) {
      // If this is a relative path
      var path = (url.substring(0, 1) === "/") ? base : location.pathname;

      if (path.substring(path.length - 1) !== "/") {
         path = path.substring(0, path.lastIndexOf("/") + 1);
      }

      if (!url.match(/^(http||https):\/\//)) {
         url = location.protocol + "//" + location.host + path + url;
      }
   }

   // reduce all 'xyz/../' to just ''
   while (reParent.test(url)) {
      url = url.replace(reParent, "");
   }

   return url;
}

// parseUri 1.2.2
// (c) Steven Levithan <stevenlevithan.com>
// MIT License
function parseUri (str) {
   var o   = parseUri.options,
       m   = o.parser[o.strictMode ? "strict" : "loose"].exec(str),
       uri = {},
       i   = 14;

   while (i--) uri[o.key[i]] = m[i] || "";

   uri[o.q.name] = {};

   uri[o.key[12]].replace(o.q.parser, function ($0, $1, $2) {
      if ($1) uri[o.q.name][$1] = $2;
   });

   return uri;
};

parseUri.options = {
   strictMode: false,
   key: ["source","protocol","authority","userInfo","user","password","host","port","relative","path","directory","file","query","anchor"],
   q:   {
      name:   "queryKey",
      parser: /(?:^|&)([^&=]*)=?([^&]*)/g
   },
   parser: {
      strict: /^(?:([^:\/?#]+):)?(?:\/\/((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:\/?#]*)(?::(\d*))?))?((((?:[^?#\/]*\/)*)([^?#]*))(?:\?([^#]*))?(?:#(.*))?)/,
      loose:  /^(?:(?![^:@]+:[^:@\/]*@)([^:\/?#.]+):)?(?:\/\/)?((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/
   }
};
// end parseUri

function cleanUri(uri) {
   uri = parseUri(uri);

   return uri.protocol + '://' + uri.host.replace(reSubDomains, '') +
      uri.path.replace(reIndexes, '').replace(reLastSlash, '');
}

$(function() {
			jQuery.extend(
						jQuery.expr[':'], {
									current_page: function(element, index, match) {
												var href = jQuery(element).attr('href');
			
												var resolved_uri = cleanUri(resolveUrl(href));
												var current_uri = cleanUri(location.href);
			
												if (match[3] == "starts-with") {
															if (current_uri.indexOf(resolved_uri) === 0) {
																		return true;		
															}
												}
			
												if (resolved_uri == current_uri) {
															return true;
												}
			
												return false;
									}
						}
			);
});
