var Worker = Class.create();

Worker.prototype = {
	initialize: function(url,container)
	{
		this.url = url;
		this.container = $(container);
	},
	
	get: function(params,is_rebuild)
	{
		this.is_rebuild = is_rebuild;		
		this.remove_childs(this.container);		

		new Ajax.Request(this.url,{
										method: "get",						
										parameters: params,
										onLoading: this.loading.bind(this),
										onComplete: this.build.bind(this),
										//onSuccess : this.build.bind(this),
										onException: this.exception.bind(this)
									});
		
	},
	
	loading: function()
	{	
		var img = document.createElement("img");
		img.setAttribute("src","/template/images/ajax/loading_small.gif");
		img.setAttribute("align","bottom");		
		var h1 = document.createElement("H1");		
		h1.appendChild(img);
		h1.appendChild(document.createTextNode(" Загрузка..."));		
		h1.setStyle({"padding":"25%"});
		h1.setAttribute("align","center");
		h1.setAttribute("valign","middle");
		
		this.message = h1;
		this.container.appendChild(this.message);
	},	
	
	build: function(response)
	{	
		this.remove_childs(this.container);
		
		if(this.is_rebuild)
		{
			var root = response.responseXML.documentElement;
			for(var i = 0; i < root.childNodes.length; i++)				
				this.container.appendChild(this.rebuild(root.childNodes[i]));			
		}
		else					
			this.container.innerHTML = response.responseText;				
	},
	
	rebuild: function(obj)
	{
		var newObj;

		if(obj.nodeType == 3)
			newObj = document.createTextNode(obj.textContent);
		else
		{
			newObj = document.createElement(obj.nodeName);
			if(obj.hasAttributes())
				newObj = this.set_attributes(newObj,obj.attributes);
			
			if(obj.hasChildNodes)
			{
				var childs = obj.childNodes;
				for(var i = 0; i < childs.length; i++)
					newObj.appendChild(this.rebuild(childs[i]));			
			}
		}
		return newObj;
	},
	
	set_attributes: function(obj,attributes)
	{
		for(var i = 0; i < attributes.length; i++)
			obj.setAttribute(attributes[i].name,attributes[i].value);

		return obj;
	},
	
	remove_childs: function(element)
	{	
		while(element.hasChildNodes())
			element.removeChild(element.firstChild);		
	},
	
	exception: function(requester,exception)
	{
		alert(requester + ":" + exception);
	}
}
