/**
 * The Syn.GamesChannelMyFavorite Component Class 
 */

/**
 * Create a Syn.GamesMyFavorite component instance 
 * @constructor
 */

Syn.GamesMyFavorite = Syn.Component.extend(
{
	lastPage: '',

 	/**
	 * Initialize the component class. This is called automatically by the default constructor.
	 * @member Syn.GamesMyFavorite
	 * @param {Object} config The configuration data structure 
	 */
	init: function(config)
	{		
		this._super(config);		
		this.uniqueElmt('games_my_favorite_pagination').find('a').connect('click', this, 'turnPage');
		this.uniqueElmt('games_my_favorite_list').find('.delete').connect('click', this, 'removeFavorite');
	},	 	

	/**
	 * AJAX Submit for passing pagination to the component
	 * @member Syn.GamesMyFavorite
	 * @param {Object} target is the handle of the object calling this function
	 * @param {Object} event is the event that was fired to call this object
	 */
	turnPage: function(target, event)
	{    
		this.submit({'page_id': $(target).attr('rel')});
	},
	
	/**
	 * AJAX Submit for passing remove user favorite to the component
	 * @member Syn.GamesMyFavorite
	 * @param {Object} target is the handle of the object calling this function
	 * @param {Object} event is the event that was fired to call this object
	 */
	removeFavorite: function(target, event)
	{     	
		var answer = confirm("Are you sure you want to delete this item?");
		if (answer)
		{
			$(target).parent().slideUp();
			this.lastPage = this.uniqueElmt('games_my_favorite_pagination').find('a:last').attr('rel');
			this.submit({'remove': $(target).attr('rel')});
		}
	},

        /**
         * Override the result function so that I can customize when
	 * the component is rerendered.
         * @param {mixed} res The content returned from the webservice
         */
        result: function (res)
        {
		if (this.uniqueElmt('games_my_favorite_list').find('.delete').length <= 1 && this.lastPage != '')
		{
			/**
			 * We have just deleted the last game on this page, and the lastPage
			 * was specified - go to it.
			 */
			var tmpVal = this.lastPage;
			this.lastPage = '';
			this.submit({'page_id': tmpVal});
		}
		else
		{
			this._super.apply(this, arguments);
		}
		return;
	}
});
