/**
* part of Dynamic Elements Collection
* 
* @author Dimitri Bashev
***********************************************************
*/

/**
* DEC HTTP Request
***********************************************************
*/
var HttpRequest = false;
function DEC_HttpRequest(){
	// only one copy of DEC_HttpRequest available
	if(HttpRequest instanceof DEC_HttpRequest){
		return HttpRequest;
	}else if(this instanceof DEC_HttpRequest){
		HttpRequest = this;
	}else{
		return new DEC_HttpRequest();
	}
	// requests collection
	var hRequests = [];
	/**
	* send requests to server
	*/
	this.send = function(sHref,hContent,fReadyHandler,node){
		fReadyHandler = fReadyHandler || function(hData){ return;};
		// request hash
		var requestId = hRequests.length;
		var hRequest = hRequests[requestId] = {
			id:requestId,
			query:sHref + "?" + this._hash2query(hContent)+"&requestId="+requestId+"&"+Math.random(), 
			content:hContent,
			querySpan:null,
			
			readyHandler:fReadyHandler,
			responseData:null,
			responseStatus:0						// 0 - sending, 1 - response
		};
var oSelector1 = document.getElementById("000000");	
if (oSelector1) oSelector1.innerHTML = hRequest.query;			

		// create script DOM element
		hRequest.querySpan = ((node)?node:document.body).appendChild(document.createElement("SPAN"));
		hRequest.querySpan.setAttribute(classFix,"loading");
		hRequest.querySpan.innerHTML = 'Подождите, идет загрузка ...<s'+'cript />';
		setTimeout( function() {
				var s = hRequest.querySpan.getElementsByTagName("script")[0];
				s.language = "JavaScript";
				(s.setAttribute)?s.setAttribute('src',  hRequest.query):s.src = hRequest.query;
		}, 5);
		return requestId;
	}
	/**
	* get response data
	*/
	this.ready = function(requestId,hData){
		if(hRequests[requestId]){
			this._cleanup(requestId);
			hRequests[requestId].responseData = hData;
			hRequests[requestId].responseStatus = 1;
			hRequests[requestId].readyHandler(hData);
		}
	}
	this.ready_new = function(requestId,hData,param1){// ну очень надо дополнительный параметр
		if(hRequests[requestId]){
			this._cleanup(requestId);
			hRequests[requestId].responseData = hData;
			hRequests[requestId].responseStatus = 1;
			hRequests[requestId].readyHandler(hData, param1);
		}
	}
	/**
	* clean memory
	*/
	this._cleanup = function(requestId){
		var hRequest = hRequests[requestId];
		setTimeout(function(){
			hRequest.querySpan.parentNode.removeChild(hRequest.querySpan);
			hRequest.querySpan=null;
		},50);
		return false;
	}
	/**
	* encode hash data
	*/
	this._hash2query = function(hContent, sPrefix){
		sPrefix = sPrefix || "";
		var query = [];
		if(hContent instanceof Object){
			for(var key in hContent) {
//				alert(key+" -> "+hContent[key]+" -> ")+this.escape(hContent[key]);
				var curPrefix = sPrefix ? sPrefix+'['+this.escape(key)+']' : this.escape(key);
				if(hContent[key] instanceof Object){
					query[query.length] = this._hash2query(hContent[key], curPrefix);
				}else{
					query[query.length] = curPrefix + "=" + this.escape(hContent[key]);
				}
			}
		}
/*		for(var key in query) {
			alert(query[key]);
		}
*/
		return query.join('&');
	}
	/**
	* JS escape() does not quote '+'
	*/
	this.escape = function(sStr){
		// с кодировками проблема что ли... некогда разбиратся :(
//		return escape(sStr).replace(new RegExp('\\+','g'), '%2B');
		return sStr;
	}
}

