/*
	This script will find links with the class "backLink" and make them invoke history.back() on click,
	but only if the page is accessed from a link with class "forwardLink".
	It should be included near the end of a document, just before the closing </body> tag.
	
	Requires core.js
*/

(function() {

	// Get the proper link elements
	var displayBackLinks
	  , HASH = '&displayBacklinks=true'
	  , rHash = new RegExp(HASH);
	
	// Add &displayBacklinks=true to the forwardLink href hash
	initForwardLinks();
	displayBackLinks = parseHash();
	initBackLinks(displayBackLinks);
	
	
	function initForwardLinks() {
		var forwardLink = core.getElements('.forwardLink');
		
		core.each(forwardLink, function(a) {
			var tempHref = a.href;
			
			tempHref = tempHref.split('#');
			if (tempHref.length = 1) {
				tempHref.push(HASH);
			}
			else if (tempHref.length = 2) {
				tempHref[1] += HASH;
			}
			
			tempHref = tempHref.join('#');
			
			a.href = tempHref;
		});
	}
	
	function parseHash() {
		var display = rHash.test(window.location.hash);
		return display;
	}
	
	function initBackLinks(displayBackLinks) {
		var backLink = core.getElements('.backLink');
		
		core.each(backLink, function(a) {
			if (!displayBackLinks) {
				a.style.display = 'none';
			}
			
			a.onclick = function() {
				window.history.back();
				return false;
			}
		});

	}
	
})();
