if(typeof(Controls) == 'undefined')
	var Controls = {};
if(typeof(Controls.TemplateLoader) == 'undefined'){
	Controls.TemplateLoader = {};
	
	Controls.TemplateLoader = function(){
		this._templates = new Array();
		this._sentRequestsCount = 0;
		this._returnedRequestsCount = 0;
		this.OnTemplatesLoaded = null;
		this._startTime = null;
	}
	
	Controls.TemplateLoader.Template = function(fileName, methodName, fileGroup, containerID, onRenderCompleteHandler, onRenderHandler){
		this.FileName = (typeof(fileName) == "undefined") ? "" : fileName;
		this.MethodName = (typeof(methodName) == "undefined") ? "" : methodName;
		this.FileGroup = (typeof(fileGroup) == "undefined") ? "" : fileGroup;
		this.ContainerID = (typeof(containerID) == "undefined" || containerID == "") ? "" : containerID;
		this.OnRenderCompleteHandler = (typeof(onRenderCompleteHandler) == "undefined") ? null : onRenderCompleteHandler;
		this.OnRenderHandler = (typeof(onRenderHandler) == "undefined") ? null : onRenderHandler;
		this.CreateInstance = false;
		this.RequestType = "HTML";
		this.Arguments = new Array();
	};
	
	Controls.TemplateLoader.prototype.Add = function(template){
		this._templates.push(template);
	};
	
	Controls.TemplateLoader.prototype.Clear = function(){
		this._templates = new Array();
	}
	
	Controls.TemplateLoader.prototype.Load = function(){
		this._sentRequestsCount = 0;
		this._returnedRequestsCount = 0;
		var width = document.documentElement.clientWidth;
		var height = document.documentElement.clientHeight;
		this._startTime = new Date();
		
		for(var i = 0; i < 	this._templates.length; i++){
			var template = this._templates[i];
			var httpRequest = new CHttpRequest();
			httpRequest.QueryString = "AjaxType="+ template.RequestType +"&RequestFile="+ template.FileGroup +"."+ template.FileName +"&MethodName=" + template.MethodName;
			if(template.CreateInstance)
				httpRequest.QueryString += "&InstanceCall=1";
			httpRequest.AsyncCall = true;
			httpRequest.AddProperty("Arguments[ClientWidth]", width);
			httpRequest.AddProperty("Arguments[ClientHeight]", height);
			if(template.Arguments != null){
				for(var argName in template.Arguments){
					httpRequest.AddProperty("Arguments["+ argName +"]", template.Arguments[argName]);
				}
			}
			httpRequest.State = {Template:template, StartTime: new Date()};
			httpRequest.OnResponse = Controls.Delegates.CreateDelegate(this, this.Load_Callback);
			httpRequest.Send();
		}
	}
	
	Controls.TemplateLoader.prototype.Load_Callback = function(request, state){
		this._returnedRequestsCount++;
		
		if(state.Template.OnRenderHandler == null){
			if(state.Template.ContainerID != null){
				var container = document.getElementById(state.Template.ContainerID);
				if(container != null)
					Controls.Ajax.LoadContent(container, request.responseText);
			}
		}else{
			state.Template.OnRenderHandler(state.Template, {Request:request});
		}
		
		if(state.Template.OnRenderCompleteHandler != null){
			var diff = new Date(new Date() - state.StartTime);
			var milliseconds = diff.getMilliseconds() + diff.getSeconds() * 1000;
			var seconds = new Number(milliseconds / 1000).toFixed(4);
			state.Template.OnRenderCompleteHandler(state.Template, {Html:request.responseText, Xml:request.responseXML, ExecutionTime:new Number(seconds)});
		}
		
		if(this._returnedRequestsCount == this._templates.length){
			if(this.OnTemplatesLoaded != null){
				var diff = new Date(new Date() - state.StartTime);
				var milliseconds = diff.getMilliseconds() + diff.getSeconds() * 1000;
				var seconds = new Number(milliseconds / 1000).toFixed(4);
				this.OnTemplatesLoaded(this, {ExecutionTime: new Number(seconds)}); 
			}
			this.Clear();
		}
	}
}
