
/**
 * Класс для взаимодействия клиента и сервера
 * -----------------------------------------------
 * @copyright TimeZero <http://www.timezero.ru/>
 * @autor Dmitriy Shkinev <berkel@timezero.ru>
 * @version 1.0 2008-6-5
 */

var Destiny = {

	init : function (id)
	{
		if (!id)
		{
			return false;
		}

		var flash = $(id);

		if (!flash)
		{
			return false;
		}

		this.ToClient.flash = flash;

		return true;
	},

	ToServer : {

		auth : function (session, login, charcter)
		{
			if ((!charcter && !login) || !session)
			{
				return false;
			}
			
			var args = 'login=' + login + '&session=' + session + '&charcter=' + charcter + '&r=' + Common.getRandom(1, 1000000);
			var content = Common.AJAX.getContent('POST', '/account/authorization/', args);
			var response = Common.JSON.decode(content);
			
			if (response.status == "success") {
				Common.location("");
			}		
			return true;
		},

		onCheckRequirements : function (result)
		{
			if (!result)
			{
				alert('Минимальные требования: error');

				return false;
			}

			alert('Минимальные требования: ok');

			return true;
		}
	},

	ToClient : {

		flash : null,

		auth : function (session, login)
		{
			if (!login || !session || !this.flash)
			{
				return false;
			}

			try
			{
				return this.flash.auth(session, login);
			}
			catch (e)
			{
			}

			return false;
		},

		checkRequirements : function (socketHost, socketPort, proxyUrl)
		{
			if (!socketHost)
			{
				return false;
			}

			if (!socketPort)
			{
				return false;
			}

			if (!proxyUrl)
			{
				return false;
			}

			if (!this.flash)
			{
				return false;
			}

			try
			{
				return this.flash.checkRequirements(socketHost, socketPort, proxyUrl);
			}
			catch (e)
			{
//				alert(e);
			}

			return false;
		},

		getAuth : function ()
		{
			if (!this.flash)
			{
				return false;
			}

			try
			{
				return this.flash.getAuth();
			}
			catch (e)
			{
//				alert(e);
			}

			return false;
		},

		isLogined : function ()
		{
			if (!this.flash)
			{
				return false;
			}

			try
			{
				return this.flash.isLogined();
			}
			catch (e)
			{
//				alert(e);
			}

			return false;
		},

		isOpened : function ()
		{
			if (!this.flash)
			{
				return false;
			}

			try
			{
				return this.flash.isOpened();
			}
			catch (e)
			{
//				alert(e);
			}

			return false;
		}
	}
};

function auth(session, login, charcter)
{
/*	if (!cLogin) 
	{
		return Destiny.ToServer.auth(session, login, charcter);
	}*/
}

function getAuth()
{
/*	if (cLogin && cCert && !Destiny.ToClient.isLogined())
	{
		Destiny.ToClient.auth(cCert, cLogin);
	}*/
}

function initialize()
{
	Destiny.init(sClientFlashId);

//	alert('Destiny.ToClient.checkRequirements(): ' + Destiny.ToClient.checkRequirements());
//	alert('Destiny.ToClient.isOpened(): ' + Destiny.ToClient.isOpened());
//	alert('Destiny.ToClient.isLogined(): ' + Destiny.ToClient.isLogined());
//	alert('Destiny.ToClient.getAuth(): ' + Destiny.ToClient.getAuth());
}

function onCheckRequirements(result)
{
	return Destiny.ToServer.onCheckRequirements(result);
}

Common.Event.add(window, 'load', initialize);