/**
 * Ask Zoom javascript class
 */

/**
 * Create a Syn.AskZoom component instance
 * @constructor
 */
Syn.AskZoom = Syn.Component.extend (
{
	/**
	 * Initialize the component class. This is called automatically by the default constructor.
	 * @member Syn.AskZoom
	 * @param {Object} config The configuration data structure
	 */
	init: function(config)
	{
		this._super(config);

		var type            = config['type'];	
		var uid             = config['uid'];	
		this.ask_proxy_url  = config['ask_proxy_url'];	
		this.ask_options    = config['ask_options'];	
		this.ask_url_params = config['ask_url_params'];	
		this.ask_visible    = config['visible'];

		if (this.ask_visible == 'no')
		{
			$('#' + type + '_' + uid).hide();
			this.container().find('img:last').connect('load',this, function ()
			{
				$('#serp_results_cont .serp_listing_cont').attr('class','serp_listing_cont_no_margin');
			});

		}
		else
		{
			// Make the request to the proxy script
			this.makeRequest(this.ask_proxy_url);
		}
	},

	/**
	 * Make the request to the proxy via getJSON
	 * @member Syn.AskZoom
	 */
	makeRequest: function(request_url)
	{
		var full_url = request_url + '?params=' + json.serialize(this.ask_options) + '&';

		// Show the loading message
		this.showLoading();

		// We need to store a reference so the callback can reference Syn.AskZoom
		Syn.AskZoom.ajaxInstance = this;

		$.getJSON(request_url + '?params=' + json.serialize(this.ask_options) + '&jsoncallback=?', this.proxyCallback);

	},
		
	/**
	 * Launches the popup window with the flash game
	 * @member Syn.AskZoom
	 */
	proxyCallback: function (response)
	{
		Syn.AskZoom.ajaxInstance.response = response.result;

		// Display error if the request failed
                if (response.result == 'success')
		{
			Syn.AskZoom.ajaxInstance.buildResults(response.data);
		}
		// Otherwise, begin processing the response
		else
		{
			Syn.AskZoom.ajaxInstance.displayError('Component is not available at this time.');
		}
	},

	/**
	 * Shows loading graphic when retrieving data
	 * @member Syn.AskZoom
	 */
	showLoading: function()
	{
		$('#askzoom_content_area').hide();
		this.uniqueElmt('askzoom_loading').show().css( { 'width':$('#askzoom_content_area').width(), 'height':$('#askzoom_content_area').height() } );
	},

	/**
	 * This gets displayed when an error occurs.
	 * @member Syn.AskZoom
	 */
	displayError: function(message)
	{
		$('#askzoom_content_area').hide();
		$('#askzoom_loading').hide();
		$('#askzoom_image').hide();
		$('#askzoom_error_message').html(message);
	},

	/**
	 * Builds results and then displays it in the respective divs
	 * @member Syn.AskZoom
	 */
	buildResults: function(results)
	{
		$('#askzoom_loading').hide();
		$('#askzoom_content_area').show();

		var nameString = results.name.toString();
		var expandString = results.expand.toString();
		var narrowString = results.narrow.toString();

		if (nameString == '' && expandString == '' && narrowString == '')
		{
			Syn.AskZoom.ajaxInstance.displayError('There are no related search terms');
		}
		else
		{
			Syn.AskZoom.ajaxInstance.sendToDisplay(nameString,   'name');
			Syn.AskZoom.ajaxInstance.sendToDisplay(expandString, 'expand');
			Syn.AskZoom.ajaxInstance.sendToDisplay(narrowString, 'narrow');
		}
	},

	/**
	 * This sets the divs with the appropriate html
	 * @member Syn.AskZoom
	 */
	sendToDisplay: function(dataString, cat)
	{
		var results    = dataString.split(',');
		var fullpath   = window.location.pathname;
		var scriptname = fullpath.substring(fullpath.lastIndexOf('/') + 1);
		var resultsLength = results.length;
		var params = this.ask_url_params;
		
		if (resultsLength >= 1 && dataString != '')
		{	
			var display   = '<ul>';

			for (var i = 0; i < resultsLength; i++)
			{
				var curResult = results[i];
				display += '<li><a href="' + scriptname + '?q=' + curResult + '&' + params + '&qry_lnk=' + escape(curResult) + '">' + curResult  + '</a></li>';
			}
			display   += '</ul>';

			$('#askzoom_'+ cat +'_search').html(display);
		}
		else
		{
			$('#askzoom_'+ cat +'_head').hide();
		}
		
	}

});

