var DEVXLoader=function (data) {
	this.data=data;
	this.data["name"]="DEVXLoader";
	this.queue=Array();
	this.groups_select=Array();
	this.objects={};
	this.tpl={};
	this.timer=null;
	this.preloadTplStatus=false;
	var jscripts={"CHttpRequest":"cajax.js", "ToolsLoaded":"tools.js"};
	this.data["tplUrls"]={
		"loading":"loading.html"
	};
	if (typeof(this.data["tplPath"])=="undefined")
	{
		this.data["tplPath"]=data["path"];
	}
	var s=this;
	var point_out=function () {s.init()};
	this.preloadData(this.data, jscripts, point_out);
};

DEVXLoader.prototype.preloadData = function (data, jscripts, point_out) {
	if (jscripts!=null)
	{
		for (var i in jscripts)
		{
			if (typeof(self[i])=="undefined")
			{
				if ((typeof(data['loading_script'])=='undefined') || (typeof(data['loading_script'][i])=='undefined')) {
					var script=document.createElement("SCRIPT");
					script.src=data["path"]+jscripts[i];
					document.body.insertBefore(script, document.body.childNodes[0]);
					if (typeof(data['loading_script'])=='undefined') data['loading_script']={};
					data['loading_script'][i]=true;
				}
				var s=this;
				this.timer=setTimeout(function () {s.preloadData(data, jscripts, point_out)}, 10);
				return;
			}
		}
	}

	var style=document.createElement("LINK");
	style.rel="stylesheet";
	if (typeof(data["tplPath"])=="undefined")
	{
		data["tplPath"]=data["path"];
	}
	if (this.detectBrowser()=="IE") {
		style.href=data["tplPath"]+'templates/css/style_IE.css';
	} else {
		style.href=data["tplPath"]+'templates/css/style_FF.css';
	}
	document.body.insertBefore(style, document.body.childNodes[0]);
	var s=this;
	if (!this.preloadTplStatus)
	{
		this.preloadTemplates(data, point_out);
	}
};

DEVXLoader.prototype.init=function () {
	if (typeof(this.data["objects"])!="undefined") {
		var s=this;
		for (var i in this.data["objects"])
		{
			var data={"path":this.data["objects"][i], "name":i};
			if (this.data["objects_tplPath"] && this.data["objects_tplPath"][i]) {
				data["tplPath"]=this.data["objects_tplPath"][i];
			}
			var core={};
			if (typeof(self[i])=="undefined")
			{
				core[i]="core.js";
				this.preloadData(data, core, function () {s.init()});
				return;
			}
			else
			{
				self[i].prototype.loader=this;
				if (!self[i].prototype.loaded) {
					self[i].prototype.data=data;
					core=self[i].prototype.jscripts;
					data["tplUrls"]=self[i].prototype.templates;
					self[i].prototype.loaded=true;
					this.preloadData(data, core, function () {s.init()});
					return;
				}
			}
		}
	}
	if (this.data["onload"]!=null) {
		this.runOnload(this.data["onload"]);
	}
};

DEVXLoader.prototype.runOnload= function (onload) {
	try {
		onload();
	} catch (e) {
		if (this.data['wait_onload']) {
			var s=this;
			setTimeout(function() {s.runOnload(onload)}, 100);
		}
	}
};


DEVXLoader.prototype.detectBrowser= function () {
	if (navigator.userAgent.toLowerCase().indexOf('opera')>=0) {
		return "OP";
	} else if ( navigator.userAgent.indexOf('MSIE')>=0 ) {
		return "IE";
	} else if (navigator.userAgent.indexOf("Firefox")) {
		return "FF";
	} else {
		return "IE";
	}
};

DEVXLoader.prototype.processGET=function (url, answer, queue, cach) {
	if (this.preProcess(url, null, answer, queue, cach)) return;
	this.showLoading();
	var s=this;
	if (answer==null) {
		answer_new=function (http) {s.hideLoading(); return s.processAnswer(http);};
	}
	else
	{
		answer_new=function (http) {s.hideLoading(); return answer(http);};
	}
	this.ajax=new CHttpRequest(answer_new, this.data["onerror"]);
	if (!cach) url=checkUrl(url)+"ajax=Y&tmpTime="+getTmpTime();
	this.ajax.processGET(url);
	return false;
};

DEVXLoader.prototype.processPOST=function (url, vars, answer, queue, cach) {
	if (this.preProcess(url, vars, answer, queue, cach)) return;
	this.showLoading();
	var s=this;
	if (answer==null) {
		answer_new=function (http) {s.hideLoading(); return s.processAnswer(http);};
	}
	else
	{
		answer_new=function (http) {s.hideLoading(); return answer(http);};
	}
	this.ajax=new CHttpRequest(answer_new, this.data["onerror"]);
	if (!cach) url=checkUrl(url)+"ajax=Y&tmpTime="+getTmpTime();
	this.ajax.processPOST(url,vars);
	return false;
};

DEVXLoader.prototype.preProcess=function (url, vars, answer, queue, cach) {
	if (queue)
	{
		if (this.loading)
		{
			this.queue[this.queue.length]={"url":url, "vars":vars, "answer":answer, "cach":cach, "method":"GET"};
			return true;
		} else {
			this.loading=true;
		}
	}
	else
	{
		if (this.loading) return true;
	}
	return false;
};

