/*
 * Sliding box
 * 
 * (c) 2007-2008 Peter Cicman, Divio GmbH 
 */

var SlideBox = Fx.Slide.extend({
	canSwitch: false,
	newContent: "",
	
	initialize: function(){
		this.parent.apply(this, arguments);
		this.addEvent('onStart', this.slideStart.bind(this));
		this.addEvent('onComplete', this.slideDone.bind(this));
		if (!this.options.onSwitchShow) this.options.onSwitching = Class.empty;
		if (!this.options.onProgress) this.options.onProgress = Class.empty;
		// no content? hide bar
		if($('footer-content').getSize().size.y == 0) {this.hide()} else this.show();
	},
	
	switchContent: function(method, args){
		//this.wrapper.setStyle('overflow', 'hidden');
		this.canSwitch = 0;
		this.open && !window.webkit ? this.slideOut() : this.trySwitch(); //doesnt work on safari !! 
		this.newContent = "";
		this.req = new JsonRpc({'onComplete': this.dataReady.bind(this)})
		this.req.rcall(method, args);
	},
	
	progress: function(){
		this.fireEvent('onProgress');
	},
	
	slideStart: function(){
		this.progressTimer = this.progress.periodical(Math.round(1000 / this.options.fps), this);
	},
	
	slideDone: function(){
		this.progressTimer = $clear(this.progressTimer);
		if (this.now[0] < 0) {
			this.trySwitch();
		} else {
			//this.wrapper.setStyle('overflow', '');
			this.progress();
		}
	},
	
	dataReady: function(data){
		this.newContent = data;
		this.trySwitch();
	},
	
	vertical: function(){
		this.margin = '';
		this.layout = 'height';
		this.offset = this.element.offsetHeight;
	},
	
	trySwitch: function(){
		this.canSwitch++;
		if (this.canSwitch == 2){
			// do switch
			$(this.element).empty().setHTML(this.newContent);
			this.req.evals(true);
			// focus to first form fied if there is one
			try{
				($E('form', this.element).getFormElements()[0].focus()).delay(600);
			} catch(e){};
			this.fireEvent('onSwitchShow');
			this.hide();
			this.slideIn();
		}
	}
});