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

function cse()
{
	// Create the navbar search form
	this.searchFormNavBar = new google.search.SearchForm(true, document.getElementById( "navbar-search" ) );
	this.searchFormNavBar.userDefinedCell.innerHTML = '<div class="branding-trans">&nbsp;</div>';
	this.searchFormNavBar.input.onfocus = function() { this.select() };

	// bind clear and submit functions
	this.searchFormNavBar.setOnSubmitCallback( this, this.onSubmit );
	this.searchFormNavBar.setOnClearCallback( this, this.onClear );

	// Get the search value
	this.searchValue = document.getElementById( "q" );
	this.defaultValue = "Search...";

	if ( this.isSearchPage() )
	{
		this.initSearchControls();
	}

	if ( "" != this.searchValue.value )
	{
		// We have a value that was submitted from the navbar
		// so execute it
		this.searchFormNavBar.input.value = this.searchValue.value;
		this.searchFormNavBar.execute();
	}
	else
	{
		this.searchFormNavBar.input.value = this.defaultValue;
		if ( this.isSearchPage() )
		{
			// Hide the spinner
			this.searchComplete();
		}
	}
}

cse.prototype.initSearchControls = function()
{
	// Create the navbar search form
	this.searchFormMain = new google.search.SearchForm( true, document.getElementById( "main-search" ) );
	this.searchFormMain.input.onfocus = function() { this.select() };

	// bind clear and submit functions
	this.searchFormMain.setOnSubmitCallback( this, this.onSubmit );
	this.searchFormMain.setOnClearCallback( this, this.onClear );

	// setup the value
	if ( "" != this.searchValue.value )
	{
		this.searchFormMain.input.value = this.searchValue.value;
	}
	else
	{
		this.searchFormMain.input.value = this.defaultValue;
	}

	// Create the search controls
	var searchControlLeft = new google.search.SearchControl();
	searchControlLeft.setResultSetSize( GSearch.LARGE_RESULTSET );
	searchControlLeft.setLinkTarget( GSearch.LINK_TARGET_SELF );
	searchControlLeft.setNoResultsString( GSearch.NO_RESULTS_DEFAULT_STRING );

	var searchControlRight = new google.search.SearchControl();
	searchControlRight.setResultSetSize( GSearch.LARGE_RESULTSET );
	searchControlRight.setLinkTarget( GSearch.LINK_TARGET_SELF );
	searchControlRight.setNoResultsString( GSearch.NO_RESULTS_DEFAULT_STRING );

	// Add in a full set of searchers
	var siteSearch;
	var cseId = "014963542567789284062:l3vopnvg098";

	var options = new google.search.SearcherOptions();
	options.setExpandMode(GSearchControl.EXPAND_MODE_OPEN);

	// News
	siteSearch = new google.search.WebSearch();
	siteSearch.setUserDefinedLabel( "News Search" );
	siteSearch.setUserDefinedClassSuffix( "siteSearch" );
	siteSearch.setSiteRestriction(cseId, "News" );
	searchControlLeft.addSearcher( siteSearch, options );

	// Servers
	siteSearch = new google.search.WebSearch();
	siteSearch.setUserDefinedLabel( "Servers" );
	siteSearch.setUserDefinedClassSuffix( "siteSearch" );
	siteSearch.setSiteRestriction(cseId, "Servers" );
	this.serversSearch = siteSearch;

	// Supports
	siteSearch = new google.search.WebSearch();
	siteSearch.setUserDefinedLabel( "Support" );
	siteSearch.setUserDefinedClassSuffix( "siteSearch" );
	siteSearch.setSiteRestriction(cseId, "Support" );
	this.supportSearch = siteSearch;

	// Fileplay
	siteSearch = new google.search.WebSearch();
	siteSearch.setUserDefinedLabel( "Fileplay" );
	siteSearch.setUserDefinedClassSuffix( "siteSearch" );
	siteSearch.setSiteRestriction(cseId, "Downloads" );
	this.fileplaySearch = siteSearch;

	// Events
	siteSearch = new google.search.WebSearch();
	siteSearch.setUserDefinedLabel( "Events" );
	siteSearch.setUserDefinedClassSuffix( "siteSearch" );
	siteSearch.setSiteRestriction(cseId, "Events" );
	this.eventsSearch = siteSearch;

	// Community
	options = new google.search.SearcherOptions();
	siteSearch = new google.search.WebSearch();
	siteSearch.setUserDefinedLabel( "Community" );
	siteSearch.setUserDefinedClassSuffix( "siteSearch" );
	siteSearch.setSiteRestriction(cseId, "Community" );
	this.communitySearch = siteSearch;

	// Add the searches
	searchControlRight.addSearcher( this.serversSearch );
	searchControlRight.addSearcher( this.fileplaySearch );
	searchControlRight.addSearcher( this.communitySearch );
	searchControlRight.addSearcher( this.eventsSearch );
	searchControlRight.addSearcher( this.supportSearch );

	this.searchControlLeft = searchControlLeft;
	this.searchControlRight = searchControlRight;

	// the right control is drawn in tabbed mode
	var drawOptions = new google.search.DrawOptions();
	drawOptions.setDrawMode( GSearchControl.DRAW_MODE_TABBED );

	// Tell the searcher to draw itself and tell it where to attach
	searchControlLeft.draw( document.getElementById( "searchcontrol-left" ) );
	searchControlRight.draw( document.getElementById( "searchcontrol-right" ), drawOptions );

	// Search Complete hander
	searchControlLeft.setSearchCompleteCallback( this, this.searchComplete );
	searchControlRight.setSearchCompleteCallback( this, this.searchComplete );
}

cse.prototype.isSearchPage = function()
{
	return ( document.getElementById( "searchcontrols" ) ) ? true : false;
}

// when the form fires a submit, grab its
// value and call the left and right control
cse.prototype.onSubmit = function( oForm )
{
	if ( this.isSearchPage() )
	{
		// On the search page
		// Syncronise the search values and execute
		this.searchFormMain.input.value = oForm.input.value;
		this.searchFormNavBar.input.value = oForm.input.value;
		this.execute( oForm );
	}
	else
	{
		// Not on main search page submit to it
		this.searchValue.value = oForm.input.value;
		this.searchValue.form.submit();
		return true;
	}

	return false;
}

cse.prototype.searchComplete = function()
{
	document.getElementById( "search-loading" ).style.display = "none";

	return true;
}

cse.prototype.execute = function( oForm )
{
	var q = oForm.input.value;
	if ( q && q != "" )
	{
		document.getElementById( "search-loading" ).style.display = "";
		this.searchControlLeft.execute( q );
		this.searchControlRight.execute( q );

		return true;
	}

	return false;
}

// when the form fires a clear, call the left and right control
cse.prototype.onClear = function(form)
{
	if ( this.isSearchPage() )
	{
		this.searchControlLeft.clearAllResults();
		this.searchControlRight.clearAllResults();
	}
	form.input.value = "Search...";
	return false;
}

google.setOnLoadCallback( function() { new cse() } );

