/**
* @desc отсутствует
* @version 0.1
*
* @author
* 	Артур Гришин <arthur.grishin@me.com>
*
* @copyright © 2008 Артур Гришин
*/
var XMLtoDOM = new Class({
	
	initialize: function(options){
    	this.options = options;
     	
    	return this.getXML();
  	},

   	getXML : function() {
   		
     	var xml = this.options.xml.documentElement;
		
   	 	return this.createXML(xml);
   	},

   	createXML : function(xml, parent) {
   		;
      	if(!parent) {
	        parent = new Element(xml.nodeName);
     	}

   		if(xml.childNodes.length) {
	        for(var i = 0; i < xml.childNodes.length; i++) {
           		if(xml.childNodes[i].nodeType == 1) {

              		var el = new Element(xml.childNodes[i].nodeName);
		
              		if(xml.childNodes[i].attributes.length) {
                 		for(var j = 0; j < xml.childNodes[i].attributes.length; j++) {
                 			alert(xml.childNodes[i].attributes[j].nodeValue);
                    		var property = xml.childNodes[i].attributes[j].nodeName;
                    		var value    = xml.childNodes[i].attributes[j].nodeValue;
                    		el.setProperty(property, value);
                 		}
              		}
			  		if(xml.childNodes[i].firstChild) {
              			el.set({ value : xml.childNodes[i].firstChild.nodeValue });
			  		} else {
			  			el.set({ value : xml.childNodes[i].xml });
			  		}
			  		
              		parent.adopt(el);
		
              		var son = xml.childNodes[i];
              		if(son.childNodes.length) {
                 		this.createXML(son, el);
              		}
           		}
        	}
     	}
     	
     	return parent;
   	}
});

var TFilter = new Class({
	
	
	FilterForm: "",
	FilterTable: "",
	FilterTableDefault: "FL_RESULT",
	FilterTableElements: new Array(),
	FilterCloudID: "",
	FilterCloudOffset: 5,
	FilterCurrentStatus: "stopped",
	


	initialize: function(FilterURL,FilterForm,FilterTable) {
		
		
		var classElement = this;
		classElement.FilterForm = FilterForm;
		classElement.FilterURL = FilterURL;		
		classElement.FilterTable = FilterTable;
		classElement.FilterCloudID = "cloud_" + Math.round(Math.random() * 6) + "asx80" + Math.round(Math.random() * 6) + "as812" + Math.round(Math.random() * 6);

		if(!$(classElement.FilterForm)) {
			return false;
		}
		
		var FilterSubmitElement = $(classElement.FilterForm).getElements("input.filterbut");
		if($(classElement.FilterForm).getElements("input.resetbut")) {
			var FilterResetElement = $(classElement.FilterForm).getElements("input.resetbut");
		}

		
		FilterSubmitElement.removeEvents();
		FilterSubmitElement.addEvent("click",function(){
			this.blur();
			if(classElement.FilterCurrentStatus == "stopped") {
				classElement.runFilter();
				classElement.FilterCurrentStatus = "started";
			}
			
			return false;
		});	
		
		return classElement;
	},
	
	runFilter: function() {
		
		var classElement = this;
		var CloudID = classElement.showCloud();
		
		$(classElement.FilterForm).set("send", {
			url: classElement.FilterURL,
			method: "post",
			onSuccess: function(RESPONSE_TEXT,RESPONSE_XML) {
				classElement.buildTable(RESPONSE_TEXT,RESPONSE_XML);	
			},
			noCache: true
		}).send();
			
		
		return false;
	},
	
	buildTable: function(RESPONSE_TEXT,RESPONSE_XML) {

		var classElement = this;
		
		classElement.hideCloud();
		
		if($(classElement.FilterTable)) {	
			var FilterTableEntity = $(classElement.FilterTable).getElements("tr");
			for(i = 0;i < FilterTableEntity.length;i++) {
				if(FilterTableEntity[i].getProperty("id") != "thead" && FilterTableEntity[i].parentNode && (FilterTableEntity[i].parentNode.tagName != "THEAD"  && FilterTableEntity[i].parentNode.tagName != "thead")) { 
					FilterTableEntity[i].destroy();
				}
			}
		}
		
		if(RESPONSE_TEXT.length > 10) {
			if($(classElement.FilterTable)) {
				//alert(RESPONSE_TEXT);
				var as = new Element("table", {
					"class" : $(classElement.FilterTable).getProperty("class"),
					"id" : $(classElement.FilterTable).getProperty("id"),
					"html" : RESPONSE_TEXT
				}).replaces($(classElement.FilterTable));
				//as.innerHTML = RESPONSE_TEXT;
				//as
				
			} else if($(classElement.FilterTableDefault)) {
				$(classElement.FilterTableDefault).innerHTML = RESPONSE_TEXT;
			} else {
				alert("К сожалению произошла ошибка. Мы постараемся её исправить как можно быстрее. Спасибо за понимание.");
			}
		} else {
			
			new Element("td", {
				"colspan": "100",
				"html": "К сожалению, по указанным параметрам ничего не найдено."
			}).inject(new Element("tr").inject($(classElement.FilterTable).getElement("tbody")));
		}
		
		classElement.FilterCurrentStatus = "stopped";
		
		return classElement.hideCloud();
	},
	
	showCloud: function() {

		var classElement = this;
		
		if(!$(classElement.FilterCloudID)) {
			
			 if($(classElement.FilterTable)) {
			 	var CloudWidth = ($(classElement.FilterForm).getParent().getSize().x+(classElement.FilterCloudOffset*2));//((($(classElement.FilterTable).getPosition().x + $(classElement.FilterTable).offsetWidth)-$(classElement.FilterForm).getParent().getPosition().x)+(classElement.FilterCloudOffset*2));
			 	var CloudHeight = ((($(classElement.FilterTable).getPosition().y + $(classElement.FilterTable).offsetHeight)-$(classElement.FilterForm).getParent().getPosition().y)+(classElement.FilterCloudOffset*2));
			 } else {
			 	var CloudWidth = ($(classElement.FilterForm).getParent().getSize().x+(classElement.FilterCloudOffset*2));
			 	var CloudHeight = ($(classElement.FilterForm).getParent().getSize().y+(classElement.FilterCloudOffset*2));
			 }
			 new Element("div", {
				"id" : classElement.FilterCloudID,
				"class" : "ieopacity",
				"styles" : {
					"position" : "absolute",
					"top" : ($(classElement.FilterForm).getParent().getPosition().y-classElement.FilterCloudOffset) + "px",
					"left" : ($(classElement.FilterForm).getParent().getPosition().x-classElement.FilterCloudOffset) + "px",
					"width" : (CloudWidth > 0 ? CloudWidth : (CloudWidth*(-1))) + "px",
					"height" : (CloudHeight > 0 ? CloudHeight : (CloudHeight*(-1))) + "px",
					"background" : "#000000",
					"z-index" : "10"
				}
			}).setStyle("opacity","0.2").inject($(document.body));
		}
					
		return classElement.FilterCloudID;

	},
	
	hideCloud: function() {
		
		var classElement = this;
		if($(classElement.FilterCloudID)) {
			$(classElement.FilterCloudID).destroy();
		}
		
		return true;
	}
	
});