// JavaScript Document

if (document.addEventListener)
	document.addEventListener('keydown',function(evt){var keyCode = evt.keyCode; checkKey (keyCode,evt);},true);
else if (document.attachEvent)
	document.onkeydown = doSomething;

function doSomething(){
	evt = window.event;
	var keyCode = evt.keyCode;
	checkKey (keyCode,evt);
}



var searchHighlight = -1;
var numResults = 15;
var focusok = false;

function highlight (id)
{
	focusok = true;
	if (id > 0)
	{
		document.getElementById('div'+id).style.backgroundColor="#71bf44";
		document.getElementById('div'+id).style.color="#FFFFFF";
		document.getElementById('link'+id).focus();
		
		tmp = document.getElementById('star'+id).src;
		if (tmp.substr((tmp.length-10)) != "-hover.gif")
			document.getElementById('star'+id).src= tmp.substr(0,(tmp.length-4)) + "-hover.gif";
	}
	else
	{
		document.getElementById('search-field').focus();
	}
}

function unhighlight (id)
{
	document.getElementById('div'+id).style.backgroundColor="";
	document.getElementById('div'+id).style.color="";
	tmp = document.getElementById('star'+id).src;
	if (tmp.substr((tmp.length-10)) == "-hover.gif")
		document.getElementById('star'+id).src= tmp.substr(0,(tmp.length-10)) + ".gif";

	focusok = false;
	if (searchHighlight == id)
		blurSearch();
}



function resultOver (id,stars)
{
/*	if (searchHighlight > 0)
		unhighlight(searchHighlight);*/
	searchHighlight = id;
	highlight(searchHighlight);
}

function resultOut(id,stars) {
	//unhighlight(id);
}


function checkKey(keyCode,evt)
{
	if (document.getElementById('search-field').value != '')
	{
		var navigateUp = keyCode == 38;
		var navigateDown = keyCode == 40;

/*		if((navigateUp || navigateDown)){
			if(evt.stopPropagation){
				evt.stopPropagation();
			}
			else{
				evt.cancelBubble = true;
			}
			return false;
		}*/	


		if (navigateUp || navigateDown)
		{
			if(evt.preventDefault){
				evt.preventDefault();
			}
			else{
				evt.returnValue = false;
			}

	/*		if (searchHighlight > 0)
				unhighlight(searchHighlight);*/
			
			if (navigateUp)
			{
				if (searchHighlight > 1)
					searchHighlight --;
				else
					searchHighlight = -1;
			}
			if (navigateDown)
			{
				if (searchHighlight == -1)
					searchHighlight = 1;
				else if (searchHighlight < numResults)
					searchHighlight ++;
				else
					searchHighlight = numResults;
			}
			
			highlight(searchHighlight);
		}
	}
}

/*
function resultOver(number,stars) {
	document.getElementById('result'+number).style.backgroundColor="#71bf44";
	document.getElementById('result'+number).style.color="#FFFFFF";
	document.getElementById('star'+number).src="pic/search/stars/star"+stars+"-hover.gif";
}

function resultOut(number,stars) {
	document.getElementById('result'+number).style.backgroundColor="";
	document.getElementById('result'+number).style.color="";
	document.getElementById('star'+number).src="pic/search/stars/star"+stars+".gif";
}*/

function getHTML() 
{ 
	if (xmlHttp.readyState==4)
	{ 
		document.getElementById('result-container').innerHTML=xmlHttp.responseText;
	}
	else if (document.getElementById('result-container').innerHTML == '')
	{
		document.getElementById('result-container').innerHTML="<p>&nbsp;</p><p align='center'><img src='pic/search/ajax-loader.gif' border='0' /></p><p>&nbsp;</p>";
	}
}

function getResults(url) {
	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null)
	{
		alert ("Your browser does not support AJAX!");
		return;
	}
	url = url + "&sid="+Math.random();
	xmlHttp.onreadystatechange=getHTML;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}

function GetXmlHttpObject()
{
  var xmlHttp=null;
  try
	{
	// Firefox, Opera 8.0+, Safari
	xmlHttp=new XMLHttpRequest();
	}
  catch (e)
	{
	// Internet Explorer
	try
	  {
	  xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
	  }
	catch (e)
	  {
	  xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
	  }
	}
  return xmlHttp;
}

function changeSearch() {
	offset = findPos(document.getElementById('search-div'));
	document.getElementById('result-container').style.left=(offset[0]-70)+'px';
	document.getElementById('result-container').style.top=(offset[1]+25)+'px';

	if (document.getElementById('search-field').value != '')
	{
		document.getElementById('result-container').style.visibility = 'visible';
		query = document.getElementById('search-field').value.replace(/&/gi,'');
		getResults('search.php?query='+query);
	}
	else
	{
		document.getElementById('result-container').style.visibility = 'hidden';
		document.getElementById('result-container').innerHTML = '';
	}
}

function focusSearch() {
	document.getElementById("search-div").style.backgroundImage="url(pic/search/bg-active.jpg)";	
	document.getElementById("search-field").style.backgroundColor="#FFFFFF";

	if (document.getElementById('search-field').value != '')
	{
		document.getElementById('result-container').style.visibility = 'visible';
		query = document.getElementById('search-field').value.replace(/&/gi,'');
		getResults('search.php?query='+query);
	}
}

function blurSearch() {
	if (!focusok)
	{
		document.getElementById("search-div").style.backgroundImage="url(pic/search/bg.jpg)";
		document.getElementById("search-field").style.backgroundColor="#FFFFFF";
	
		setTimeout(hideResults,200);
		searchHighlight = -1;
	}
}

function hideResults() {
	document.getElementById('result-container').style.visibility = 'hidden';
	document.getElementById('result-container').innerHTML = '';
}

function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		do {
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
		} while (obj = obj.offsetParent);
	}
	return [curleft,curtop];
}
