// HELPER FOR TOGGLING AN INPUT FIELD LABEL
function input_toggle(selector,message)
{
	$(selector).focus(function(){
		if($(this).val()==message) $(this).val('')
	}).blur(function(){
		if($(this).val()=='') $(this).val(message)
	});
}


// SEARCH BOX ___________________

$('.menu a.search').live('click',function(e){
	
	e.preventDefault();
	
	$('.search_block').slideToggle();
	
});

/*
*  The SearchControl manages searchers and draws a UI for them.  However,
*  searchers can be used by themselves without the SearchControl.  This is
*  called using a "Raw Searcher".  When doing this, you must handle and draw
*  the search results manually.
*/

google.load('search', '1');

var webSearch;

$('#search_submit').live('click',function(e){
								   
	e.preventDefault();
	
	if ( $('#search_value').val() ) webSearch.execute( $('#search_value').val() );
	else alert(  'Please include a search query' );
	
});

function OnLoad()
{
	webSearch = new google.search.WebSearch();
	webSearch.setResultSetSize(google.search.Search.LARGE_RESULTSET);
	webSearch.setSiteRestriction('www.petshopboys.co.uk');
	webSearch.setSearchCompleteCallback(this, searchComplete, null);
	
	// Find me a beautiful car.
	//webSearch.execute("Subaru STI");
}
google.setOnLoadCallback(OnLoad);

function searchComplete() {
	
	// Check that we got results
	if (webSearch.results && webSearch.results.length > 0)
	{
		var d = $('#search_content').empty().append('<div class="hr"><hr></hr></div><p></p>');
		
		// Loop through our results, printing them to the page.
		var results = webSearch.results;
		
		for (var i = 0; i < results.length; i++) {
			// For each result write it's title and image to the screen
			var result = results[i];
			
			d.append(result.html).append('<p></p>');
		}
	
		// Now add the paging links so the user can see more results.
		addPaginationLinks(webSearch);
	}
}

function addPaginationLinks()
{
	// The cursor object has all things to do with pagination
	var cursor = webSearch.cursor,
		curPage = cursor.currentPageIndex, // check what page the app is on
		pagesDiv = $('<div class="search-pagination">Pages &mdash; </div>');
		
	$.each(cursor.pages,function(i){
		var page = this;
		
		if (curPage == i) // Current
		{ 
		  pagesDiv.append('<b> ' + page.label + ' </b>');
		}
		else // Link
		{
		  var link = $('<a href="#"> '+page.label+' </a>').click(function(e){
				e.preventDefault();
				webSearch.gotoPage(i);
			});
		
		  pagesDiv.append(link);
		}
	});
	
	$('#search_content').append(pagesDiv);
}

