var resultsSuffix = '_results';
var nothingFound = 'Nothing found for ';
var searchControl = null;
var drawOptions = null;
var recommended = null;
var searches = {};

var map = null;
var geocoder = null;
var defaultZoom = 13;

var DigitalPlatform = function()
{
	
}

DigitalPlatform.prototype.require = function( src )
{
	document.write('<script type="text/javascript" src="'+src+'"><\/script>');
}

DigitalPlatform.prototype.google = function()
{
	google.load( "search", "1", { "nocss" : true } );
	google.setOnLoadCallback( platform.onSearchLoad );
}

DigitalPlatform.prototype.maps = function()
{
	google.load( "maps", "2" );
}

DigitalPlatform.prototype.searchComplete = function( id, searcher, query )
{
	searches[ id ] = false;
	
	var div = $( '#' + id + resultsSuffix );
	//div.show( 'fast' );
	div.find( '.loading' ).remove();				
	
	//alert( searcher + " || " + searcher.cursor );
	
	if( !searcher || !searcher.cursor || !searcher.results || searcher.results.length == 0 )
	{
		//alert( nothingFound );
		
		div.prepend( '<h5>' + nothingFound + '<em>"' + query + '"</em></h5>' );
		
		return;
	}
	
	if( searcher.cursor && searcher.results )
	{
		//alert( "Search found : " + searcher.cursor.estimatedResultCount );
		
		if( searcher.results )
		{
			var i = 0;
			var l = searcher.results.length;
			var result = null;
			var node = null;
			
			div.prepend( '<div class="search_result_set" />' );
			var resultSet = div.find( '.search_result_set' );
			resultSet.append( "<h5>Showing " + searcher.results.length + " results for <em>\"" + query + "\"</em> - " + searcher.cursor.estimatedResultCount + " estimated matches</h5>" );
			
			for( ;i < l;i++ )
			{
				result = searcher.results[ i ];
				node = result.html.cloneNode( true );
				resultSet.append( node );
			}
		}
	}
}

DigitalPlatform.prototype.searchStarting = function( id, searcher, query )
{
	//alert( 'searchStarting: ' + id );
	
	var div = $( '#' + id + resultsSuffix );
	if( div )
	{
		div.append( '<div class="loading"></div>' ).find( '.loading' ).append( '<img src="/platform/images/spinner.gif" /> <em>Searching for ' + query +  '</em>' );
		
		var close = div.append( '<div class="close"></div>' );
		var link = close.find( ".close" ).append( 'x - <a class="close_link" href="#">close</a>' );
		link.click(	function()
		{
			$( '#' + id + resultsSuffix ).hide( 'fast', function(){ $( '#' + id + resultsSuffix ).html( '' ) } );
			return false;
		} );
		
	
		div.show( 'fast' );
	}
}

DigitalPlatform.prototype.searchRecommendedSites = function( query, id )
{
	
	//currently searching for this id
	if( searches[ id ] )
	{
		return false;
	}
	
	var div = $( '#' + id + resultsSuffix );	
	
	if( searchControl && div )
	{
		
		div.html( '' );
		
		var recommended = new google.search.WebSearch();
		recommended.setSiteRestriction( "amazon.com" );
		recommended.setSiteRestriction( "myspace.com" );
		//recommended.setSiteRestriction( "lastminute.com" );

		var options = new google.search.SearcherOptions();
		options.setExpandMode( google.search.SearchControl.EXPAND_MODE_OPEN );
		searchControl.addSearcher( recommended, options );
		
		//searchControl.setResultSetSize(google.search.Search.LARGE_RESULTSET);
		searchControl.setResultSetSize(google.search.Search.SMALL_RESULTSET);
		
		searches[ id ] = true;
		
		//TODO: remove this temporary query suffix
		var tempQuerySuffix = "";
		var modifiedQuery = query + tempQuerySuffix;
		
		//original query is passed to the callbacks
		recommended.setSearchCompleteCallback( platform, platform.searchComplete , [ id, recommended, query ] );
		platform.searchStarting( id, recommended, query );
		
		//execute the modified query
		recommended.execute( modifiedQuery );
		
		return true;
	}
}

DigitalPlatform.prototype.onSearchLoad = function()
{
	
	drawOptions = new google.search.DrawOptions();
	
	searchControl = new google.search.SearchControl();
	
	//drawOptions.setDrawMode(google.search.SearchControl.DRAW_MODE_TABBED);



	searchControl.setResultSetSize( google.search.Search.LARGE_RESULTSET );

	/*
	searchControl.addSearcher(new google.search.LocalSearch());
	searchControl.addSearcher(new google.search.VideoSearch());
	searchControl.addSearcher(new google.search.BlogSearch());
	*/

	// Set the Local Search center point
	//localSearch.setCenterPoint("New York, NY");

	//initial test
	//platform.searchRecommendedSites( "musicals" );	
	
	//alert( platform );
}

DigitalPlatform.prototype.searchResult = function( point )
{
	if( point )
	{
		map.setCenter( point, defaultZoom );
	}
}

DigitalPlatform.prototype.searchMap = function( address )
{
	geocoder.getLatLng( address, platform.searchResult );
}

DigitalPlatform.prototype.embedMap = function( id ) {
	
	if( GBrowserIsCompatible && GBrowserIsCompatible() )
	{
		geocoder = new GClientGeocoder();
		
		map = new GMap2( document.getElementById( id ) );
		map.addControl(new GLargeMapControl());
		map.addControl(new GScaleControl()); 	
		map.addControl(new GMapTypeControl()); 	
		map.addControl(new GOverviewMapControl());
	}
}

platform = new DigitalPlatform();

var tableSuffix = "Table";
var sortableTables = [ "skills" ];

$(document).ready(function() 
    {
		var i = 0;
		var l = sortableTables.length;
		var table = null;
		
		for( ;i < l;i++ )
		{
			table = $( "#" + sortableTables[ i ] + tableSuffix );
			
			if( table )
			{
	        	table.tablesorter( { sortList: [ [ 1,0 ] ] } );
				
				//TODO : add logic so that odd/even colour distinction is maintained
				
				/*
				$("table").bind("sortStart",function() { 
				        $("#overlay").show(); 
				    }).bind("sortEnd",function() { 
				        $("#overlay").hide(); 
				    });
				*/
			}			
		}
    }
);