DEVXLoader.prototype.processNext=function () {
	this.loading=false;
	if (this.queue.length > 0) {
		var next=this.queue.shift();
		if (next["method"]=="POST")	{
			this.processPOST(next["url"], next["vars"], next["answer"], false, next["cach"]);
		} else {
			this.processGET(next["url"], next["answer"], false, next["cach"]);
		}
	}
};

DEVXLoader.prototype.processAnswer=function (http) {
	var i;
	var xml=http.responseXML;
	var error=xml.getElementsByTagName("error");
	if (error.length>0) {
		for (var i=0; i<error.length; i++) {
			this.show_text(error[i].getAttribute('id'), error[i].childNodes.item(0).data, false);
		}
		this.processNext();
		return;
	}
	this.xmldata=xml.getElementsByTagName("data");
	if ((this.xmldata.length!=null) && (this.xmldata.length>0)) {
		this.xmldata=this.xmldata[0].childNodes.item(0).data;
	} else {
		this.xmldata="";
	}
	var jscode=xml.getElementsByTagName("jscode");
	if (jscode.length>0) {
		eval(jscode[0].childNodes.item(0).data);
	}
	this.xmldata=null;
	this.processNext();
};

DEVXLoader.prototype.preloadTemplates=function (data, return_method) {
	this.preloadTplStatus=true;
	var s=this;
	if (typeof(data["tplUrls"])!="undefined") {
		for(var i in data["tplUrls"]) {
			if (typeof(this.tpl[data["name"]+i])=="undefined") {
				this.loadTemplates(data, i, function () {s.preloadTemplates(data, return_method)});
				return ;
			}
		}
	}
	this.preloadTplStatus=false;
	//clearTimeout(this.timer);
	if (return_method!=null) {
		return_method();
	}
};

DEVXLoader.prototype.loadTemplates=function (data, name, return_method) {
	if (typeof(this.tpl[data["name"]+name])=="undefined")
	{
		if (typeof(data["tplPath"])=="undefined")
		{
			data["tplPath"]=data["path"];
		}
		var s=this;
		var url=data["tplUrls"][name];
		this.processGET(data["tplPath"]+"templates/"+url, function (http) {s.saveTemplates(http, name, data); if (typeof(return_method)!="undefined") {return_method();} }, true, true);
	}
};

DEVXLoader.prototype.saveTemplates=function (http, name, data) {
	this.tpl[data["name"]+name]=http.responseText;
	str=/_TPLPATH_/g;
	this.tpl[data["name"]+name]=this.tpl[data["name"]+name].replace(str, data["tplPath"]);
	this.processNext();
};

DEVXLoader.prototype.showTemplate=function (data, name, id, return_method) {
	if (typeof(this.tpl[data["name"]+name])=="undefined")
	{
		var http, s=this;
		this.loadTemplates(data, name, function () {s.showTemplate(data, name, id, return_method);});
//		var self=this;
//		setTimeout(function () {self.showTemplate(data, name, id)}, 10);
	}
	else
	{
		var o=document.getElementById(id);
		if (o!=null)
		{
			o.innerHTML=this.tpl[data["name"]+name];
		}
		if (typeof(return_method)!="undefined")
		{
			return_method();
		}
	}
};

DEVXLoader.prototype.hideLoading=function () {
	if (this.data["loadingbar"]!=null) {
		var loadingbar=this.data["loadingbar"];
		loadingbar(false);
	} else {
		var o=getObj("DEVXPhotoEditor_loading_show");
		if (o!=null) {
			document.body.removeChild(o);
			getObj('shadow_loader').parentNode.removeChild(getObj('shadow_loader'));
		}
	}

};

DEVXLoader.prototype.showLoading=function () {
	if (this.data["loadingbar"]!=null) {
		var loadingbar=this.data["loadingbar"];
		loadingbar(true);
	} else {
		var tpl=this.tpl[this.data["name"]+"loading"];
		if (typeof(tpl)!="undefined") {
			var shadow=document.createElement("DIV");
			shadow.id="shadow_loader";
			shadow.className="shadow";
			shadow.style.position="absolute";
			shadow.style.zIndex="10";
			var wSize=getWindowSize();
			shadow.style.height=wSize.h+"px";
			shadow.style.width=wSize.w+"px";

			document.body.appendChild(shadow);

			var div=document.createElement("DIV");
			div.id="DEVXPhotoEditor_loading_show";
			div.className="DEVXPhotoEditor_loading_show";
			div.innerHTML=tpl;
			div.style.zIndex=1100;
			div.style.position="absolute";
			document.body.insertBefore(div, document.body.childNodes[0]);
			centerObjectInScreen(getObj("DEVXPhotoEditor_loading_show"));
		}
	}
};

DEVXLoader.prototype.show_text=function (id, text, empty) {
	try	{
		var o=document.getElementById(id);
		if ( (empty) || (!empty && (text>"")) ) {
			o.innerHTML=text;
		}
	}
	catch (e) {}
};
