/*
* Author:      Marco Kuiper (http://www.marcofolio.net/)
*/
google.load("jquery", "1.3.1");

var lastEnd = 0;
var searchState = "off";
var incremental = 11;

function clearInput()
{
document.getElementById('searchQuery').value = ""; 
$('#autocomplete').fadeOut(); 

			$('#autocomplete').html(''); 
}

 function bodyFirstLoad()
{
	var p = $_GET('p');
	if((p.length >0 || p != 'undefined') && isNumber(p))
	{
	searchLive(p);
	}
	else{
	 lookup();
	 }
   
}

function isNumber(n) {
  return !isNaN(parseFloat(n)) && isFinite(n);
}

  function $_GET(q,s) { 
        s = s ? s : window.location.search; 
        q = q ? q : '';
        var re = new RegExp('&'+q+'(?:=([^&]*))?(?=&|$)','i'); 
        return (s=s.replace('?','&').match(re)) ? (typeof s[1] == 'undefined' ? '' : decodeURIComponent(s[1])) : 'undefined'; 
    } 
    
function lookup() 
{
	
		// $('#suggestions').fadeOut();
		// Hide the suggestions box
		$('#more').html('Loading ... ');
			$('#autocomplete').fadeOut(); 
	if(searchState == "on")
	{
	
		var searchQuery = document.getElementById('searchQuery'); 
		var myQuery = searchQuery.value;
		$.post("rpc.php", {queryString: ""+myQuery+"", lowerLimit: ""+lastEnd+""}, function(data) { 
			if(data.length <= 1)
			{
			$('#loadMore').fadeOut(); 
			$('#suggestions').fadeIn(); // Show the suggestions box
			$('#suggestions').append('No results'); // Fill the suggestions box
			}
			else if(data.substr(data.length-5, 2) < 10)
			{ $('#loadMore').fadeOut(); 
			$('#suggestions').fadeIn(); // Show the suggestions box
			$('#suggestions').append(data); // Fill the suggestions box
			}
			else
			{
			$('#loadMore').fadeIn();
			$('#suggestions').fadeIn(); // Show the suggestions box
			$('#suggestions').append(data); // Fill the suggestions box
			$('#more').html('Load More');
			}
		});
	
		lastEnd = lastEnd + incremental;
	}
	else
	{
		$.post("rpc.php", {lowerLimit: ""+lastEnd+""}, function(data) { 
					if(data.length <= 1)
			{ $('#loadMore').fadeOut(); }
			else{
			 $('#loadMore').fadeIn();
			$('#suggestions').fadeIn(); // Show the suggestions box
					
			$('#suggestions').append(data); // Fill the suggestions box
			$('#more').html('Load More');
		}
			});
	
	lastEnd = lastEnd + incremental;
	}
}

function autocomplete()
{
	var searchQuery = document.getElementById('searchQuery'); 
		var myQuery = searchQuery.value;
		
		if(myQuery.length>1)
		{
		if (self.timer) { clearTimeout(self.timer);	}
		self.timer = setTimeout(function(){ 
		$.post("rpc.php", {queryType: "complete", queryString: ""+myQuery+"", lowerLimit: "0"}, function(data) { 
		 
			if(data.length <= 1)
			{ $('#autocomplete').fadeOut(); }
			else if(data.substr(data.length-5, 2) < 1)
			{ $('#autocomplete').fadeOut(); }
			else
			{
			$('#autocomplete').fadeIn();
			$('#autocomplete').html(data); // Fill the suggestions box
			}
		});
		}, 500);
		}
}

function complete(id, phrase)
{
$('#autocomplete').fadeOut(); 
document.getElementById('searchQuery').value = phrase; 
searchLive(id);
}

function searchLive(id) 
{
	
	
		lastEnd = 0;
		searchState = "on";

	

  var searchQuery = document.getElementById('searchQuery'); 
  var myQuery = searchQuery.value;
	$('#loadMore').fadeOut();
	$('#suggestions').fadeOut(); 
	$('#autocomplete').fadeOut(); 
		
		if(id)
		{$.post("rpc.php", {queryType: "identify", queryID: ""+id}, function(data) { 
			 
			$('#suggestions').fadeIn(); // Show the suggestions box
			
			$('#suggestions').html(data); // Fill the suggestions box
		
			});
			}
			else{
			
			$.post("rpc.php", {queryString: ""+myQuery+"", lowerLimit: ""+lastEnd+""}, function(data) { 
			if(data.substr(data.length-5, 2) < 10)
			{ $('#loadMore').fadeOut(); }
			else{
			$('#loadMore').fadeIn();}
		 
			$('#suggestions').fadeIn(); // Show the suggestions box
			
			$('#suggestions').html(data); // Fill the suggestions box
		
			});
			}

	lastEnd = lastEnd + incremental;
}


