//� <stipuha /> development (2008)
//--------------------------------
//CLIENT CONFIGURATION
//--------------------
// define href="?key1=value&key2=(this.value)&key3=(func())" (get query)
// define target="uid123" (unique tag id for ajax content)
// define autoload (automatic send get query on load content)
// define onstarting="func()" (event calling before ajax sending)
// define oncomplete="func()" (event calling when content already loaded)

//SERVER CONFIGURATION
//--------------------
// define $RESPONSE["complete"]=1 (condition for execute oncomplete function after POST query)
// define $RESPONSE["alert"]="message message message" (condition for open system information popup)

	
var LAST_AJAX_CALL=null; //last object which call ajax


//> HTMLAJAX functionality
//----------------------------------
function HTMLAJAX(obj){
	var property='ajax';
	this.Tobj=obj;
	var _this=this;
    (this.req=new AJAX()).onready=function(response){_this.onready(response)};
	
	
	this.init=function(){
		//init vars
		this.target=_(_ATTR(this.Tobj,"target"));
		this.indicator=eval(_ATTR(this.Tobj,"indicator"));
		this.onstarting=_EV(this.Tobj,'onstarting','request');
		this.oncomplete=_EV(this.Tobj,'oncomplete','response');
		this.onfailure=_EV(this.Tobj,'onfailure');
		if(this.target) this.target.call_func=LAST_AJAX_CALL; //attach call ajax on target
		
		//try to cache
		var cache_obj=this.Tobj.cache_obj || obj.cache_obj || this.Tobj || obj;
		if(cache_obj && _ATTR(cache_obj,"cache")){
			if(!cache_obj._cache) cache_obj._cache=CACHE.set(_ATTR(cache_obj,"cache"));
			if(!CACHE.out(cache_obj._cache) && !cache_obj.nocaching) this.req.caching=true;
		}
	};

	this.exec=function(){
		if(this.onstarting(this.req.hash)==false) return false; //break if onstarting event return false
		if(this.onbeforesend()==false) return false; //break if onbeforesend function  return false
		if(this.indicator!=0) if(_("loader_ico")) _("loader_ico").style.display="block"; //show global ajax loading icon
		this.req.send();
		return false;
	};

	
	// parse link dynamic params & return executed query
	this.url2query=function(url){
		if(!url) return "";
		url=url.replace(/[^?]*\/(.*)$/,"$1"); // clear path from url
		var file=url.replace(/([^?]*)\??.*/,"$1");
		//if(file) this.req.file=file;
		url=url.replace(/[^?]*\?/,"");
		if(!url) return "";
		var dyn=url.match(/&?[a-z0-9_]+=\([^&]+\)/gi);
		if(dyn && dyn.length){
			for(var i=0,a,l=dyn.length;i<l;i++){
				a=dyn[i].split(/=/);
				var hash={};
				hash[a[0].replace(/&/,"")]=((new Function('return '+a[1])).$this(obj))();
				url=url.replace(/&?[a-z0-9_]+=\([^&]+\)/, this.req.json2query(hash)); //add value to query
			}
		}
		return url;
	};
	
	this.onready=function(response){
		try{if(DEBUGGER_ENABLED)Debugger.add("(target="+obj.target+")\n"+"["+this.req.query+"]\n"+"\n----------\n"+o2s(response.obj)+"\n----------\n"+response.text);}catch(e){};
		
		if(this.target){
			this.target.sender=obj;
			this.target.innerHTML=this.req.response.text; //if echo -- write innerHTML
			if(this.req.response.obj && this.req.response.obj.target) //if target properties in responce exist set properties to target
				for(var i in this.req.response.obj.target)
					this.target[i]=this.req.response.obj.target[i];
			init_content(this.target);
		}
		_act(obj);
		this.onreturn(this.req.response);
		if(_("loader_ico")) _("loader_ico").style.display="none";
	};
	
	this.onbeforesend=function(){};
	this.onreturn=function(){};
	
	return this;
};



//> AJAX CACHE control functionality
//--------------------------------
//> INITIALIZE: if(!obj._cache) obj._cache=CACHE.set(5); //10 minutes cache
//> check cache time out:  if(CACHE.out(obj._cache)) ajax.caching=false; 
//> from tag:  cache="5" (5 minutes cache) || cache="*" (default cache)
var CACHE={
	MULTIPLIER:60*1000, // set cache in [minutes]
	from:(new Date()).getTime(),
	set:function(cache_period){
		var _cache={};
		var cur_time=(new Date()).getTime();
		_cache.period=Number(cache_period=="*"?CACHE_DEFAULT:cache_period);
		_cache.start=cur_time;
		_cache.end=cur_time+_cache.period*this.MULTIPLIER;
		return _cache;
	},
	out:function(_cache){
		var cur_time=(new Date()).getTime();
		if(_cache.end<cur_time || _cache.start<this.from){ 
			//if cache time out replace tag cache attribuses (start,end)
			_cache.start=cur_time;
			_cache.end=this.set(_cache.period).end;
			return true;
		}
		return false;
	},
	reset:function(){this.from=(new Date()).getTime()}
};




