/**
 * GamesArticles script
 */

/**
 * Create a Syn.GamesArticles component instance
 * @constructor
 */
Syn.GamesArticles = Syn.Component.extend (
{
	/**
	 * Persistent properties for how to display results
	 */
	page_index:  1,
	browse_num:  15,
	platform_id: 0,
	page_count:  1,
	sort_field:  'publish_date',
	sort_order:  0,

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

		this.page_index  = config.page_index;
		this.browse_num  = config.browse_num;
		this.platform_id = config.platform_id;
		this.page_count  = config.page_count;
		this.sort_field  = config.sort_field;
		this.sort_order  = config.sort_order;
		this.game_id     = config.game_id;

		$( '#'+this.uniqueKey('next_page_bot') )
			.connect('click',this,'submit',[{page_index:this.page_index+1,browse_num:this.browse_num,platform_id:this.platform_id,sort_field:this.sort_field,sort_order:this.sort_order}])
			.connect('click',this,'showLoading');

		$( '#'+this.uniqueKey('prev_page_bot') )
			.connect('click',this,'submit',[{page_index:this.page_index-1,browse_num:this.browse_num,platform_id:this.platform_id,sort_field:this.sort_field,sort_order:this.sort_order}])
			.connect('click',this,'showLoading');

		// Bind event for 15/25/50 per page links
		$( '#'+this.uniqueKey('15_top')+' a,#'+this.uniqueKey('15_bot') + ' a' )
			.connect('click',this,'submit',[ {'page_index': 0,'browse_num': 15,'platform_id': this.platform_id,'sort_field': this.sort_field,'sort_order': this.sort_order, 'game_id': this.game_id}])
			.connect('click',this,'showLoading')
			.connect('mouseover mouseout', this, 'toggleOnState');
		$( '#'+this.uniqueKey('25_top')+' a,#'+this.uniqueKey('25_bot') + ' a' )
			.connect('click',this,'submit',[ {'page_index': 0,'browse_num': 25,'platform_id': this.platform_id,'sort_field': this.sort_field,'sort_order': this.sort_order, 'game_id': this.game_id}])
			.connect('click',this,'showLoading')
			.connect('mouseover mouseout', this, 'toggleOnState');
		$( '#'+this.uniqueKey('50_top')+' a,#'+this.uniqueKey('50_bot') + ' a' )
			.connect('click',this,'submit',[ {'page_index': 0,'browse_num': 50,'platform_id': this.platform_id,'sort_field': this.sort_field,'sort_order': this.sort_order, 'game_id': this.game_id}])
			.connect('click',this,'showLoading')
			.connect('mouseover mouseout', this, 'toggleOnState');

		// Bind event for platform dropdown
		this.uniqueElmt('platform_dropdown')
			.connect('change',this,'showLoading')
			.connect('change', this, 'setPlatform');

		$('#'+this.uniqueKey('bot_page')+ ' li:not(:first):not(:last) a')
				.connect('click',this,'setPage')
				.connect('click',this,'showLoading')
				.connect('mouseover mouseout', this, 'toggleOnState');

		// Bind event for column headers
		this.uniqueElmt('results_table').find('tr th a')
			.connect('click',this,'showLoading')
			.connect('click', this, 'setSort');
	},

	/**
	 * Toggle the "on" class for elements.
	 * @member Syn.GamesArticles
	 */
	toggleOnState: function(elmt, ev)
	{
		$(elmt).parent().toggleClass('on');
	},

	/**
	 * Shows loading graphic when retrieving data
	 * @member Syn.GamesArticles
	 */
	showLoading: function()
	{
		this.uniqueElmt('loading').show().css( 
			{ 'width'  : this.container().width(),
			  'height' : this.container().height() });
	},

	/**
	 * Set the sort options
	 * @member Syn.GamesArticles
	 */
	setSort: function(target, event)
	{
		if (this.sort_field == $(target).attr('rel'))
		{
			this.sort_order = (this.sort_order == 1) ? 0 : 1;
		}
		else
		{
			this.sort_field = $(target).attr('rel');
			this.sort_order = 1; // Ascending
		}

		this.refreshResults();
	},

	/**
	 * Set the page number 
	 * @member Syn.GamesArticles
	 */
	setPage: function(target, event)
	{
		this.submit({'page_index': $(target).attr('rel'),'browse_num': this.browse_num,'platform_id': this.platform_id,'sort_field': this.sort_field,'sort_order': this.sort_order});
	},

	/**
	 * Set the platform id
	 * @member Syn.GamesArticles
	 */
	setPlatform: function(target, event)
	{
		if (this.platform_id == $(target).val())
		{
			return;
		}

		this.platform_id = $(target).val();

		// Extract just the file path and not query parameters
		var path = window.location.pathname;
		
		if (path.indexOf('?') != -1)
		{
			path = path.substr(0, path.indexOf('?'));
		}
		window.location = path + '?games_platform_id=' + this.platform_id;
	},

	/**
	 * Refresh article results through AJAXification
	 * @member Syn.GamesArticles
	 */
	refreshResults: function()
	{
		this.submit({
			'page_index':  this.page_index,
			'browse_num':  this.browse_num,
			'platform_id': this.platform_id,
			'sort_field':  this.sort_field,
			'sort_order':  this.sort_order
		});
	}
});

