/*
Improved by Vuong Quang Khai
For
- keep the right case of the original words
- match full words only
* support the search syntax: "xxx" and "yyy zzz"
*/


/*
Text marker code 
Ashok Hariharan
*/


/*helper function , just does search & replace */

function replaceMe(str, phrase, chg) {
var pattern = new RegExp (phrase ,'ig');
return str.replace( pattern, chg);
}

/*helper function , just does search & replace */

function wrapMe(str, phrase, chg1, chg2) {
var pattern = new RegExp (phrase,'ig');
return str.replace( pattern, '$2' + chg1 + '$3' + chg2 + '$4');
}


/*helper function , gets innerHtml of a layer */
/*code ripped from ppk : http://xs4all.nsl/~ppk/js/ */
function getLayerHtml(id)
{
	var inHtml = new String('');
	if (document.getElementById)
	{
		x = document.getElementById(id);
		if ((x != null) && (x != 'undefined'))
			inHtml = x.innerHTML;
	}
	else
	 if (document.all)
	{
		x = document.all[id];
		if ((x != null) && (x != 'undefined'))
			inHtml = x.innerHTML;
	}
	return inHtml;
}

/*helper function , sets the innerHtml of a layer */
/*code ripped from ppk : http://xs4all.nsl/~ppk/js/ */
function setLayerHtml(text,id)
{
	if (document.getElementById)
	{
		elemObj = document.getElementById(id);
		elemObj.innerHTML = text;
	}
	else if (document.all)
	{
		elemObj = document.all[id];
		elemObj.innerHTML = text;
	}
	/*
	this is ns4 compatible code ...
	*/
	/*
	else if (document.layers)
	{
		x = document.layers[id];
		text2 = '<P CLASS="testclass">' + text + '</P>;';
		x.document.open();
		x.document.write(text2);
		x.document.close();
	}
	*/
}

/*main marker function*/
function markText(txtKeyword, inputHtml) 
{
           var re; 						/*regex object*/
           var varMatches; 					/*matches array*/
           var outHtml; 					/*output html*/
           var replaceText;
           replaceText = '<span style="background-color:yellow;color:red;font-weight:bold;">'+txtKeyword+ '</span>';
           var replaceText1 = '<span style="background-color:yellow;color:red;font-weight:bold;">';
		   var replaceText2 = '</span>';
	   re=new RegExp("(\<[^>][^<]*\>)([^<]*)","g");	/*create non-greedy regex match*/	   

	   /*
	   note : 
	   in 99% of cases <[^>]*\>)([^<]*) 
	   should also work , i did have the rare case where some invisible 
	   characters caused this to crap out ....
	   */

	   outHtml=new String('');				/*init html string*/
	   while ((varMatches = re.exec(inputHtml)) != null)		/*exec sequentially to apply span tags*/
	   {
		 outHtml+=varMatches[1]; 	/*html tag part*/
		 outHtml+=wrapMe(varMatches[2], txtKeyword, replaceText1, replaceText2); 
	   }
	   return outHtml;
}


function main()
{
	var DivContent = 'divContent';
	var Highlight = 'SearchQuery';
	var NonWordChars = '[^A-Za-z0-9\u00c0-\u01b0\u1ea0-\u1ef9]+';
	var Separator = 'KAMIROUTER';
	/*get the href of the current page*/
	var ref = document.location.href;
	if (ref.indexOf(Highlight + '=') == -1)
	{
		ref = document.referrer;
	}
	/*this stores the name of the query string that contains the keyword to be higlighted*/
	/*array for temp. use*/
	var arr;
	/*stores the keywords that are to be highlighted*/
	var highlightText;
	

	/*split into array using & as a separator*/
	arr = ref.split('&');
	for (var i=0 ; i < arr.length; i++)
	{
		var idx = arr[i].indexOf(Highlight + '=');	
		if (idx != -1)
		{
			/*unescape the string to be on the safe side*/
			
			/*Hieuvm: hot fix*/
			try{
				highlightText =  decodeURIComponent(arr[i].substring(idx+Highlight.length+1));
			}
			catch(e) 
			{
				highlightText = "";
			}

			//Trim leading and trailing spaces
			highlightText = replaceMe(highlightText, '(^' + NonWordChars + '|' + NonWordChars + '$)', '');
			//Hide and/or separators
			highlightText = replaceMe(highlightText, NonWordChars + '(AND|OR)' + NonWordChars, Separator);
			//Replace the rest of all non words operators by the notations
			highlightText = replaceMe(highlightText, NonWordChars, NonWordChars);
			//Use separators
			highlightText = '((^|' + NonWordChars + ')(' + replaceMe(highlightText, Separator, '|') + ')(' + NonWordChars  + '|$))';

		
			if (highlightText.length != 0)
			{
				/*we have successfully parsed out the url now pass it to the highlight func.*/
				var inHtml;
				var outHtml;
				/*get the innerHtml content from within the div*/
				inHtml = getLayerHtml(DivContent);
				if (inHtml == '') //Failed to get content from DivContent
				{
					return;
				}
				inHtml = '<em></em>'+ inHtml;
				/*run the highlight function to highlight stuff*/
				outHtml = markText(highlightText, inHtml);
				/*write the highlightd stuff back to the div*/
				setLayerHtml(outHtml, DivContent);
			}
			return;
		}
	}
}

if (typeof encodeURIComponent == "function")
	main();
