(function($) {
	$(document).ready(function() {
		var loadedHash = null;
		var origContent = "";
		
		$('a.eview-link').livequery (function() {
			$(this).click(function () {
				if ($.history) {
					var url = $(this).attr('href');
					if (url.indexOf('#') >= 0) {
						// If we have another hash in the url cut it out
						url = url.split('#').shift();
					}
					if (window.location.hash != url) {
						// Only set hash when it's changed
						loadedHash = url;
						window.location.hash = url;
						if (!origContent) {
							// save original content, needed for /#hash back to /
							origContent = $(this).parents('div.eview-link-target:first');
						}
					}
				}
				return $(this).eviewLoad();
			});
		});

		$('form.eview-form').livequery('submit', function() {
			return $(this).eviewLoad();
		});
		
		if ($.history) {
		    $.history.init(function(hash) {
		    	// We only want to load content once and the other method $('a.eview-link').click() 
		    	// is much more effecient than this one.
		    	if (hash && hash != loadedHash && (match = hash.match("/:([^/]+)/"))) {
		    		// Find the first link object with the eview-link class matching the same block as the hash.
		    		// Then force the href to the hash value and load it through eviewLoad.
		    		match = match.shift();
		    		if (!origContent) {
		    			// save original content, needed for /#hash back to /
		    			origContent = $('a[href*="' + match + '"].eview-link:first').parents('div.eview-link-target:first');
		    		}
		    		$('a[href*="' + match + '"].eview-link:first').attr('href', hash).eviewLoad();
		    	} else if(!hash && origContent && loadedHash && (match = loadedHash.match("/:([^/]+)/"))){
		    		var ieBackAnchor = "IE-backhash";
		    		// Add a back anchor on top of the block for IE 6/7
	    			$('<a name="' + ieBackAnchor + '" />').prependTo(origContent);
		    		$('a[href*="' + match.shift() + '"].eview-link:first').parents('div.eview-link-target:first').replaceWith(origContent);
		    		if($.browser.msie && ($.browser.version < 8 || document.documentMode < 8)) {
		    			// IE 6/7 does not remove hash tag so it goes all the way back up, we don't want that
		    			window.location.hash = ieBackAnchor;
		    		}
		    	}
		    	loadedHash = hash;
	        });
		}
	});
})(jQuery);
