// 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*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/
	}
};

// jQuery Current Web;
//
(function($) {
	// What does the pluginName plugin do?
	$.fn.current_web = function(options) {
		var current_url = new parseUri(location.href);
		var current_path = current_url.directory;
		return this.each(function() {
			var $this = $(this);
			var this_url = parseUri(this.href);
			var this_path = this_url.directory;
			if(current_path == this_path){
				var current_menu = $this.children("img");
				var src = current_menu.attr("src");
				var ext = src.lastIndexOf('.');
			    var current_src = src.substr(0, ext) + '_on' + src.substr(ext, 4);
				var img = current_menu.get(0);
				current_menu.replaceWith($("<img />").attr({"src":current_src,"width":current_menu.width(),"height":img.height}));
				
			}
		});
	};
	$.fn.current_web_menu = function(options) {
		var opts = $.extend({},
		$.fn.current_web_menu.defaults, options);
		var url = new parseUri(location.href);
		var $this =$(this);
		var find_selector = "a[href='"+ url.path + "']";
		//console.log(find_selector);
		$this.find("dd, dd ul ul").hide();
		var anchor = $this.find(find_selector).addClass("current-link");
		
		anchor.parents("li:last").addClass("parent-link");
		anchor.next().show();
		var menu = anchor.parents("ul,dd").show();
		anchor.parents("dd").addClass("current-dd");
		anchor.parents("dt").next().show().addClass("current-dd");
		return this;

		// private function for debugging
		function debug($obj) {
			if (window.console && window.console.log) {
				window.console.log($obj);
			}
		}
	};
	
	$.fn.current_web_menu_static = function(options) {
		var opts = $.extend({},
		$.fn.current_web_menu.defaults, options);
	
		var $this =$(this);
		var find_selector = "a[href='"+opts.path + "']";
		//console.log(find_selector);
		$this.find("dd, dd ul ul").hide();
		var anchor = $this.find(find_selector).addClass("current-link");
		
		anchor.parents("li:last").addClass("parent-link");
		anchor.next().show();
		var menu = anchor.parents("ul,dd").show();
		anchor.parents("dd").addClass("current-dd");
		anchor.parents("dt").next().show().addClass("current-dd");
		return this;

		// private function for debugging
		function debug($obj) {
			if (window.console && window.console.log) {
				window.console.log($obj);
			}
		}
	};
	// default options
	$.fn.current_web.defaults = {
		default_file: 'index.html'
	};
	$.fn.current_web_menu.defaults = {
		default_file: 'index.html'
	};

})(jQuery);

