// Copyright (c) 2004 www.BanBots.com
function parseReferrer() {
	if (window.location.href.match(/highlight=/)) {
		var referrer = unescape(window.location.href.toLowerCase());
		var referrer = referrer.replace(/\#/g, "&");
	} else {
		var referrer = unescape(document.referrer.toLowerCase());
	}
	if (!referrer) {
		return false;
	} else if (referrer.match(/&googlesearch=/)) {
		var refMatch = "googlesearch=";
	} else if (referrer.match(/[&|\?]query=/)) {
		var refMatch = "query=";
	} else if (referrer.match(/[&|\?]search=/)) {
		var refMatch = "search=";
	} else if (referrer.match(/^http:\/\/search\.yahoo\.com\/.*[&|\?]p=/)) {
		var refMatch = "p=";
	} else if (referrer.match(/^http:\/.*\/cgi-bin\/search\/search\.cgi.*terms=/)) {
		var refMatch = "terms=";
	} else if (referrer.match(/^http:\/\/search\.about\.com\/fullsearch.*[&|\?]terms=/)) {
		var refMatch = "terms=";
	} else if (referrer.match(/^http:\/.*\/vbulletin\/showthread.php\?.*&highlight=/)) {
		var refMatch = "highlight=";
	} else if (referrer.match(/^http:\/.*dogpile\.com.*\/search\/web\//)) {
		var referrer = referrer.replace(/\/web\/([^\/]+)\/(.*)/gi, "/web/$1");
		var refMatch = "/web/";
	} else if (referrer.match(/[&|\?]searchfor=/)) {
		var refMatch = "searchfor=";
	} else if (referrer.match(/[&|\?]q=/)) {
		var refMatch = "q=";
	} else if (referrer.match(/highlight=/)) {
		var refMatch = "highlight=";
	} else {
		return false;
	}
	var beginInsert = referrer.toLowerCase().indexOf(refMatch);
	if ((beginInsert < 0) || ((beginInsert + refMatch.length) == referrer.length)) {
		return false;
	}
	var endInsert = referrer.indexOf("&", beginInsert);
	if (endInsert < 0) {
		var endInsert = referrer.length;
	}
	if ((endInsert - beginInsert) <= refMatch.length) {
		return false;
	}
	var refString = referrer.substring((beginInsert + refMatch.length), endInsert);
	var refString = refString.replace(/([\+\.\(\)\{\}\[\]\|\\\/\*\@:,\"\!\s]+)/g, " ");
	var stopWords = /\b(\d+|\w|about|after|also|an|and|are|as|at|be|because|before|between|but|by|can|com|de|do|en|for|from|has|how|however|htm|html|if|i|in|into|is|it|la|no|of|on|or|other|out|since|site|such|than|that|the|there|these|this|those|to|under|upon|vs|was|what|when|where|whether|which|who|will|with|within|without|www|you|your)\b/gi;
	var refString = refString.replace(stopWords, " ");
	var refString = refString.replace(/[\s]+/g, " ");
	var refString = refString.replace(/^[\s]*(.*?)[\s]*$/g, "$1");
	if (refString.length < 1) {
		return false;
	}
	return compileText(refString, false);
}
function compileText(refText) {
	var remDuplicate = new Array();
	var storeTerms = refText.split(/[\s]+/);
	var hash = new Object();
	for (var x=0; x < storeTerms.length; x++) {
		if (hash[storeTerms[x].toLowerCase()] != 1) {
			remDuplicate = remDuplicate.concat(storeTerms[x]);
			hash[storeTerms[x].toLowerCase()] = 1;
		}
	}

	var storeTerms = remDuplicate;
	var refText = remDuplicate.join(" ");
	if (!document.body || typeof(document.body.innerHTML) == "undefined") {
		return false;
	}
	if (window.location.href.match(/highlight=/)) {
		var textBody = "<\!--ign--><div class=\"highlight\">The following keywords have been highlighted on this page: <\!--\/ign-->" + refText + "<\/div>" + document.body.innerHTML;
	} else {
		var textBody = "<\!--ign--><div class=\"highlight\">The following keywords have been highlighted in your search: <\!--\/ign-->" + refText + "&nbsp;&nbsp;&nbsp;[<a href=\""+ self.location + "\">remove highlighting<\/a>]<\/div>" + document.body.innerHTML;
	}
	for (var x = 0; x < storeTerms.length; x++) {
		if (x == 0) {
			var colorCode = "#00ff66";
		}
		else if (x == 1) {
			var colorCode = "#66ffff";
		}
		else if (x == 2) {
			var colorCode = "#ff9966";
		}
		else if (x == 3) {
			var colorCode = "#ffcccc";
		}
		else if (x == 4) {
			var colorCode = "#99cccc";
		}
		else {
			var colorCode = "#ccff66";
		}
		var textBody = textHighlight(textBody, storeTerms[x], colorCode);
	}
	document.body.innerHTML = textBody;
	return true;
}
function textHighlight(textBody, highlightedWord, colorCode) {
	var beginTag = "<font style='background-color:" + colorCode + ";'>";
	var endTag = "</font>";
	var rebuildText = "";
	var x = -1;
	var ihighlightedWord = highlightedWord.toLowerCase();
	var itextBody = textBody.toLowerCase();
	while (textBody.length > 0) {
		var x = itextBody.indexOf(ihighlightedWord, (x + 1));
		if (x < 0) {
			rebuildText += textBody;
			var textBody = "";
		} else {
			if (textBody.lastIndexOf(">", x) >= textBody.lastIndexOf("<", x)) {
				if (itextBody.lastIndexOf("<\!--\/", x) >= itextBody.lastIndexOf("<\!--", x)) {
					rebuildText += textBody.substring(0, x) + beginTag + textBody.substr(x, highlightedWord.length) + endTag;
					var textBody = textBody.substr(x + highlightedWord.length);
					var itextBody = textBody.toLowerCase();
					var x = -1;
				}
			}
		}
	}
	return rebuildText;
}