/**
 * @version $id $
 *
 * Flash games javascript class
 */

/**
 * Create a Syn.FlashGames component instance
 * @constructor
 */
Syn.FlashGames = Syn.Component.extend (
{
	/**
	 * Initialize the component class. This is called automatically by the default constructor.
	 * @member Syn.FlashGames
	 * @param {Object} config The configuration data structure
 	*/
	init: function(config)
	{
		this._super(config);
		this.game_index = config['game_index'];
		this.game_name  = config['game_name'];
		this.game_url   = config['game_url'];
		
		this.uniqueElmt('playitnow_next').connect('click',this,'submit',[ {game_index:this.game_index+1} ])
		                                 .connect('click',this,'showLoading');
		this.uniqueElmt('playitnow_prev').connect('click',this,'submit',[ {game_index:this.game_index-1} ])
		                                 .connect('click',this,'showLoading');

		this.uniqueElmt('playitnow_browse').connect('click',this,'toggleBrowseMenu' );
		this.uniqueElmt('playitnow_recent_menu').connect('click',this,'toggleRecentMenu' );

		for (var i=0; this.uniqueElmt('game'+i).length; i++)
		{
			this.uniqueElmt('game'+i).find('a').connect('click',this,'launchGame');
		}

		for (var i=0; this.uniqueElmt('game_list'+i).length; i++)
		{
			this.uniqueElmt('game_list'+i).find('a').connect('click',this,'launchGame');
		}

		for (var i=0; this.uniqueElmt('recent_game_list'+i).length; i++)
		{
			this.uniqueElmt('recent_game_list'+i).find('a').connect('click',this,'launchGame');
		}
		
	},

	/**
	 * Shows loading graphic when retrieving data
	 * @member Syn.FlashGames
	 */
	showLoading: function()
	{
		var ga = this.uniqueElmt('playitnow_gamearea');
		this.uniqueElmt('playitnow_loading').css({'width':ga.width(),'height':ga.height()}).toggle();
	},

	/**
	 * Launches the popup window with the flash game
	 * @member Syn.FlashGames
	 */
	launchGame: function (elmt,ev)
	{
		this.submit({'log_game_token':$(elmt).attr('rel')}, {norender:false});
		window.open($(elmt).attr('href'), 'gamewrapper', 'menubar=0,toolbar=0,location=0,status=0,scrollbars=0,resizable=1,directories=0,width=742,height=700');
	},

	/**
	 * Toggles Browse Games Menu
	 * @member Syn.FlashGames
	 */
	toggleBrowseMenu: function ()
	{
		this.uniqueElmt('playitnow_browse').toggleClass('playitnow_browse');
		this.uniqueElmt('playitnow_browse').toggleClass('playitnow_browse_on');

		this.uniqueElmt('playitnow_browse_menu').slideToggle();
		this.uniqueElmt('playitnow_carousel').toggle();
		this.uniqueElmt('playitnow_recent_menu').toggle();
	},
	
	/**
	 * Toggles Recently Played Menu
	 * @member Syn.FlashGames
	 */
	toggleRecentMenu: function ()
	{
		
		this.uniqueElmt('playitnow_recent').toggleClass('playitnow_recent');
		this.uniqueElmt('playitnow_recent').toggleClass('playitnow_recent_on');

		this.uniqueElmt('playitnow_recent_menu_list').slideToggle();
	}
});

