/* DataUpdater class. Copyright by ADenis (adeniss@ukr.net) */

function DataUpdater(){	
	
	var preloader = null;
	
	this.sessions = new Array();
	this.debug = false;
	this.onError = null;

	this.addObject = function(_class, template, xsl, form, objectId, callbackObject, callbackFunction, callbackAttributes, useCache, debug){
		this.debug = debug;
		try{
			if(!preloader){
				preloader = new PreLoader();
			}
			if(this.sessions[objectId]){
				this.sessions[objectId].init(_class, template, xsl, form, objectId, callbackObject, callbackFunction, preloader, callbackAttributes, useCache);
			}else{
				this.sessions[objectId] = new DataUpdateObject(_class, template, xsl, form, objectId, callbackObject, callbackFunction, preloader, callbackAttributes, useCache);
			}
			
			return false;
		}catch(e){
			alert("Error. The system can't update data.\nPlease, make sure your system is compliant with minimum system requirements");
			return true;
		}
	}
	
	this.update = function(objectId){
		this.sessions[objectId].update();
	}

	this.updateObject = function(_class, template, xsl, form, objectId, callbackObject, callbackFunction, callbackAttributes, useCache){
		//return true;
		
		if(objectId && !$(objectId)){
			return true;
		}
		
		this.addObject(_class, template, xsl, form, objectId, callbackObject, callbackFunction, callbackAttributes, useCache);
		this.update(objectId);
	
		//return true;
		//alert("OK!");
		return false;
	}

	this.updateByNodeData = function(node){
		var _class = false;
		var template = false;
		var xsl = false;
		var form = false;
		var objectId = false;
		var callbackObject = false;
		var callbackFunction = false;
		var callbackAttributes = false;
		var useCache = false;
		
		if(!node || !node.tagName)
			return true;
		
		_class = getNodeAttribute(node, "_class", _class);
		template = getNodeAttribute(node, "_template", template);
		xsl = getNodeAttribute(node, "_xsl", xsl);
		objectId = getNodeAttribute(node, "_objectId", objectId);
		callbackFunction = new Function(getNodeAttribute(node, "onData", callbackFunction));		
		useCache = getNodeAttribute(node, "_useCache", useCache);
		
		if(node.tagName == "FORM"){
			form = node;
		}else if(getNodeAttribute(node, "href", false)){
			form = getNodeAttribute(node, "href", false);
		}
		
		return this.updateObject(_class, template, xsl, form, objectId, callbackObject, callbackFunction, callbackAttributes, useCache);
	}
	
	this.removeLoader = function(object) {
		preloader.removeLoader(object);
	}
}

function DataUpdateObject(_class, template, xsl, form, objectId, callbackObject, callbackFunction, preloader, callbackAttributes, useCache){
	var preloader = preloader;
	this.xml = new XML();
	
	this._class = "";
	this.template = "";
	this.xsl = "";
	this.form = null;
	this.objectId = "";
	this.callbackObject = null;
	this.callbackFunction = null;
	this.callbackAttributes = null;
	
	this.useCache = false;
	this.cacheURL = "";
	this.cacheXML = new Array();
	this.cacheText = new Array();
	this.cacheStatus = new Array();
	
	this.init = function(_class, template, xsl, form, objectId, callbackObject, callbackFunction, preloader, callbackAttributes, useCache){
		this._class = _class;
		this.template = template;
		this.xsl = xsl;
		this.form = form;
		this.objectId = objectId;
		this.callbackObject = callbackObject;
		this.callbackFunction = callbackFunction;
		this.callbackAttributes = callbackAttributes;
		this.useCache = useCache;
	}
	
	this.init(_class, template, xsl, form, objectId, callbackObject, callbackFunction, preloader, callbackAttributes, useCache);
	
	this.update = function(){
		var url = "http://"+document.location.host+"/index.php?";
		
		if(this._class)
			url += ("class=" + this._class + "&");
		if(this.template)
			url += ("template=" + this.template + "&");
		if(this.xsl)
			url += ("xsl=" + this.xsl + "&");
		if(this.form){			
			if(this.form.tagName){
				url += formToRequest(this.form);
			}else if(typeof(this.form) == "string"){
				if(this.form.length){
					var page = document.location.pathname;
					
					if(this.form.indexOf(page) > -1 || this.form.indexOf(".html") < 0 || page.indexOf(this.form) > -1){
						this.form = this.form.substr(this.form.indexOf("?")+1, this.form.length);
						url += this.form;
					}else{
						window.location.href = this.form;
						return;
					}
				}
			}
		}
		
		if(this.debug)
			alert("USE CACHE: "+this.useCache + "\n" +"CACHE URL: "+ this.cacheURL + "\n" + "URL: "+ url+ "\n"+this.cacheText[url]);
		
		this.cacheURL = url;
		
		if(this.useCache && this.cacheText[url]){
			//alert("USE CACHE!");
			this.onUpdate(this.cacheXML[url], this.cacheText[url], this.cacheStatus[url]);			
			return;
		}
		
		//alert(url);
		
		if(this.debug){
			alert(url);
			//window.open(url+"&xml");
		}
				
		var object = document.getElementById(this.objectId);
		
		if(object){
			preloader.addLoader(object);
		}
		
		this.xml.load(url);
	}

	this.onUpdate = function(xml, text, status){
		this.debug = false;
		
		var object = $(this.objectId);
		
		this.cacheXML[this.cacheURL] = xml;
		this.cacheText[this.cacheURL] = text;
		this.cacheStatus[this.cacheURL] = status;
		
		//alert(text);
		if(this.debug){
			showInDebugWindow(text);
		}
		
		if(object){
			object.innerHTML = text;
			preloader.removeLoader(object);
		}

		if(this.callbackFunction){
			if(typeof(this.callbackFunction) == "string")
				this.callbackFunction = new Function(this.callbackFunction);
			
			if(this.callbackObject)
				this.callbackFunction.call(this.callbackObject, this.callbackAttributes);
			else
				this.callbackFunction(this.callbackAttributes);			
		}
	}
	
	this.xml.listener = this;
	this.xml.onLoad = this.onUpdate;
	this.xml.onError = getDataUpdater().onError;
}

var dataUpdater = new DataUpdater();

function getDataUpdater() {
	if (!dataUpdater) {
		dataUpdater = new DataUpdater();
	}
	
	return dataUpdater;
}