//
// highlight.js
// Google Highlighter
//
// Copyright(C)2001 - 2003
// Cal Henderson <cal@iamcal.com>
//
// Thanks to Ian Beveridge for bugfixes
//
// This code may be freely redistributed,
// providing this message remains intact.
//

//Script featured on & available at Dynamic Drive (http://www.dynamicdrive.com)
//Changes: Modification to window.onload code

// 15/10/06 - Changed Google to Omgili to hightlight results from Omgili and fixed some bugs ;)

var omgili_text_color = '#000000';

var omgili_link_colors = new Array('#fd9932','#fece9d','#feb061','#fec992','#ffdbb6');

function init_omgili(){



	var pattern = /omgili\./i;
	if (pattern.exec(document.referrer) != null){

		var url_parts = document.referrer.split('?');
		if (url_parts[1]){ 
			var url_args = url_parts[1].split('&');
			for(var i=0; i<url_args.length; i++){
				var keyval = url_args[i].split('=');
				if (keyval[0] == 'q'){
					f.QueryID.value = decode_url(keyval[1]);
					go_omgili(decode_url(keyval[1]));
					return;
				}
			}
		}
	}else
	{
		var pattern = /google\./i;
		if (pattern.exec(document.referrer) != null){
			var url_parts = document.referrer.split('?');
			if (url_parts[1]){ 
				var url_args = url_parts[1].split('&');
				for(var i=0; i<url_args.length; i++){
					var keyval = url_args[i].split('=');
					if (keyval[0] == 'q'){
						f.QueryID.value = decode_url(keyval[1]);
						go_omgili(decode_url(keyval[1]));
						return;
					}
				}
			}
		}
	}
}

function decode_url(url){
	
	return decodeURI(url.replace(/\+/g,' '));
	
}

function go_omgili(terms){

	terms = terms.replace(/\"/g,"");
	var terms_split = terms.split(' ');
	var c = 0;
	
	for(var i=0; i<terms_split.length; i++){
		highlight_goolge(terms_split[i], document.body,omgili_link_colors[c]);
		c = (c == omgili_link_colors.length-1)?0:c+1;

	}
}
function isLetter(char)
{
 if(char>64 && char<90)
 	return true;
 if(char>96 && char<123)
 	return true

 return false;
}
function highlight_goolge(term, container, color){
	var term_low = term.toLowerCase();
	if(term_low.length<3)
		return;

	for(var i=0; i<container.childNodes.length; i++){
		var node = container.childNodes[i];
		
		if (node.nodeType == 3 && node.parentNode.parentNode != null  && node.parentNode.parentNode.nodeName != "A"){
			var data = node.data;
			var data_low = data.toLowerCase();
			termIndex = data_low.indexOf(term_low)
			if (termIndex != -1){
				//term found!
				
				if(termIndex>0)
				{
				 	if(isLetter(data_low.charCodeAt(termIndex-1)))
						continue;
				}
				if(termIndex<data_low.length-1)
				{
				 	tmpTermIndex = termIndex + term_low.length;
				 	if(isLetter(data_low.charCodeAt(tmpTermIndex)))
						continue; 
				}
				
				var new_node = document.createElement('SPAN');
				node.parentNode.replaceChild(new_node,node);
				var result;
				var lnTmpCount = 0;
				
				if(term_low.length)
				{
					while((result = data_low.indexOf(term_low)) != -1){
						new_node.appendChild(document.createTextNode(data.substr(0,result)));
						new_node.appendChild(create_node_omgili(document.createTextNode(data.substr(result,term.length)),color));
						data = data.substr(result + term.length);
						data_low = data_low.substr(result + term.length);
							
					}
				}
				new_node.appendChild(document.createTextNode(data));
			}
		}else{
			//recurse
			highlight_goolge(term, node, color);
		}
	}
}

function create_node_omgili(child, color){
	var node = document.createElement('SPAN');
	node.style.backgroundColor = color;
	node.style.color = omgili_text_color;
	node.appendChild(child);
	return node;
}

if (window.addEventListener)
window.addEventListener("load", init_omgili, false)
else if (window.attachEvent)
window.attachEvent("onload", init_omgili)
else if (document.getElementById)
window.onload=init_omgili
