/**
 * AS4AllResourcesPart 
 * Client-side Controller class for the all_resources_part
 *
 * @author Chris Blunt
 * @version 1.0
 */
$j = jQuery.noConflict();

var AS4AllResourcesPart = Class.create({

	/**
	 * @type {Element} The all_resources_part DOM element which this object controls
	element: null,

	/**
	 * @type {string} The currently set search string
	 */
	searchTerms: null,

	/**
	 * @type string Store a reference to the last terms that were successfully searched
	 */
	previousTerms: null,

	/**
	 * @type {AS4Channel} The part's containing channel controller
	 */
	channelController: null,

	/**
	 * @type {integer} The channel_widgets_id for this widget
	 */
	channelWidgetsId: null,
	
	perPageUpdated: "false",
	
	bottomChange: "",

	// # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # 

	/**
	 * @constructor
	 */
	initialize: function()
	{
	},

	/**
	 * Begins loading a page of resources for this all_resources component and updates that component within the containing channel's page.
	 *
	 * @param {integer} page The page number of resources to load
	 * @param {Hash} options Additional options, such as filters to apply
	 */
	refresh: function(page, options)
	{
		var options = Object.extend({
				limit: 3
			}, options
		);

		var url = '/widget/index/';

		var params = {
			renderMode: 'update',
			channel_widgets_id: this.channelWidgetsId,
			id: this.channelController.channelId,
			page: page,
			limit: options.limit,
			search_terms: this.searchTerms
		};

		//console.log(AS4Channel.getInstance());
		AS4Shell.getInstance().ajaxUpdate(url, params, $$('.widget_content.all_resources').first().identify(), $A([this.onRefreshComplete.bind(this)]), {showIndicator: false});
	},
	
	/**
	 * filter by the search terms
	 *
	 */
	filter: function(page)
	{
		
		cwid = AS4AllResourcesPart.getInstance().channelWidgetsId;
		var url = '/widget/index/';

		var keywords = $j("#filter_keyword_"+cwid).val();
		var month = $j("#filter_month_"+cwid).val();
		var year = $j("#filter_year_"+cwid).val();
		var category = $j("#filter_category_"+cwid).val();
		var language = $j("#filter_language_"+cwid).val();
		var perPage = $j("#show_per_page_"+cwid+""+AS4AllResourcesPart.getInstance().bottomChange).val();
			
		var params = {
			renderMode: 'update',
			channel_widgets_id: AS4AllResourcesPart.getInstance().channelWidgetsId,
			id: this.channelController.channelId,
			keywords: keywords,
			month: month,
			year: year,
			category: category,
			language: language,
			page: page,
			perPage: perPage,
			pageUpdated: AS4AllResourcesPart.getInstance().perPageUpdated
		};

		//console.log(AS4Channel.getInstance());
		AS4Shell.getInstance().ajaxUpdate(url, params, $('all_resources_list_'+cwid), $A([this.onFilterComplete.bind(this)]), {showIndicator: false});
	},

	onFilterComplete: function()
	{
		AS4AllResourcesPart.getInstance().perPageUpdated = "false";
	},
	

	/**
	 * Perform a tag/title search for resources matching the given terms. Calls the
	 * onSearchComplete AJAX callback. Note that this method makes singleton calls to
	 * the current resource manager because the delay method does not bind its call to
	 * the correct context
	 *
	 * @param {String} terms Search terms to use
	 * @return void
	 */
	search: function(terms)
	{
		// If we have been triggered by a delayed 'quick search', check the value we are searching for
		// matches what is in the search box; otherwise, discard the search request.
		if ($('pagination_search_terms')) 
		{
			if (this.searchTerms == terms || (terms == null && $F('pagination_search_terms') != this.searchTerms) || this.searchTerms == this.previousTerms) 
				return;
		}
		else if(terms)
			this.searchTerms = terms;
			
		this.refresh(1);
	},

	// # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # 
	// AJAX Callbacks

	onRefreshComplete: function(transport, target)
	{

		// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
		// Reference the search terms that were used
		if(transport.headerJSON.meta.search_terms == this.searchTerms) 
		{
			this.previousTerms = transport.headerJSON.meta.search_terms;
			$('pagination_search_terms').value = this.previousTerms;
		}

	}
});


// # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # 
// Static Methods & Properties

// Static instance of the AS4Channel object
AS4AllResourcesPart.instance = null;

/**
 * Return a singleton instance of the AS4Channel object
 * @return AS4Channel
 */
AS4AllResourcesPart.getInstance = function()
{
	if(!AS4AllResourcesPart.instance)
		AS4AllResourcesPart.instance = new AS4AllResourcesPart();
		
	return AS4AllResourcesPart.instance;
}


