// JavaScript Document
//ListControl object constructor
var ListControlAjax = function() {
//	var url ='&notemplate=1';
//	var getListMethod = 'getListAX';
//	var list_container = 'list_container';
//	var componentID = '0';
}

//ListControl object powered by Ajax calls.
//TODO: should make all responces in JSON.
ListControlAjax.prototype = {
	url:'&notemplate=1',
	getListMethod:'getListAX',	
	componentID:'0',
	list_container:this.componentID+'_list_container',
	searchStr:'',
	filterStr:'',
	
	myReturn : function() {
		alert(this.list_container);
	},

	//updates ListControl list
	ListControXupd:function(list_container)
	{		
		var XgetData_url = this.url+'&f='+this.getListMethod;
		
		this.updateIndicatorShow(list_container);
		
		new Ajax.Request(XgetData_url, {
		  method: 'get',
		  asynchronous:true, 
		  evalScripts:true,
		  onSuccess: function(transport) {
			var list_container_obj = $(list_container);			
			list_container_obj.update(transport.responseText);
			list_container_obj.show();
			//list_container_obj.hide();
			//new Effect.Appear(list_container_obj );			
		  }
		});
	},
	//update column config for current user and return a list
	ListControlColumnAdminXupd:function(list_container){
		var XgetData_url = this.url+'&f='+this.getListMethod+'&search'+this.componentID+'='+this.searchStr+this.filterStr;
		
		//get all column administration elements
		var form = $('ListControlAdminForm'+this.componentID);
		$str = form.serialize();
		
		new Ajax.Request(XgetData_url, {
		  method: 'post',
		  asynchronous:true, 
		  evalScripts:true,
		  postBody:$str,
		  onSuccess: function(transport) {
			var list_container_obj = $(list_container);			
			list_container_obj.update(transport.responseText);
			list_container_obj.show();
			//list_container_obj.hide();
			//new Effect.Appear(list_container_obj );			
		  }
		});
	},
	//turns a page of a list. returns required page html
	turnListPageXupd:function(page, limit, list_container)
	{
		var XgetData_url = this.url+'&f='+this.getListMethod+'&page'+this.componentID+'='+page+'&limit'+this.componentID+'='+limit+'&search'+this.componentID+'='+this.searchStr+this.filterStr;
		//this.myReturn();
		this.updateIndicatorShow(list_container);
		
		new Ajax.Request(XgetData_url, {
		  method: 'get',
		  asynchronous:true, 
		  evalScripts:true,		  
		  
		  onUninitialized: function(transport) {
			  //alert('onUninitialized');
			//write(transport.responseText);
		  }, //(maps on Created)
			onLoading: function(transport) {
				//alert('onLoading');
			//write(transport.responseText);
		  } ,//(maps on Initialized)
			onLoaded: function(transport) {
				//alert('onLoaded');
			//write(transport.responseText);
		  }, //(maps on Request sent)
			onInteractive: function(transport) {
				//alert('onInteractive');
			//write(transport.responseText);
		  }, //(maps on Response being received)
			//onXYZ, //(numerical response status code), onSuccess or onFailure (see below)
			onComplete: function(transport) {
				//alert('onComplete');
			//write(transport.responseText);
		  },//
		  
		  //onComplete: function(transport) {
//			//if (200 == transport.status)
//			  this.myReturn();
//		  },
		  onSuccess: function(transport) {

			var list_container_obj = $(''+list_container);//NT_c1_list_container
			
			list_container_obj.update(transport.responseText);
			list_container_obj.show();	
			//list_container_obj.hide();
			//new Effect.Appear(list_container_obj, {duration:0.4} );
			//new Effect.Grow(list_container_obj, {direction:'top-left'} );
		  }
		});
	},
	
	sortListPageXupd:function(sortby,list_container)
	{
		var XgetData_url = this.url+'&f='+this.getListMethod+'&sort'+this.componentID+'='+sortby+'&search'+this.componentID+'='+this.searchStr+this.filterStr;
		this.updateIndicatorShow(list_container);
		
		new Ajax.Request(XgetData_url, {
		  method: 'get',
		  asynchronous:true, 
		  evalScripts:true,
		  onSuccess: function(transport) {
			var list_container_obj = $(list_container);	
			
			list_container_obj.update(transport.responseText);
			list_container_obj.show();
			//new Effect.Appear(list_container_obj );
		  }
		});
	},
	//search list
	searchListXupd:function(list_container){
		
		//var searchStr = $('otsing'+this.componentID).value;
		this.searchStr = encodeURIComponent($('otsing'+this.componentID).value);
		//alert(searchStr);
		
		var XgetData_url = this.url+'&f='+this.getListMethod+'&search'+this.componentID+'='+this.searchStr+this.filterStr;		
		this.updateIndicatorShow(list_container);
		
		new Ajax.Request(XgetData_url, {
		  method: 'get',
		  asynchronous:true, 
		  evalScripts:true,
		  onSuccess: function(transport) {
			var list_container_obj = $(list_container);	
			
			list_container_obj.update(transport.responseText);
			list_container_obj.show();
			//new Effect.Appear(list_container_obj );
		  }
		});
		
	},
	
	setFilter:function(new_filter_str){
		this.filterStr = new_filter_str;
	},
	
	updateIndicatorShow:function(container_id){
		var container = $(container_id);
		container.innerHTML = '<div style="float:right; z-index:3000;  padding-left:10px;"><img src="/templates/default/img/indicator.gif"  alt="Loading..." border=0 align="absmiddle" title="Loading..."/></div>'+container.innerHTML;
		$(container_id).show();
	}	
}
