/************************************************/
/* IPB3 Javascript								*/
/* -------------------------------------------- */
/* ips.googleSearch.js (IM) Google Site Search	*/
/* (c) Invision Modding, 2010					*/
/* -------------------------------------------- */
/* Author: Martin Aronsen						*/
/************************************************/

var _googleSearch = window.IPBoard;

_googleSearch.prototype.googleSearch = 
{
	cseKey: '',
	alwaysDefault: false,
	notProperlySetup: false,
	
	init: function()
	{
		Debug.write( "Initializing ips.googleSearch.js" );
		
		document.observe( 'dom:loaded', function()
		{
			ipb.googleSearch.initEvent();
		});
	},

	initEvent: function()
	{		
		Debug.write( "Initializing googleSearch events" );
		
		if ( ipb.googleSearch.notProperlySetup == true )
		{
			errorDiv = new Element( 'div', { 'class': 'message error' });
			errorDiv.update( '<h2>Admin warning</h2>(IM) Google Site Search is not properly setup. Please refer to the system settings or readme file for this hook for more information on how to set it up. NB: An AJAX API key IS required!<br />If you don\'t want to use this hook, disable or uninstall it to remove this warning message.' );
			$( 'content' ).insert( { top: errorDiv } );
		}
		else
		{
			if ( $( 'search-box' ) )
			{
				$$( '#search-box .submit_input' )[0].observe( 'click', ipb.googleSearch.doSearch );
			}
			
			/* Always make this the default search engine */
			if ( ipb.googleSearch.alwaysDefault )
			{
				$$( '#search_options_menucontent .input_radio' ).each( function( elem )
				{
					$( elem ).checked = false;
				});
				
				$( 's_google' ).checked = true;
			}
		}
	},
	
	doSearch: function(e)
	{
		if ( $( 's_google' ).checked == true )
		{
			Event.stop(e);
			
			afterAjax = function( popupObj )
			{
				var customSearchControl = new google.search.CustomSearchControl( ipb.googleSearch.cseKey );

				// Add in a full set of searchers
				customSearchControl.addSearcher( new google.search.WebSearch() );
				customSearchControl.addSearcher( new google.search.VideoSearch() );
				customSearchControl.addSearcher( new google.search.BlogSearch() );
				
				customSearchControl.setLinkTarget(google.search.Search.LINK_TARGET_PARENT);
				customSearchControl.setSearchCompleteCallback( ipb, ipb.googleSearch.pageChangeScrollTop );
				
				var drawOptions = new google.search.DrawOptions();
				customSearchControl.draw( 'googleSearchResults', drawOptions );
				customSearchControl.execute( $F( 'main_search' ) );
				
				$( 'document_modal' ).observe( 'click', function()
				{
					popupObj.hide();
				});

			};
			
			afterHide = function( popupObj)
			{
				$( popupObj.wrapper ).remove();
			};
			
			_url = ipb.vars['base_url'] + 'app=core&module=ajax&section=googleSearch&secure_key=' + ipb.vars['secure_hash'];
			
			popup = new ipb.Popup( 'googleSearchPopup', 
			{ 
				type: 'pane', 
				modal: true, 
				w: '800px', 
				h: '480px', 
				ajaxURL: _url,
				hideAtStart: false, 
			},
			{
				afterShow: afterAjax,
				afterHide: afterHide
			});
		}
			
		
		return true;
	},
	
	pageChangeScrollTop: function( searchControl, searcher )
	{
		$$( '.gsc-cursor-page' ).each( function(elem)
		{
			elem.identify();
			
			$( elem ).observe( 'click', function(e)
			{
				$$( '.gsc-wrapper' )[0].scrollTop = 0;
			});
		});
	}
}

ipb.googleSearch.init();
