/**
 * AS4FeedPortalPanel 
 * Model object represents a single feed reader panel within the portal page
 */
 
var AS4FeedPortalPanel = Class.create(AS4PortalPanel, {
	
	/**
	 * @constructor
	 */
	initialize: function($super, title, options) {
		$super(title, options, 'feed');
	},
	
	/**
	 * AJAX refresh the channel portal page
	 */
	reload: function($super)
	{
		$super();
		
		var url = "/home/panel_content";
		
		var params = {
			renderMode: "update",
			update: true, 
			type: "feed",
			uri: this.options.uri, // feed URI (RSS2.0)
			items: this.options.items	// number of items to show
		};
		
		AS4Shell.getInstance().ajaxUpdate(url, params, this.contentElement(), $A([this.onReload.bind(this), this.onFeedLoaded.bind(this)]), {showIndicator: false});
	},
	
	
	// # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
	// User Interface Methods
	
	/**
	 * Display the next available invisible item
	 */
	incrementItem: function()
	{
		var elements = this.element.select('.feed_item');
		
		for(var i=0; i < elements.length; i++)
		{
			if (elements[i].style.display == 'none') 
			{
				elements[i].setStyle({
					display: 'block'
				});
				
				return;
			}
		}

		this.onChangeItemCount();
	},

	/**
	 * Hide the last visible item
	 */
	deductItem: function()
	{
		var elements = this.element.select('.feed_item');
		
		for(var i=elements.length - 1; i > 0; i--)
		{
			if (elements[i].style.display == 'block') 
			{
				elements[i].setStyle({
					display: 'none'
				});
				
				return;
			}
		}

		this.onChangeItemCount();
	},
	
	
	// # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
	// Virtual Accessor Methods
	
	/**
	 * Hook into the getControlStripHTML() method
	 *
	 * @param {String} uuid The UUID of the panel being generated, passed in by the controller class
	 * @return {String}
	 */
	getControlStripHTML: function($super, uuid)
	{
		return	'<a href="javascript:$(\'' + uuid + '\').controller.incrementItem()" id="' + uuid + '_increment" class="portal_add" title="Display another item from this feed"><span /></a> ' +
				'<a href="javascript:$(\'' + uuid + '\').controller.deductItem()" id="' + uuid + '_deduct" class="portal_remove" title="Hide the last item from this feed"><span /></a> ' +
				$super(uuid);
	},
	
	// # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
	// AJAX Callback Methods
	
	onChangeItemCount: function()
	{},

	onFeedLoaded: function(transport, target)
	{
		if (transport.headerJSON) 
		{
			if(transport.headerJSON.meta.title.length < 15)
				this.setTitle(transport.headerJSON.meta.title);
			else
				this.setTitle(transport.headerJSON.meta.title.substr(0, 15) + '...');
		}
	}
	
});
