Cache= Class.create();
Cache.prototype={
		initialize: function(options) {

			this.store=new Array();
			this.timeouts=new Array();
			if (typeof options['timeout'] != 'undefined' ) {
				this.defaultTimeout=options['timeout'];
			}
		},
		_timeout: function(hash) {
			this.delHashItem(hash);
		},
		_addItem: function(obj,hash) {
			this.store[hash]=obj;
			return hash;
		},
		setTimeout: function(timeout){
			this.defaultTimeout=timeout;
		},
		addItem: function(obj,hash,timeout) {
			this._addItem(obj,hash);
			if (typeof timeout!='undefined') {
				setTimeout(this._timeout.bind(this,hash),timeout);
			} else {
				if (this.defaultTimeout>0) {
					var tid=setTimeout(this._timeout.bind(this,hash),this.defaultTimeout);
					this.timeouts.push(tid);
				}
			}
		},
		getItem: function(obj) {
			var item=this.store[hex_md5(obj)];
			if (typeof item!='undefined') {
				return item;
			} else {
				return -1;
			}
		},
		getHashItem: function(hash) {
			var item=this.store[hash];
			if (typeof item!='undefined') {
				return item;
			} else {
				return -1;
			}
		},
		delItem: function(obj) {
			var item=this.store[hex_md5(obj)];
			if (typeof item!='undefined') {
				this.store[hex_md5(obj)]=undefined;
				return 1;
			} else {
				return -1;
			}
		},
		delHashItem: function(hash) {
			if (typeof this.store[hash]!=undefined) {
				this.store[hash]=undefined;
				return 1;
			} else {
				return -1;
			}
		},
		reset: function() {
			this.store=new Array();
			while (this.timeouts.length >0) {
				clearTimeout(this.timeouts.pop());
			}
		}
};


cItem= Class.create();
cItem.prototype={
		initialize: function(options) {
			if (options['sync']) {
				this.setSync();
			} else {
				this.setASync();
			}
			var type=typeof options['cache'];
			if (typeof options['timeout']!='undefined') {
				this.timeout=options['timeout'];
			} else {
				this.timeout=0;
			}
			this.setCache('off');
			switch (type) {
				case 'boolean': 
					if (options['cache']) {
						this.Cache= new Cache({'timeout':this.timeout});
						this.setCache('on');
					} else {
						this.setCache('off');
					}
					break;
				case 'object':
					this.Cache= options['cache'];
					this.setCache('on');
					break;
			}
			
			
			this.OnReady=options['OnReady'];
			//this.setCache('on');
			//this.Cache.setTimeout(this.timeout);
			this.uuid=Math.round(1000*Math.random());
			return this;
		},
		setSync: function() {
			this.reqMode='sync';
		},
		setASync: function() {
			this.reqMode='async';
		},
		setCache: function(mode) {
			if (mode.toLowerCase()=='on') {
				this.useCache=true;
			} else {
				this.useCache=false;
			}
		},
		resetCache: function() {
			this.Cache.reset();
		},
		SyncRequestItem:function(fController,fName,Params) {
			this.cached=false;
			this.msg=this._JSONMessage(fController,fName,Params);this.msg=[];
			this.useCache=false;
			if (this.useCache) {
				var cached=this.Cache.getItem(this.msg);
				if (cached!=-1) {
					this.cached=true;
					return cached;
				} else {
					var req=new Ajax.Request("/bllng/ajax/request.method", {
						  method: 'post',
						  contentType: 'text/html',
						  postBody: this.msg,
						  asynchronous: false});
					ret=req.evalResponse();
					this.Cache.addItem(ret,hex_md5(this.msg),this.timeout);
				}
			} else {
				var req=new Ajax.Request("/bllng/ajax/request.method", {
					  method: 'post',
					  contentType: 'text/html',
					  postBody: this.msg,
					  asynchronous: false});
				ret=req.evalResponse();
			}
			return ret;
		},
		
		callMethod: function(fController,fName,Params) {
			this.aSyncRequestItem(fController,fName,Params);
		},
		syncCallMethod: function(fController,fName,Params) {
			this.SyncRequestItem(fController,fName,Params);
		},
		
		aSyncRequestItem: function(fController,fName,Params) {
			this.cached=false;
			var href=document.getElementsByTagName('BASE')[0].href;
			//this.msg=this._XMLMessage(modName,type,fName,fParameter);
			this.msg=this._JSONMessage(fController,fName,Params);
			this.useCache=false;
			if (this.useCache) {
				var item=this.Cache.getItem(this.msg);
				if (item!=-1) {
					this.cached=true;
					this.OnReady(this,item);
				} else {
					var req=new Ajax.Request(href+"ajax/request.method", {
						  method: 'post',
						  postBody: this.msg,
						  contentType: 'text/html',
						  onSuccess: this.onSuccess.bind(this)
					});
				}
			} else {
				
				var req=new Ajax.Request(href+"ajax/request.method", {
					method: 'post',
					postBody: this.msg,
					contentType: 'text/html',
					onSuccess: this.onSuccess.bind(this)
				});
			}
		},
		onSuccess: function(transport) {
			this.item = transport.responseJSON;
			if (transport.responseText!=undefined && (this.item==undefined || this.item==null)) {
				this.item=eval("("+transport.responseText+")");
			}
			if (this.useCache) {
				this.Cache.addItem(this.item,hex_md5(this.msg),this.timeout);
			}
			eval(this.item.beforeData);
			this.OnReady(this,this.item.data);
			eval(this.item.afterData);
			

return;
		},
		_XMLMessage: function(modName,type,fName,fParameter) {
			var msg=new XMLRPCMessage(this.wrapperFunc);
			msg.addParameter(modName);
			msg.addParameter(type);
			msg.addParameter(fName);
			msg.addParameter(fParameter);
			return msg;
		},
		_JSONMessage: function(fController,fName,fParameter) {
				var msg='{"fController":"'+fController+'",'+
				'"fName":"'+fName+'",'+
				'"fParameter":'+Object.toJSON(fParameter)+'}';
				return msg;
		},
		_JSONBlockMessage: function(blockType,blockModule,blockName) {
			var msg='{"blockType":"'+blockType+'",'+
					'"blockModule":"'+blockModule+'",'+
					'"blockName":"'+blockName+'",'+
					'"blockRender":true}';
			return msg;
		
		}
};

function callMethod(fController,fName,fParameter,onReadyCall) {
	new cItem({
		 OnReady:OnReadyCall
	 }).callMethod(fController,fName,fParameter); 
}


