/* SimpleUpdater 1.0 */

/*
Questa classe č costruita per essere utilizzata con singole chiamate senza inizializzare alcun menu, 
o per essere chiamata da oggetti swf. I contenuti vengono aggiornati esclusivamente via ajax, caricando 
txt contenenti xhtml.
*/



var SimpleUpdater = Class.create();

SimpleUpdater.prototype = {
	initialize:function(boxId){
						
		this.boxId = (boxId) ? boxId : null;
		
		this.fxDuration = 0.7;
		this.fxtype = 'default';
		this.fxFunction = this.routeFx('fade');//uso fade come default
		
		this.onShow = function(){}; //settare al di fuori della classe
		
		this.utils = new Utils();
	},
	
	updateContent:function(contentPath,boxId){
		if(boxId){ this.boxId = boxId; }
		if(this.boxId == null){ this.utils.report('Errore: assegnare un boxId') }
		this.startAjaxRequest(contentPath);
	},

	startAjaxRequest:function(path){//>> parseAjaxResponse
		new Ajax.Request(path,
						{
						method:'get',
						onSuccess:this.parseAjaxResponse.bindAsEventListener(this),
						onFailure: function(){ alert('Error: no ajax request'); }
						});
	},
	
	parseAjaxResponse:function(t){//>> showAjaxResponse
		//this.utils.report(t.responseText,'debug','#000000');
		var r = (this.fxType != 'slide') ? t.responseText : '<div>'+t.responseText+'</div>';
		
		if(!r){ alert('Error: no ajax response'); return; }
		this.showAjaxResponse(r,this.boxId);
	},
	
	showAjaxResponse:function(r,box){
		if(box != null){
			//non uso hide(), cosė mantengo le dimensioni del box
			$(box).setStyle({opacity:'0'});
			$(box).update(r);
	
			//apro nuovo elemento
			this.fxFunction(box,1);
			this.onShow();
			//rimuovo il loader
			//this.loadBox.remove();
			//delete this.loadBox;
		}
	},
	
	/// EFFETTI ///
	
	defaultFx:function(el,type){
		if(type == 1){ el.setStyle({display:'block'}); this.busy = false; }
		else{  el.setStyle({display:'none'}); }
	},

	fadeFx:function(el,type){
		if(type == 1){  new Effect.Appear(el,{ duration:this.fxDuration, queue:'end', afterFinish:function(){ this.busy = false }.bind(this) }); }
		else{ new Effect.Fade(el,{ duration:this.fxDuration, queue:'front' }); }
	},
	
	slideFx:function(el,type){
		if(type == 1){  new Effect.SlideDown(el,{ duration:this.fxDuration, queue:'end', transition:Effect.Transitions.exponential, afterFinish:function(){ this.busy = false }.bind(this) }); }
		else{ new Effect.SlideUp(el,{ duration:this.fxDuration, queue:'front' }); }
	},
	
	blindFx:function(el,type){
		if(type == 1){ new Effect.BlindDown(el,{ duration:this.fxDuration, queue:'end', transition:Effect.Transitions.exponential, afterFinish:function(){ this.busy = false }.bind(this) }); }
		else{ new Effect.BlindUp(el,{ duration:this.fxDuration, queue:'front' }); }
	},
	
	routeFx:function(fxtype){
		
		this.fxtype = fxtype;
		switch(this.fxtype){
			case 'fade':
				this.fxDuration = 0.5;
				return this.fadeFx;
				break;
			case 'slide':
				this.fxDuration = 0.7;
				return this.slideFx;
				break;
			case 'blind':
				this.fxDuration = 0.7;
				return this.blindFx;
				break;
			default:
				this.fxtype = 'default';
				return this.defaultFx;
		}
	},
	
	setFx:function(fxtype,duration){
		this.fxFunction = this.routeFx(fxtype);
		this.fxType = fxtype;
		this.fxDuration = (duration) ? duration : this.fxDuration;
	},
	
	setFxDuration:function(duration){
		this.fxDuration = (duration) ? duration : this.fxDuration;
	}
}