//
// 
//
function _onCreate(rq, x, y, z){
	// tylko updater
	// periodicalupdater, request musza byc oddzielnie serwowane
	if (rq.container){
		if($(rq.container['success'])){
			$(rq.container['success']).addClassName('loader')
		}
		if($(rq.container['failure'])){
			$(rq.container['failure']).addClassName('loader')
		}
	}
}



function _onComplete(rq, x, y, z){
	if (rq.container){
		if($(rq.container['success'])){
			$(rq.container['success']).removeClassName('loader')
		}
		if($(rq.container['failure'])){
			$(rq.container['failure']).removeClassName('loader')
		}
	}
}


Ajax.Responders.register({onComplete: _onComplete, 
						  onCreate: _onCreate})
						  

var TabRotator = Class.create({
	initialize: function(tabs_configuration){
		/*
		 * tabs_configuration zawiera konfigurację rotatora
		 *  
		 * @param tabs_configuration = {'tabs': selektor css do listy tabsów (kontrolek)
		 *    'content': selektor css do listy elementow z zawartoscia
		 * }
		 */
		this.configuration = tabs_configuration;
		this.delay = tabs_configuration.get('delay') * 1000;
		this.lock = false;
		this.install();
		
//		this.rotate();
	},
	get_next_tab_idx: function() {
		if ( this.current_tab_idx  != undefined && this.current_tab_idx <this.tabs.length -1 ){
			return this.current_tab_idx +1;
		}
		else { return 0}
	},
	
	
	get_next_tab: function(elm){
		/*
		 * ustawia nastepny element i kontener z zawartością z listy tabów 
		 * jeśli jest podany elm, to szuka następnego elementu w liście za elementem
		 *    jeśli podany elm nie został znaleziony, rzucany jest wyjątek
		 */

		if (elm){

			var idx = this.tabs.indexOf(elm);
			if (idx< 0){throw 'Element '||elm||' nie został znaleziony w liście zakładek rotatora'}
			this.current_tab_idx = idx;	
		}

		this.current_tab_idx = this.get_next_tab_idx();
		
		this.current_tab = this.tabs[this.current_tab_idx];
		this.rotate_element = $$(this.current_tab.down('a').readAttribute('href'));
		if (! this.rotate_element) {throw "Nie znaleziono elementu wskazanego do rotacji: "+this.current_tab.readAttribute('rotate') }
		this.rotate_element = this.rotate_element[0];
		return this.current_tab;
	},
	
	get_tab: function(elm){
		var idx = this.tabs.indexOf(elm);
		if (idx< 0){throw 'Element '||elm||' nie został znaleziony w liście zakładek rotatora'}
		this.current_tab_idx = idx;	
		
		this.current_tab = this.tabs[this.current_tab_idx];
		this.rotate_element = $$(this.current_tab.down('a').readAttribute('href'));
		if (! this.rotate_element) {throw "Nie znaleziono elementu wskazanego do rotacji: "+this.current_tab.readAttribute('rotate') }
		this.rotate_element = this.rotate_element[0];
		return this.current_tab;
		
	},
	
	install: function(){
		/*
		 * instaluje taby i kontenery z treścią
		 * 
		 */
		this.tabs = $$(this.configuration.get('tabs'));
		if (!this.tabs || this.tabs.length<1) { throw 'Nie znaleziono zakładek do rotacji';}
		//this.current_tab_idx = false;
		
		this.get_next_tab();
		this.install_events();
		
		
		this.run_timer();
		},
		
	install_events: function(){
		
		var rotator = this;
		var ev = function(event){
			
			var object = event.element();
			//Element.extend(object);
			rotator.rotate(this);
			event.stop();
			return false;

		}
		
		
		this.tabs.each( function(e){ e.observe('click', ev)})
		
	},
	run_timer: function(){
		
		if (this.timer){
			clearTimeout(this.timer)
		}
		
		
		var ob = this;
		
		var do_rotate = function(){
			ob._rotate();
		}
		this.timer = setTimeout(do_rotate, this.delay);
		
	},
	
	rotate: function(elm){
		if (this.timer){
			clearTimeout(this.timer);
		}
		
		this.lock = true;
		var rotate_element = this.rotate_element;
		var current_tab = this.current_tab;
		
		this.get_tab(elm);
		var h = this.rotate_element.getHeight();		
		
		
		$(this.rotate_element.parentNode).morph('height:'+h+'px', {'duration': 0});
		current_tab.removeClassName('tab-sel');
		//rotate_element.fade({'delay': 0});
		rotate_element.hide();
		//this.rotate_element.appear({'delay': 0});
		this.rotate_element.show();
		this.current_tab.addClassName('tab-sel');
		this.lock = false;
		this.run_timer();
	},
	
	_rotate: function(){
		/*
		 * rotate wywoływane z timera
		 */
		rotator = this;
		
		if (rotator.lock){
			return false;
		}
		rotator.lock = true;
		
		var rotate_element = rotator.rotate_element;
		var current_tab = rotator.current_tab;
		rotator.get_next_tab();
		
		var h = rotator.rotate_element.getHeight();

		$(rotator.rotate_element.parentNode).morph('height:'+h+'px', {'duration': 0.05});
		current_tab.removeClassName('tab-sel');
		rotate_element.fade({'delay': 0.03});
		rotator.rotate_element.appear({'delay': 0.03, 'queue': 'end'});
		rotator.current_tab.addClassName('tab-sel');
		rotator.run_timer();
		rotator.lock = false;
	}
	
});
