Mys.Renderer = function(){};

Mys.Renderer.prototype =
{
    render : function(el, response, updateManager, callback)
    {
        //el.update(response.responseText, updateManager.loadScripts, callback);
    }
};

Mys.AsyncHttpRequest = function()
{
	this.init();
	this.requestPool = {};
	this.formRequestPool = {};
};


Mys.AsyncHttpRequest.prototype =
{
	serverURL:'/api/index.php'
	,el:null
	,um:null
	,requestPool : null
	,scope:null
	,skipError : false
	,formRequestPool : null
	,formTID : 0
	
	,init : function()
	{
		this.el = document.createElement("DIV"); // 나중에 이거 만들기
		
		this.um = new Ext.UpdateManager(this.el);
		this.um.indicatorText = '<div id="mys-loading" class="loading-indicator"></div>';
		
		this.um.on("update", this.onLoad, this);
		this.um.on("failure", this.onLoadException, this);
		this.um.setRenderer(new Mys.Renderer);
	}
	,notting : function ()
	{
	}
	,setStateElement : function (elState)
	{
	    this.um.el = Ext.get(elState);
    }

	,request : function(param, callback, scope, internalCallback, intScope, passData)
	{
		this.um.update(this.serverURL, param, this.onCallback.createDelegate(this));

		//
		this.requestPool[this.um.transaction.tId] =
		{
			scope : scope,
			passData : passData,
			callback : callback,
			intScope : intScope,
			internalCallback : internalCallback
		}
	}
	
	,open : function(url, param, callback, scope, internalCallback, intScope, passData)
	{
		this.um.update(url, param, this.onCallback.createDelegate(this));

		//
		this.requestPool[this.um.transaction.tId] =
		{
			scope : scope,
			passData : passData,
			callback : callback,
			intScope : intScope,
			internalCallback : internalCallback,
			justOpen : true
		}
	}
	
	,formRequest : function(form, reset, callback, scope) {
		this.um.formUpdate(form, this.serverURL, reset, this.onFormCallback.createDelegate(this));
		this.formRequestPool = {
			scope : scope,
			callback : callback
		}
	}
	,onFormCallback : function(el, bSuccess, response) {
		var tr = this.formRequestPool;
		this.ftr = tr;
		
		if (!bSuccess)
			return;
		var data;
		try
		{
			data = Ext.util.JSON.decode(response.responseText);
			
			if(!data || data.response.code != 100 && !this.skipError)
			{
				if(data && data.response)
					alert(data.response.message);
				//return;
			}
		}
		catch(e){
			window.errorResponse = response;
			alert("내부오류 : 서버로부터 받은 데이타를 해석 할 수 없습니다.");
			return;
		}

		if (tr.internalCallback)
		{
			var result = tr.internalCallback.call(tr.intScope, data, tr.passData, tr.callback, tr.scope);
			if (tr.callback && result !== true)
			{
				tr.callback.call(tr.scope, data, tr.passData);
			}
		}
		else
		{
			if (tr.callback)
				tr.callback.call(tr.scope, data);
		}
	}
	,onCallback : function(el, bSuccess, response)
	{
		var tr = this.requestPool[response.tId];
		this.tr = tr;

		if (!bSuccess)
			return;

		var data;
		try
		{
			data = Ext.util.JSON.decode(response.responseText);
			
			if(!data || data.response.code != 100 && !this.skipError)
			{
					
/*				var str = "";
				data.errors.error = typeof data.errors.error.length == "number" ? data.errors.error : [data.errors.error];

				for(var i = 0; i < data.errors.error.length; i++)
				{
					if(data.errors.error[i].level == "user")
						str += data.errors.error[i].message + "\n";
				}

				if(str)*/
				if(data && data.response && data.response.code % 1000 >= 400)
					alert(data.response.message);

				//return;
			}
		}
		catch(e)
		{
			alert("내부오류 : 서버로부터 받은 데이타를 해석 할 수 없습니다.");
			return;
		}


		if (tr.internalCallback)
		{
			var result = tr.internalCallback.call(tr.intScope, data, tr.passData, tr.callback, tr.scope);
			if (tr.callback && result !== true)
			{
				tr.callback.call(tr.scope, data, tr.passData);
			}
		}
		else
		{
			if (tr.callback)
				tr.callback.call(tr.scope, data, tr.passData);
		}
	}
	,callback : function ()
	{
		if (this.tr.callback)
			this.tr.callback.apply(this.tr.scope, arguments);
	}

	,onLoad : function()
	{
	    this.um.el.update("");
	}

	,onLoadException : function()
	{
		alert("서버에 연결할 수 없습니다.");
	}
}
