/**
 * VCMGoogleSearch
 * @author Réjean Bouchard
 * @author Jonathan Dextraze <jonathan.dextraze@isiconseil.com>
 */
function VCMGoogleSearch(page) {
	this._page = page;
}

VCMGoogleSearch.prototype = {
	/**
	 * Reference to a VCMPage object
	 * @type {VCMPage}
	 */
	_page: null,
	
	/**
	 * jQuery reference to the search result container
	 * @type {jQuery}
	 */
	_cse: null,
	
	/**
	 * Initialize
	 */
	init: function() {
		console.log('VCMGoogleSearch.init()');
		this._cse = $('#cse');
		// Note: The search will only be loaded one time. Callback is always called.
		google.load('search', '1.0', {language : 'fr', nooldnames: "true", callback: $.proxy(this,'onSearchLoaded')});
	},	
	
	/**
	 * Destroy
	 */
	destroy: function() {
		console.log('VCMGoogleSearch.destroy()');
		this._cse = null;
	},

	/**
	 * Hide the search results
	 * This function is called by the onUnload function of VCMContent
	 */
	hideResults: function(){
		console.log('VCMGoogleSearch.hideResults()');
		$('#content').show();
		this._cse.hide();
	},

	/**
	 * Search complete callback
	 * @see CustomSearchControl.setSearchCompleteCallback
	 */
	onSearchComplete: function(){
		console.log('VCMGoogleSearch.onSearchComplete()');
		$('#content').hide();
		this._cse.show();
	},

	/**
	 * Search loaded callback
	 * @see google.load
	 */
	onSearchLoaded: function(){
		console.log('VCMGoogleSearch.onSearchLoaded()');
		// Init local
		var lang = VCMUtils.getLang().toLowerCase();
		var cx;
		if (lang == 'en') {			
			google.search.CurrentLocale='en';
			//google.search.a.strings.search = 'search';
			//google.search.a.strings['search-uc'] = 'Search';
			cx = '009768563285077055153:vr-nxzsmoa0';
		} else {
			google.search.CurrentLocale='fr';
			//google.search.a.search = 'rechercher';
			//google.search.a.strings.search = 'rechercher';
			//google.search.a.strings['search-uc'] = 'Rechercher';
			cx = '009860986635575945772:glvnxnx01ze';
		}
		// Create a custom search control
		var customSearchControl = new google.search.CustomSearchControl(cx);
		customSearchControl.setSearchCompleteCallback(this, this.onSearchComplete);
		customSearchControl.setResultSetSize(google.search.Search.FILTERED_CSE_RESULTSET);
		customSearchControl.setNoResultsString(lang == 'fr' ? 'Aucun résultat' : 'No results');
		customSearchControl.setLinkTarget(google.search.Search.LINK_TARGET_SELF);
		// Specify draw options
		var options = new google.search.DrawOptions();
		options.setSearchFormRoot('cse-search-form');
		options.setAutoComplete(true);
		// Draw the search
		customSearchControl.draw('cse', options);
		
		//TODO : Utiliser la nouvelle API de Google.
		if (lang == 'en') {			
			$(".gsc-search-button").attr("value", "Search");
			//alert($('#topLinks input:eq(0)').attr("class"));
			$('#topLinks input:eq(0)').css("background-image", 'url("http://www.google.com/cse/intl/en/images/google_custom_search_watermark.gif');
		} else {
			$(".gsc-search-button").attr("value", "Rechercher");
		}
	}

};