// GET - ajax subclass
function GET(obj,e){
	if(!AJAX_ENABLED) return false;
	LAST_AJAX_CALL=function(e){window["GET"](obj,e)};
	
	if(window.event||e) obj.nocaching=(window.event||e).ctrlKey; //nocaching flag if ctrlKey pressed
	
	var htmlajax=new HTMLAJAX(obj); /*init parent class*/
	htmlajax.req.file=AJAX_GET;
	if(obj.Tobj) htmlajax.Tobj=obj.Tobj;
	htmlajax.init();
	htmlajax.req.query=htmlajax.url2query(_ATTR(obj,"aref")||_ATTR(obj,"href")); //set parameters from attribute [href]
	
	if(!htmlajax.req.query && !obj.hash) return false; //exit if no parameters
	
	htmlajax.onbeforesend=function(){
		if(this.target && this.indicator!=0) this.target.innerHTML="Loading...";
	};
	htmlajax.onreturn=function(response){
		exec_vars(this,"GET");
	};
  return htmlajax.exec();
};



// POST - ajax subclass
function POST(obj,e){
	if(!AJAX_ENABLED) return false;
	LAST_AJAX_CALL=function(e){window["POST"](obj,e)};
	
	if(obj.in_process) return false;
	
	htmlajax=new HTMLAJAX(obj); /*init parent class*/
	htmlajax.req.file=AJAX_POST;
	
	var Tobj=document.forms[_ATTR(obj,"form")];
	if(Tobj){
		Tobj.target=_ATTR(obj,"target") || Tobj.target;
		Tobj.action=_ATTR(obj,"href") || Tobj.action;
		htmlajax.Tobj=Tobj;
	}
	
	htmlajax.init();
	
	
	obj._form=_(htmlajax.Tobj, "/form") || htmlajax.Tobj; //find form element in parent nodes
	htmlajax.req.query=htmlajax.url2query(_ATTR(obj._form,"action")); //set parameters from form attribute [action]
	
	if(obj._form.elements)
		for(var i=0;i<obj._form.elements.length;i++){
			if(obj._form.elements[i].tagName=="FIELDSET") continue;
			var is_send=((!obj._form.elements[i].getAttribute("flag")&&!_ATTR(obj,"flag"))||(obj._form.elements[i].getAttribute("flag")&&_ATTR(obj,"flag")&&obj._form.elements[i].getAttribute("flag")==_ATTR(obj,"flag"))?1:0);
			if(is_send && obj._form.elements[i].getAttribute("send")!=undefined) is_send=eval(obj._form.elements[i].getAttribute("send"));
			
			if(is_send && obj._form.elements[i].type=="checkbox") htmlajax.req.hash[obj._form.elements[i].name]=(obj._form.elements[i].checked?1:0);
			else if(is_send && obj._form.elements[i].type=="radio"){ if(obj._form.elements[i].checked) htmlajax.req.hash[obj._form.elements[i].name]=obj._form.elements[i].value;}
			else if(is_send && obj._form.elements[i].type=="file") htmlajax.req.hash[obj._form.elements[i].name]=obj._form.elements[i];
			else if(is_send) htmlajax.req.hash[obj._form.elements[i].name]=obj._form.elements[i].value;
		}
	//for(k in this.req.hash) alert(k+"="+this.req.hash[k]);
		
	htmlajax.onbeforesend=function(){
		obj.in_process=1;
		//if(this.indicator) _loading_modal=new Modal({opacity:0});
	};
	htmlajax.onreturn=function(response){
		setTimeout(function(){obj.in_process=0},500);
		//if(this.indicator) _loading_modal.remove();
		exec_vars(this,"POST");
	};
  return htmlajax.exec();
};




//check and execute vars operation which come from server
function exec_vars(htmlajax, ajax_type){
if(!htmlajax) return null;
with(htmlajax.req)
	if(response && response.obj){
		if(response.obj.change_zoom!=undefined){			
			if(_("map_zoom_ctrl")){
				//alert(response.obj.change_zoom);
				var zoom_obj=_("map_zoom_ctrl").getElementsByTagName("a")[response.obj.change_zoom];
				var zoom_level=_ATTR(zoom_obj, "zoom");
				var form=_("search_form");
				_act(zoom_obj);
				form.zoom.value=zoom_level;
				//mapa.setZoom(zoom_level);
				//alert(zoom_level);
			}
		}
		if(response.obj.point){
			drawMarkers(response.obj.point, response.obj.center);
		}
		if(response.obj.alert!=undefined){
			document.alert(response.obj.alert);
		}
		if(response.obj.error!=undefined){
			document.alert(response.obj.error, "error");
		}
		
		if(response.obj.complete==undefined || Number(response.obj.complete)) htmlajax.oncomplete(response);
		else htmlajax.onfailure(response);
	}
};

