

var searchHighLight=
{
	searchString:'',
	init : function(){
		if(searchHighLight.checkHighLight()==true){
			this.attachObjEvent(window, 'load', searchHighLight.highLight, false);
		}
	},
	
	attachObjEvent : function (target, event, func , p){
		if(target.addEventListener){
			target.addEventListener(event, func, p);
			return true;
		}else if(target.attachEvent){
			target.attachEvent('on'+event, func , p);
			return true;
		}
		return false;
	},

	checkHighLight : function(){
		var url			= location.href; 
		var path		= url.split('/');
		var args		= path.slice((path.length-2),(path.length-1)).toString();

		if(args.indexOf('/')==-1){
			var search = args.split('-');
			if(search){
				for(var i=0;i<search.length;i++){
					if(search[i]=='q'){
						searchHighLight.searchString=search[i+1];
						return true;
					}
				}
			}
		}
		return false;
	},
	
	highLight : function(){
		searchHighLight.searchString=searchHighLight._utf8_decode(unescape(searchHighLight.searchString));
		searchArray = searchHighLight.searchString.split(" ");

		if (!document.body || typeof(document.body.innerHTML) == "undefined"){
			return false;
		}

		var bodyText = document.body.innerHTML;
		for (var i = 0; i < searchArray.length; i++) {
			bodyText = searchHighLight.doHighlight(bodyText, searchArray[i]);
		}

		document.body.innerHTML = bodyText;
		return true;
	},
	
	doHighlight : function(bodyText, searchTerm){
	  // the highlightStartTag and highlightEndTag parameters are optional

		highlightStartTag = "<font style='color:blue; background-color:yellow;'>";
		highlightEndTag = "</font>";

	  
	  // find all occurences of the search term in the given text,
	  // and add some "highlight" tags to them (we're not using a
	  // regular expression search, because we want to filter out
	  // matches that occur within HTML tags and script blocks, so
	  // we have to do a little extra validation)
	  var newText = "";
	  var i = -1;
	  var lcSearchTerm = searchTerm.toLowerCase();
	  var lcBodyText = bodyText.toLowerCase();
		
	  while (bodyText.length > 0) {
		i = lcBodyText.indexOf(lcSearchTerm, i+1);
		if (i < 0) {
		  newText += bodyText;
		  bodyText = "";
		} else {
		  // skip anything inside an HTML tag
		  if (bodyText.lastIndexOf(">", i) >= bodyText.lastIndexOf("<", i)) {
			// skip anything inside a <script> block
			if (lcBodyText.lastIndexOf("/script>", i) >= lcBodyText.lastIndexOf("<script", i)) {
			  newText += bodyText.substring(0, i) + highlightStartTag + bodyText.substr(i, searchTerm.length) + highlightEndTag;
			  bodyText = bodyText.substr(i + searchTerm.length);
			  lcBodyText = bodyText.toLowerCase();
			  i = -1;
			}
		  }
		}
	  }
	  
	  return newText;
	},



	// private method for UTF-8 encoding
	_utf8_encode : function (string) {
		string = string.replace(/\r\n/g,"\n");
		var utftext = "";
 
		for (var n = 0; n < string.length; n++) {
 			var c = string.charCodeAt(n);
 			if (c < 128) {
				utftext += String.fromCharCode(c);
			}else if((c > 127) && (c < 2048)) {
				utftext += String.fromCharCode((c >> 6) | 192);
				utftext += String.fromCharCode((c & 63) | 128);
			}else {
				utftext += String.fromCharCode((c >> 12) | 224);
				utftext += String.fromCharCode(((c >> 6) & 63) | 128);
				utftext += String.fromCharCode((c & 63) | 128);
			}
 		}
 
		return utftext;
	},

	// private method for UTF-8 decoding
	_utf8_decode : function (utftext) {
		var string = "";
		var i = 0;
		var c = c1 = c2 = 0;
 
		while ( i < utftext.length ) {
 			c = utftext.charCodeAt(i);
 			if (c < 128) {
				string += String.fromCharCode(c);
				i++;
			}else if((c > 191) && (c < 224)) {
				c2 = utftext.charCodeAt(i+1);
				string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
				i += 2;
			}else {
				c2 = utftext.charCodeAt(i+1);
				c3 = utftext.charCodeAt(i+2);
				string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
				i += 3;
			}
 		}
 		return string;
	}
}

searchHighLight.init();




