/**
 * @author oem
 */

var cmsAjaxResponse = new Class({
	options:{
		loader: true,
		loaderId: '',
		loaderClassName:'loaderMedium',
		timeOut: 10000
	},
	initialize: function(url_req, options){
		var $this = this;
		this.setOptions(options);        
		this.request = new Request.JSON({ url: url_req,
		onRequest: function(){
			$this.showLoader();
			$this.startTimer();
		},
		/*
		onSuccess: function(jsonObj) {
			clearTimeout($this.requestTimer);
			$this.callFunction(jsonObj.response);
			$this.hideLoader();
		},
		*/
		onComplete: function(jsonObj) {
			clearTimeout($this.requestTimer);
			$this.callFunction(jsonObj.response);
			$this.hideLoader();
		},
		onFailure: function(){
			clearTimeout($this.requestTimer);
			$this.hideLoader();
			$this.errorLoad();
		}
		});
	},
	doRequest: function(req){
		this.request.get(req);
	},
	showLoader: function(){
		var $this = this;
		if(this.options.loader){
			this.loader = new Element('div', {'class':this.options.loaderClassName,'id':'loaderDiv'});
			if(this.options.loaderId!=''){
				this.loaderParent = $(this.options.loaderId);
				var dims = this.loaderParent.getSize();
				this.loader.setStyles({'width':dims.x,'height':dims.y});
				if($this.loaderParent.getStyles('position')!='relative') $this.loaderParent.setStyles({'position':'relative'}) 
				$this.loader.injectInside($this.loaderParent);
				return
			}
			else{
				$this.loaderParent = document.body;
				$this.loader.injectInside($this.loaderParent)
			}
		}	
	},
	hideLoader: function(){
		if (this.options.loader) {
			this.loader.remove();
		}
	},
	startTimer: function(){
		var $this = this;
		this.requestTimer = setTimeout(function() {
     		$this.request.fireEvent('onFailure', $this.request.transport);
    	 }, $this.options.timeOut); 
	},
	errorLoad: function(){
		var $this = this;
		//alert('error');
		clearTimeout($this.requestTimer);
	},
	callFunction: function(jsonObj){
		var $this = this;
		if (jsonObj.result == 0) { // error
			var wind = alert(jsonObj.message);
			return	
		}
		else {
			jsonObj.actions.each(function(item){
				switch (item.action) {
					case 'assign':
						$this.assign(item.id,item.attribute,item.value);
						break
					case 'append':
						$this.append(item.id,item.attribute,item.value);
						break
					case 'prepend':
						$this.prepend(item.id,item.attribute,item.value);
						break
					case 'replace':
						$this.replace(item.id,item.attribute,item.search, item.value);
						break
					case 'alert':
						$this.alertBox(item.title, item.message)
						break
					case 'insertAfter':
						$this.insertAfter(item.idAfter, item.id)
						break
					case 'insertBefore':
						$this.insertBefore(item.idBefore, item.id)
						break
					case 'addEvent':
						$this.addEv(item.id,item.event,item.value)
						break
					case 'removeEvent':
						$this.removeEv(item.id,item.event)
						break
					case 'script':
						$this.script(item.script)
						break
					case 'call':
						$this.call(item.funcName, item.args)
						break
					case 'removeById':
						$this.removeById(item.id);
						break
					case 'removeByClass':
						$this.removeByClass(item.className)
						break
					case 'create':
						$this.create(item.parent,item.tag,item.id,item.html,item.className,item.styles,item.events)
						break
					default:
						var wind = new cmsWindow(self, {title: 'error',content: ''});
						return
					}
				})
			return;
		}
	},
	assign: function(obj,attribute,value){
		var obj = $(obj);
		var $value = value;
		eval('obj.'+attribute+'=$value');
	},
	append: function(obj,attribute,value){
		var obj = $(obj);
		var $value = value;
		eval('obj.'+attribute+'=$value');
	},
	prepend: function(obj,attribute,value){
		var obj = $(obj);
		var oldVal = eval('obj.'+attribute);
		var newVal = value+oldVal;
		eval('obj.'+attribute+'=newVal');
	},
	replace: function(obj,attribute,search,value){
		var obj = $(obj);
		var oldVal = eval('obj.'+attribute).toString();
		var newVal = oldVal.replace(search,value);		
		eval('obj.'+attribute+'=newVal');
	},
	alertBox:	function(title, message){
		var wind = new cmsWindow(self,{title:title, content:message, loadMethod:'html',modal:true});
	},
	insertAfter: function(elAfter, el){
		$(el).injectAfter( elAfter );
	},
	insertBefore: function(elBefore, el){
		$(el).injectBefore( elBefore );
	},
	addEv: function(id,event,value){
		var $value = value;
		$(id).addEvent(event,function(){
			eval($value);
		})
	},
	removeEv: function(id, event){
		$(id).removeEvent(event)
	},
	script: function(script){
		eval(script);
	},
	call: function(funcName, args){
		eval(funcName+'('+args+')');
	},
	removeById: function(id){
		$(id).remove();
	},
	removeByClass: function(classname){
		$$(classname).each(function(item){
			item.remove();
		})
	},
	create: function(elParent,elTag,elId,elHtml,elClass,elStyles,elEvents){
		var newEl = new Element(elTag, {'styles':elStyles,'events':elEvents,'class':elClass,'id':elId});
		newEl.innerHTML = html;
		if(elParent){
			newEl.injectInside($(elParent));
			return
		}
		else {
			newEl.injectInside(document.body)
		}
	}
});
cmsAjaxResponse.implement(new Options);
