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

var AS4BlogPart = Class.create({

	/**
	 * @type {Element} The blog_part DOM element which this object controls
	element: 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 blog 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
		};
		AS4Shell.getInstance().ajaxUpdate(url, params, $$('.blog_cwid_'+this.channelWidgetsId).first().identify(), $A([this.onRefreshComplete.bind(this)]), {showIndicator: false});
	},

	/**
	 * filter by the search terms
	 *
	 */
	filter: function()
	{
		
		cwid = AS4BlogPart.getInstance().channelWidgetsId;
		var url = '/widget/index/';

		var month = $j("#filter_month_"+cwid).val();
			
		var params = {
			renderMode: 'update',
			channel_widgets_id: AS4BlogPart.getInstance().channelWidgetsId,
			id: this.channelController.channelId,
			month: month,
			page: 1,
			pageUpdated: AS4BlogPart.getInstance().perPageUpdated
		};

		//console.log(AS4Channel.getInstance());
		AS4Shell.getInstance().ajaxUpdate(url, params, $$('.widget_content.blog.blog_cwid_'+AS4BlogPart.getInstance().channelWidgetsId).first().identify(), $A([this.onFilterComplete.bind(this)]), {showIndicator: false});
	},

	onFilterComplete: function()
	{
		AS4BlogPart.getInstance().perPageUpdated = "false";
	},
		
	// # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # 
	// 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
AS4BlogPart.instance = null;

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


