/**
* @description CDSearchAPI's main file. Includes support for an Ajax cacheing service.
* @author Chloé Desoutter
*/

var CDSearchAPI = Class.create(
	{
		initialize: function(options)
		{
			options.resultCallback = this.gotWebResult;
			options.resultCallbackContext = this;
			this.options = new Object();
			this.searchLanguage = Languages.en_US;
			this.webSearch = new WebSearch(options);
			this.ajaxCachePath = ajaxCachePath;
			this.parseParams(options);
			
		},

		parseParams: function(options)
		{
			if ('ajaxCachePath' in options)
			{
				this.ajaxCachePath = options.ajaxCachePath;
				this.options.ajaxCachePath = options.ajaxCachePath;
			}
			if ('resultAvailableCB' in options)
			{
				this.resultAvailable = options.resultAvailableCB;
				this.options.resultAvailable = options.resultAvailableCB;
			}
			if ('searchLanguage' in options)
			{
				this.searchLanguage = Languages[options.searchLanguage];
				this.options.searchLanguage = options.searchLanguage;
			}
			if ('pageWanted' in options)
			{
				this.pageWanted = options.pageWanted;
				this.options.pageWanted = options.pageWanted;
			}
//			this.imgSearch = new ImageSearch(options);
		},
		
		searchweb: function(phrase, options)
		{
			this.parseParams(options);
			var that = this;
			this.searchPhrase = phrase;
			var swc = this.searchwebCallback;
			var swnc = this.searchwebNoCache;
			new Ajax.Request(
			this.ajaxCachePath,
			{
				method: 'post',
				parameters:
				{
					'service': 'Web',
					'keywords': phrase + that.searchLanguage.Bing,
					'action': 'retr'
				},
				onSuccess: swc.bind(that),
				onFailure: swnc.bind(that)
			}
			);
			
		},

		cachedResult: function(transport)
		{
			var id_cache = transport.responseText;
			var json_str = this.jsonString;

			this.resultAvailable(json_str.evalJSON(), id_cache, undefined, this.pageWanted);
		}
	}
);
