var Carousel = {
	switchDelay: 1000,
	delayBeforeResume: 25*1000, 
	switcherLocked : false, 
	resumePeriodical : false,
	effectDuration : 0.5,
	lockTimeout : 750,
	items : 0,
	total : 0,
	current : 0,   

	timedSwitch: function () {
		if (this.switcherLocked) return false;
		this.switcherLocked = true;
		curr = this.current;
		if (curr < this.total-1) { next = curr+1; } else { next = 0 };
//		Effect.Fade(this.items[curr],{duration:this.effectDuration});
		Effect.Fade(this.items[curr],{duration:1});
		Effect.Appear(this.items[next],{duration:this.effectDuration});
		this.updateTitle( this.items[next].down('img').alt );
		this.current = next;
		setTimeout(this.unlock.bind(this),this.lockTimeout);
		this.periodical = setTimeout(this.timedSwitch.bind(this),this.switchDelay);
		return false;
	},
	
	updateTitle: function(title){
		imageTitle = $$('.slideshow-bar h3').first();
		if (imageTitle) {
			$(imageTitle).update( title );
		}
	},
	
	unlock: function(){
		this.switcherLocked = false;
	},

	prev: function() {
		if (this.switcherLocked) return false;
		this.switcherLocked = true;
		curr = this.current;
		clearInterval(this.periodical);
		if (curr > 0) prev = curr-1; else prev = this.total-1;
		Effect.Fade(this.items[curr],{duration:this.effectDuration});
		Effect.Appear(this.items[prev],{duration:this.effectDuration});
		this.current = prev;
		setTimeout(this.unlock.bind(this),this.lockTimeout);
		this.periodical = setTimeout(this.timedSwitch.bind(this),this.switchDelay);
		return false;
	},

	next: function() {
		if (this.switcherLocked) return false;
		this.switcherLocked = true;
		curr = this.current;
		clearInterval(this.periodical);
		if (curr < this.total-1) next = curr+1; else next = 0;
		Effect.Fade(this.items[curr],{duration:this.effectDuration});
		Effect.Appear(this.items[next],{duration:this.effectDuration});
		this.current = next;
		setTimeout(this.unlock.bind(this),this.lockTimeout);
		this.periodical = setTimeout(this.timedSwitch.bind(this),this.switchDelay);
		return false;
	},
	
	switchItem: function(next) {

		if (this.current == next) return false;
		if (this.switcherLocked) return false;
		this.switcherLocked = true;
		
		clearInterval(this.periodical);
		if (this.resumePeriodical) clearInterval(this.resumePeriodical);
		
		curr = this.current;
		
		Effect.Fade(this.items[curr],{duration:this.effectDuration});
		Effect.Appear(this.items[next],{duration:this.effectDuration});
		this.current = next;
		setTimeout(this.unlock.bind(this),this.lockTimeout);
		
//		if (this.delayBeforeResume) 
//			this.resumePeriodical = setTimeout(this.periodical = setTimeout(this.timedSwitch.bind(this),this.switchDelay),this.delayBeforeResume);
		this.setControlState('play');
		this.periodical = setTimeout(this.timedSwitch.bind(this),this.switchDelay);
		return false;

	},
	
	setControlState: function(state){
		control = $('control_button');
		if (control){
			$(control).removeClassName('play');
			$(control).removeClassName('pause');
			$(control).addClassName(state);
		}			
	},
	
	togglePlay: function() {
		if (this.total < 2) return;
		control = $('control_button');
		clearInterval(this.periodical);
		if (this.resumePeriodical) clearInterval(this.resumePeriodical);
		if (control.hasClassName('pause')) {
			this.setControlState('play');
		} else {
			this.setControlState('pause');
			this.periodical = setTimeout(this.timedSwitch.bind(this),250);
		}
		return false;
	},

	
	start: function(delay) {
		this.items = $$('.fce-slider-item');
		this.total = $$('.fce-slider-item').length;
		if (this.total < 2) return;
		if(delay) this.switchDelay = delay;
		this.items.each(function(e){ e.hide()});
		this.items[0].show();
		this.periodical = setTimeout(this.timedSwitch.bind(this),this.switchDelay);		
	}
}