
function utf8UrlToAscii(str) {
	/*
	Á	%C1	%C3%81
	É	%C9	%C3%89
	Í	%CD	%C3%8D
	Õ	%D5	%C5%90
	Ó	%D3	%C3%93
	Ö	%D6	%C3%96
	Ú	%DA	%C3%9A
	Ü	%DC	%C3%9C
	Û	%DB	%C5%B0
	á	%E1	%C3%A1
	é	%E9	%C3%A9
	í	%ED	%C3%AD
	ó	%F3	%C3%B3
	ö	%F6	%C3%B6
	õ	%F5	%C5%91
	ú	%FA	%C3%BA
	û	%FB	%C5%B1
	ü	%FC	%C3%BC
	*/

	str = str.replace(/%C3%81/g, "%C1");
	str = str.replace(/%C3%89/g, "%C9");
	str = str.replace(/%C3%8D/g, "%CD");
	str = str.replace(/%C5%90/g, "%D5");
	str = str.replace(/%C3%93/g, "%D3");
	str = str.replace(/%C3%96/g, "%D6");
	str = str.replace(/%C3%9A/g, "%DA");
	str = str.replace(/%C3%9C/g, "%DC");
	str = str.replace(/%C5%B0/g, "%DB");
	str = str.replace(/%C3%A1/g, "%E1");
	str = str.replace(/%C3%A9/g, "%E9");
	str = str.replace(/%C3%AD/g, "%ED");
	str = str.replace(/%C3%B3/g, "%F3");
	str = str.replace(/%C3%B6/g, "%F6");
	str = str.replace(/%C5%91/g, "%F5");
	str = str.replace(/%C3%BA/g, "%FA");
	str = str.replace(/%C5%B1/g, "%FB");
	str = str.replace(/%C3%BC/g, "%FC");
	
	return str;
}

function getSearchString() {
	var words = new Array();
	var searchExp = '';
	var match = false;
	
	/*
	searchExp = document.referrer.match(/^http:\/\/[^?]*?google\.[^?]+(\?(.*&)?q=([^&#]*?)(&.*)?)?(#.*)?$/);
	if (searchExp) {
		words = unescape(utf8UrlToAscii(searchExp[3])).split("+");
		match = true;
	}
	*/

	if (!match) {
		searchExp = document.location.href.match(/^http:\/\/[^?]*?makettinfo\.[^?]+(\?(.*&)?q=([^&#]*?)(&.*)?)(#.*)?$/);
		if (searchExp != null) {
			words = unescape(utf8UrlToAscii(searchExp[3])).split(/[+\s]/);
			//getFields = document.location.search.split("&");
			match = true;
		}
	}
	if (!match) {
		searchExp = document.referrer.match(/^http:\/\/[^?]*?google\.[^?]+(\?(.*&)?q=([^&#]*?)(&.*)?)(#.*)?$/);
		if (searchExp != null) {
			words = unescape(utf8UrlToAscii(searchExp[3])).split(/[+\s]/);
			match = true;
		}
	}
	if (!match) {
		searchExp = document.referrer.match(/^http:\/\/[^?]*?yahoo\.[^?]+(\?(.*&)?p=([^&#]*?)(&.*)?)(#.*)?$/);
		if (searchExp != null) {
			words = unescape(utf8UrlToAscii(searchExp[3])).split(/[+\s]/);
			match = true;
		}
	}
	if (!match) {
		searchExp = document.referrer.match(/^http:\/\/[^?]*?altavista\.[^?]+(\?(.*&)?q=([^&#]*?)(&.*)?)(#.*)?$/);
		if (searchExp != null) {
			words = unescape(utf8UrlToAscii(searchExp[3])).split(/[+\s]/);
			match = true;
		}
	}

	/*
	if (words.length > 0) {
		markDFS(document.body, words);
	}
	*/
	if (match) {
		var searchPattern = "";
		for (var i=0; i<words.length; i++) {
			//alert(words[i]);
			if (words[i] != "") {
				if (searchPattern != "") {
					searchPattern += "|";
				}
				searchPattern += words[i];
			}
		}
		if (searchPattern != "") {
			searchPattern = "("+searchPattern+")";
			markDFS(document.body, searchPattern);
		}
	}
	return true;
}

function markDFS(node, str) {
	if (node.nodeType == 3) node = markText(node, str);
	if (node.firstChild != null) markDFS(node.firstChild, str);
	if (node.nextSibling != null) markDFS(node.nextSibling, str);
	return true;
}

function getDocument(node) {
	if (node.parentNode) return getDocument(node.parentNode);
	else return node;
}

function markText(node, str) {
	var i = 0;
	var searchObj = new RegExp(str, "im");
	searchObj.multiline = true;
	var matchObj;
	var match = "";
	var lastIndex = 0;
	var words = str.replace(/\(|\)/g, "").toLowerCase().split("|");
	var bgColor = "";
	
	//if (node.nodeValue.match("aaabbbaaabbb")) alert(node.nodeValue);
	
	while (0 < node.nodeValue.length) {
		//i = node.nodeValue.toLowerCase().indexOf(str.toLowerCase(), i);
		//i = node.nodeValue.search(new RegExp(str, "ims"));
		matchObj = searchObj.exec(node.nodeValue);
		//alert(node.nodeValue);
		if (matchObj != null) {
			i = matchObj.index;
			match = matchObj[0];
			if (words.length>0 && words[0] == match.toLowerCase()) bgColor = "yellow";
			else if (words.length>1 && words[1] == match.toLowerCase()) bgColor = "#aaffaa";
			else if (words.length>2 && words[2] == match.toLowerCase()) bgColor = "red";
			else if (words.length>3 && words[3] == match.toLowerCase()) bgColor = "#ddddff";
			else bgColor = "#aaaaff";
			
			var doc = getDocument(node);
			doc.location.hash = 'found_0_1';
			var nN1 = node.splitText(i);
			var nN2 = nN1.splitText(match.length);
			var newSpan = doc.createElement('span');
			newSpan.style.backgroundColor = bgColor;
	
			//  node newSpan->nN1 nN2
			newSpan.appendChild(nN1);
			node.parentNode.insertBefore(nN2, node);
			try {
				node.parentNode.insertBefore(newSpan, nN2);
				node.parentNode.insertBefore(node, newSpan);
			} catch (e) {
				node.parentNode.insertBefore(newSpan, node);
				node.parentNode.insertBefore(node, newSpan);
			}

			newSpan.setAttribute('id', 'found_'+foundCount);
			//newSpan.setAttribute('class', 'found');
			newSpan.className = 'found';
			if (foundCount == 0) {
				//alert(doc.location.hash);
				//doc.location.hash = 'found_0_1';
				//setTimeout("doc.location.hash = 'found_'+foundCount;", 10);
				doc.location.hash = 'found_'+foundCount;
				
			}
			foundCount++;
			
			node = nN2;
		} else {
			break;
		}
	}
	//alert("*"+node.nodeValue + "*\n matchObj: "+ searchObj.exec(node.nodeValue));
	return node;
}

var active = '';
var foundCount = 0;
//window.onload = function() {getSearchString();};
