/*!
 * avangard/battle_controller.js
 * © 2009 avangardonline.ru
 * @author BlooDHounD <http://www.blooddy.by>
 */

if ( !window.blooddy ) throw new Error( '"blooddy" not initialized' );

blooddy.require( 'avangard' );

if ( !avangard.BattleController ) {

	blooddy.require( 'blooddy.events.EventDispatcher' );
	blooddy.require( 'blooddy.utils' );

	/**
	 * класс работы с чатом
	 * @class
	 * @namespace	avangard
	 * @extends		blooddy.events.EventDispatcher
	 * @author		BlooDHounD	<http://www.blooddy.by>
	 */
	avangard.BattleController = ( function() {

		//--------------------------------------------------------------------------
		//
		//  Class variables
		//
		//--------------------------------------------------------------------------

		/**
		 * @private
		 */
		var	win =		window,

			b =			blooddy,
			utils =		b.utils;

		//--------------------------------------------------------------------------
		//
		//  Constructor
		//
		//--------------------------------------------------------------------------

		/**
		 * @constructor
		 * @param	{blooddy.Flash.ExternalFlash}	connection
		 * @param	{Number}						type
		 */
		var BattleController = function(connection, type, redirectURI) {
			BattleController.superPrototype.constructor.call( this );
			this._connection = connection;
			this._type = type;
			this.redirectURI = redirectURI;
			// инитиализируем
			if ( connection.isInitialized() ) {
				initHandler.call( this );
			} else {
				connection.addEventListener( 'init', this, initHandler );
			}
		};

		b.extend( BattleController, b.events.EventDispatcher );

		var BattleControllerPrototype = BattleController.prototype;

		//--------------------------------------------------------------------------
		//
		//  Variables
		//
		//--------------------------------------------------------------------------

		/**
		 * @private
		 * @type	{Number}
		 */
		BattleControllerPrototype._type = 0;

		/**
		 * @private
		 * @type	{blooddy.Flash.ExternalFlash}
		 */
		BattleControllerPrototype._connection = null;

		//--------------------------------------------------------------------------
		//
		//  Properties
		//
		//--------------------------------------------------------------------------

		/**
		 * @private
		 * @type	{Number}
		 */
		BattleControllerPrototype.redirectURI = null;

		//--------------------------------------------------------------------------
		//
		//  Event handlers
		//
		//--------------------------------------------------------------------------

		/**
		 * @private
		 * флэшка проинитиализировалась
		 */
		var initHandler = function() {
			// удаляем слушателя инитиализации
			this._connection.removeEventListener( 'init', this, initHandler );
			// вешаем хэнлеры на методы
			if ( this._type != 1 ) {
				this._connection.closeBattle =	utils.createDeferredDelegate( this, this.closeBattle );
			}
		};

		//--------------------------------------------------------------------------
		//
		//  Methods
		//
		//--------------------------------------------------------------------------

		/**
		 */
		BattleControllerPrototype.closeBattle = function() {
			if ( this.redirectURI ) {
				win.location = this.redirectURI;
			} else {
				this._connection.dispose();
			}
		};

		/**
		 * @return	{String}
		 */
		BattleControllerPrototype.toString = function() {
			return '[BattleController object]';
		};

		//--------------------------------------------------------------------------
		//
		//  Class methods
		//
		//--------------------------------------------------------------------------

		/**
		 * @static
		 * @method
		 * @return	{String}
		 */
		BattleController.toString = function() {
			return '[class BattleController]';
		};

		return BattleController;

	}() );

}
