(function($) {
	$.fn.jgate = function(options) {
		var opts = $.extend({}, $.fn.jgate.defaults, options);
		return this.each(function() {
			var $this = $(this);
			// CHECK CURRENT LOCATION
			// NOTE: MIGHT NOT WORK WITH SINGLE FILE INFRASTRUCTURE
			// USE ignoreStrings OPTION TO TAKE OUT PROBLEM TEXT
			$.fn.jgate.file = getFile();
			if (opts.ignoreStrings !== false) {
				$.each(opts.ignoreStrings, function(i, val) {
					$.fn.jgate.file = $.fn.jgate.file.replace(eval('/' + val + '/g'),'');
				});
			}
			$this.find('a[href=' + $.fn.jgate.file + ']').addClass('highlight');
			
			// ADD HOLDING CLASS TO PARENT OF HIGHLIGHTED LINKS
			$this.find('a.highlight, span.highlight').each(function() {
				$(this).parent('li').parent('ul').addClass('hold');
			});
			
			// ADD HOLDING CLASS TO ENTIRE ANCESTRY OF HIGHLIGHTED LINKS
			while ($this.find('.hold').parent('li').parent('ul:not(.hold)').size()) {
				$this.find('.hold').parent('li').parent('ul:not(.hold)').addClass('hold');
			}
			// HIDE ALL SUBMENUS WHICH AREN'T ANCESTOR OR NEXT SIBLING OF HIGHLIGHTED LINK
			$this.find('ul').each(function(i) {
				if ($(this).prev('span.highlight,a.highlight').size() == 0 && !($(this).hasClass('hold'))) {
					$(this).hide();
				}
			});
			// ----------------------------------------------------------------------------
			// ANIMATE SUB-MENU LISTS
			// NOT TO BE RUN ON SUB-MENUS WHICH ARE ANCESTOR OR SIBLING OF HIGHLIGHTED LINK
			// ADD HIGHLIGHT CLASS TO CLICKED ANCHORS/SPANS
			$this.find('span, a').click(function() {
				if ($(this).next('ul:not(.hold)').length > 0) {
					$(this).next('ul:not(.hold)').each(function(i) {
						if ($(this).prev('span, a').hasClass('highlight')) {
							$(this).slideUp(opts.hideSpeed);
							$(this).prev('span, a').removeClass('highlight');
						} else {
							$(this).slideDown(opts.showSpeed);
							$(this).prev('span, a').addClass('highlight');
						}
					});
				}
			});
			// ADD HOVER CLASS TO HOVERED LINKS TO COUNTER IE6'S BUG OF HOVERING ONLY ON LINKED ANCHORS
			$this.find('span, a').hover(function() {
				$(this).addClass('hover');
			}, function() {
				$(this).removeClass('hover');
			});
			// ----------------------------------------------------------------------------
		});
	}
	
	$.fn.jgate.defaults = {
		showSpeed: 'normal',
		hideSpeed: 'normal',
		ignoreStrings: ['index.php']
	};
	// GET CURRENT SOURCE FILE
	// ORIGINAL AUTHOR UNKNOWN
	function getFile(str){
		if("undefined" == typeof(str))
			str=location.href;
		return (str.replace(/.+[\/]([^\/]+)$/,'$1'));
	}
})(jQuery);