$(document).ready(function() {
	$('#searchHint').hide();
	var standardSearchText =  '' + $('input[@name=search]').attr("value");
	$('input[@name=search]').focus(function() {
		if($(this).attr("value") == standardSearchText)
	  	$(this).attr("value", "");
	});
	$("input[@name=search]").blur( function () {
		if($(this).attr("value") == null || $(this).attr("value") == '')
			$(this).attr("value", standardSearchText);	 
	});
	$("input[@name=search]").keyup( function (e) {
		if($(this).attr("value") != null && $(this).attr("value").length > 2) {
			$('#searchHint').fadeIn(300);
			var queryString = $('#searchForm').formSerialize() + "&ajax=1"; 
		 	$.post($('#searchForm').attr("action"), queryString, function (data, textStatus) {$("#searchHint").html(data);});
		} else {
			$('#searchHint').hide();
		}
	});
	
	
	$("#outerservicesearch").hover(function() {


	},function(){
		$('#searchHint').hide();
	});
	var matches;
	if(document.referrer) {
		matches = document.referrer.match(/[?&]q=([^&]*)/);
	}
	if(!matches && (unescape(window.location) == $('#searchForm').attr("action") || unescape(document.referrer) == $('#searchForm').attr("action"))) {
		var queryString = $('#searchForm').formSerialize() + "&ajax=1&getkeywords=1"; 
		$.post($('#searchForm').attr("action"), queryString, function (data, textStatus) {highlightNormal(data, false);});
	 }
	else {
		highlightGoogle(matches);
	}
});

function highlightGoogle(matches) {
	if (!matches) return;
  var terms;
	terms = unescape(matches[1].replace(/\+/g, ' '));
  highlightNormal(terms);
}

function highlightNormal(matches) {
	if (!matches) return;
  var terms = matches.split(" ");
  var re = Array();
  
	var term = "";
  for(i=0; i<terms.length; i++) {
  	re.push(new RegExp().compile('(' + terms[i] + ')', 'i'));
  }	
  
  $("body *").each(function() {
    if ($(this).children().size() > 0) return;
    if ($(this).is("xmp, pre")) return;
    var newhtml = $(this).html();
    for(i=0; i<re.length; i++) {
			newhtml = newhtml.replace(re[i], '<font style="background-color:yellow">$1</font>');
		}
    
    $(this).html(newhtml);
  });
  
}