var current = 0;

function showResult(str)
{
	current = 0;

	if (str.length==0)
	  { 
		  document.getElementById("livesearch").innerHTML="";
		  document.getElementById("livesearch").style.border="0px";
		  document.getElementById("livesearch").style.background="none";	  
		  return;
	  }
	if (window.XMLHttpRequest)
	  {// code for IE7+, Firefox, Chrome, Opera, Safari
		xmlhttp=new XMLHttpRequest();
	  }
	else
	  {// code for IE6, IE5
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	  }
	xmlhttp.onreadystatechange=function()
	  {
	  if (xmlhttp.readyState==4 && xmlhttp.status==200)
		{
			document.getElementById("livesearch").innerHTML=xmlhttp.responseText;
			document.getElementById("livesearch").style.border="1px solid #A5ACB2";
			document.getElementById("livesearch").style.backgroundColor="#FFFFFF";
		}
	  }
	xmlhttp.open("GET","/livesearch.php?q="+str,true);
	xmlhttp.send();
}

function moreSearch(str, page)
{

	if (str.length==0)
	  { 
		  document.getElementById("livesearch").innerHTML="";
		  document.getElementById("livesearch").style.border="0px";
		  document.getElementById("livesearch").style.background="none";	  
		  return;
	  }
	if (window.XMLHttpRequest)
	  {// code for IE7+, Firefox, Chrome, Opera, Safari
		xmlhttp=new XMLHttpRequest();
	  }
	else
	  {// code for IE6, IE5
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	  }
	xmlhttp.onreadystatechange=function()
	  {
	  if (xmlhttp.readyState==4 && xmlhttp.status==200)
		{
			document.getElementById("moresearch"+page).innerHTML = xmlhttp.responseText;
		}
	  }
	xmlhttp.open("GET","/livesearch.php?q="+str+"&page="+page,true);
	xmlhttp.send();
}

function hideSearch()
{
	document.getElementById('livesearch').innerHTML='';
	document.getElementById('livesearch').style.border='none';
	document.getElementById('livesearch').style.background='none';
}
function doSearch(e)
{
	var code = e?e.which:window.event.keyCode;
	
	if (code == 38){
		current = (current > 1) ? current - 1 : 1;
		
		var tmp = current + 1;
		
		document.getElementById("td"+current).style.background = "#DDDDDD";
		document.getElementById("td"+tmp).style.background = "#FFFFFF";
	}
	
	if (code == 40){
		
		var tmp = current + 1;
		var link = document.getElementById("td"+tmp);
		
		if (link)
		{
			if (current > 0) document.getElementById("td"+current).style.background = "#FFFFFF";
			current++;
			link.style.backgroundColor = "#DDDDDD";
		}
	}
	
	if (code == 38 || code == 40)
	{
		//alert(current);
	}
	else
	{
		if (code == 13){
			if (current > 0) location.href = document.getElementById("link"+current).href;
		} else
		{
		
			if (typeof loadTimer != 'undefined')
			{
				clearTimeout(loadTimer);
			}
			
			loadTimer=setTimeout('showResult(\''+addslashes(document.getElementById('search').value)+'\')', 1200);
		}
	}
	
}

function addslashes (str) {
    return (str+'').replace(/[\\"']/g, '\\$&').replace(/\u0000/g, '\\0');
}

function slideSwitch() {
    var $active = $('#slideshow DIV.active');

    if ( $active.length == 0 ) $active = $('#slideshow DIV:last');

    // use this to pull the divs in the order they appear in the markup
    var $next =  $active.next().length ? $active.next()
        : $('#slideshow DIV:first');

    // uncomment below to pull the divs randomly
    // var $sibs  = $active.siblings();
    // var rndNum = Math.floor(Math.random() * $sibs.length );
    // var $next  = $( $sibs[ rndNum ] );


    $active.addClass('last-active');
    $next.css({opacity: 0.0})
        .addClass('active')
        .animate({opacity: 1.0}, 1000, function() {
            $active.removeClass('active last-active');
        });
}

$(function() {
    setInterval( "slideSwitch()", 3500 );
});
