updaters = {};

lang.updater = {
	'es':{'load':"Cargando datos...",
		   'submit':"Mandando datos..."
		  },
	'en':{'load':"Loading data...",
		  'submit':"Submitting data..."
		  }
};

Updater = Class.create();
Updater.prototype = {
	
	initialize: function(id, app, cmd, options) {
		if (id) {
			this.id = id;
			this.self = $(this.id);
			this.content = $(this.id + "_content");
			this.options = options || {};
			if ($(this.id + "_status")) {
				var col = this.options.color || status_color;
				this.status = new StatusComponent(this.id + "_status", col);
			}
			this.app = app;
			this.cmd = cmd;
			
			this._initAjax();
		}
	},
	
	
	
	/** PUBLIC INTERFACE **/
	
	run: function(args, cmd, msg) {	
		if (this.options.status_style == 'simple') msg = '';
		if (this.status) this.status.show(msg);
		var callParams = [];
		callParams.push( this.id + '_request');
		callParams.push('id=' + this.id);	
		callParams.push('cmd=' + ((cmd) ? cmd : this.cmd));	
		if (args) {
			for(var i=0; i<args.length; ++i) {
				callParams.push(args[i]);			
			}
		}
		if (console)console.log(callParams);
		ajaxEngine.sendRequest.apply(ajaxEngine, callParams);
	},
	
	update: function(ajaxResponse) {		
		var fields = ajaxResponse.getElementsByTagName('field') || [];
		if (console) console.log('fields length: ' + fields.length);
		if (fields.length) {
			var effects = [];
			for(var i=0; i<fields.length; ++i) {
				var input = fields[i];
				var value = RicoUtil.getContentAsString(input);
				var target = $(input.getAttribute('id'));
				if (console) console.log('target: ' + target);
				
				if (target) {
					
					switch(target.nodeName) {						
						
						default:
							var e_flag = false;
							switch(input.getAttribute('type')) {
								case 'html':
									target.innerHTML = value;
									e_flag = true;
									break;
								case 'transition':
									
									var v = value;
									var style = input.getAttribute('style');
									
									switch(style) {
										case 'blind':
											if (v.length) {
											var effect = new Effect.BlindUp(target.id,
																			{duration: 0.5,
																			 afterFinish: this._enterAnim,
																			 value:v, me:this, style:style
																			 });
											} else {
												var effect = new Effect.BlindUp(target.id,
																			{duration: 0.5});
											}
											break;
										case 'appear':
											var effect = new Effect.Fade(target.id,
																		  {duration: 0.5,
																		  afterFinish: this._enterAnim,
																		  value:v, me:this, style:style
																		  });
											break;
										default:	
											var effect = new Effect.Opacity(target.id, 
																			  {duration: 0.5,
																			  transition: Effect.Transitions.linear, 
																			  from: 1.0, to: 0.0,
																			  afterFinish: this._enterAnim,
																			  value:v, me:this, style:style
																			  });
									}
									
									break;
								case 'texteditor':
									tinyMCE.setContent(value);
									break;
								case 'append':
									target.innerHTML += value;
									e_flag = true;
									break;
								default:
									switch (target.type) {
										case 'checkbox':
											target.checked = 'checked';
											break;
										case 'radio':
											target.checked = 'checked';
											break;
										default:
											target.value = value;
											if (console) console.log('SET VALUE: ' + value);
											break;
									}
							}
							if (e_flag) {
								ApexUtil.evalScripts(target);
							}
							break;
					}
				}
				
			}
		}
	}, 
	
	callService: function(service, args, msg, recurring) {
		if (console) console.log('REQUEST FOR: '+service+' ARGS: '+args+' RECURRING: '+recurring);
		if (typeof ajaxEngine.requestURLS[service] == 'undefined') {
			ajaxEngine.registerRequest(service, '/services/'+service);	
			if (console) console.log('REGISTERING REQUEST: '+service+' : '+ajaxEngine.requestURLS[service]);
		}
		if (this.options.status_style == 'simple') msg = '';
		if (this.status && (msg || this.options.status_style == 'simple')) this.status.show(msg);
		var callParams = [];
		callParams.push(service);
		callParams.push('updater=' + this.id);
		if (args) {
			for(var i=0; i<args.length; ++i) {
				callParams.push(args[i]);			
			}
		}
		if (console)console.log(callParams);
		ajaxEngine.sendRequest.apply(ajaxEngine, callParams);
		
		if (recurring) {
			clearInterval(this.timers[service]);
			this.timers[service] = setInterval(this.callService.bind(this), recurring, service, args, msg, false);
		}
	},
	
	/** PRIVATE METHODS **/
	
	ajaxUpdate: function(ajaxResponse) {

		if (console) console.log('UPDATER RESPONSE: '+this.id);
		if (this.options.parent) {
			this.options.parent.ajaxUpdate(ajaxResponse);
		} else {
		
			if (console) console.log('ajax response: ' + ajaxResponse);
			this.update(ajaxResponse);
			
			var content = ajaxResponse.getElementsByTagName('content')[0];
			if (content) {
				this.content.innerHTML = RicoUtil.getContentAsString(content);
				ApexUtil.evalScripts(this.content);
			}
				
			if (this.self) this.self.style.display = '';
			if (this.status) this.status.hide();		
		}
		
		this.onUpdate();
	},
	
	onUpdate: function() {},
	
	_initAjax: function() {
		ajaxEngine.registerRequest(this.id + '_request', this.app);		
		ajaxEngine.registerAjaxObject(this.id + '_update', this);
	},
	
	_enterAnim: function(ob) {
		ob.element.innerHTML = ob.options.value;
		ApexUtil.evalScripts(ob.element);
		
		switch(ob.options.style) {
			case 'blind':
				var effect = new Effect.BlindDown(ob.element.id, {duration: 1});	
				break;
			case 'appear':
				var effect = new Effect.Appear(ob.element.id, {duration: 1});	
				break;
			default:	
				var effect = new Effect.Opacity(ob.element.id, {duration: 1,
											  transition: Effect.Transitions.linear, 
											  from: 0.0, to: 1.0});	
		}
		
	}
};
