/* start slideshow */
var slideShow = new Class({
	initialize: function(images, togglers, iShow, delay, play, pause){
		this.images = images;
		this.togglers = togglers;
		this.iNow = iShow;
		this.delay = delay;
		this.pause = false;
		this.lock = false;

		this.hideAll(iShow);
		this.play();
		this.toggle();

		var self = this;

		//$('slideshow').addEvent('mouseenter', self.pauseShow.bind(self));
		//$('slideshow').addEvent('mouseleave', self.play.bind(self));
		
		pause.onclick = self.pauseShow.bind(self);
		play.onclick = self.play.bind(self);


	},
	toggle: function(){
		this.togglers.each(function(el,i){

			el.onclick = function(){
				if(!el.hasClass('active') && !this.lock){
					var images = this.images;
					var readmore = this.readmore;
					var iNow = this.iNow;
					$clear(this.timer);

					this.pause = true;
					this.lock = true;
					this.togglers[iNow].removeClass('active');
					images[iNow].effect('opacity', {duration: 700}).start(1,0)
					images[i].effect('opacity', {duration: 700, onComplete: function(){this.lock = false;}.bind(this)}).start(0,1);
					el.addClass('active');
					this.iNow = i;
					this.pause = false;
					//this.play();
				}
				return false;
			}.bind(this);
		}.bind(this));
		return false;
	},
	next: function(){
		var images = this.images;
		var readmore = this.readmore;
		var togglers = this.togglers;

		images[this.iNow].effect('opacity', {duration: 800, onStart: function(){this.lock = true;}.bind(this)}).start(1,0);
		togglers[this.iNow].removeClass('active');
		this.iNow ++;
		if(this.iNow == images.length) this.iNow = 0;
		togglers[this.iNow].addClass('active');
		images[this.iNow].effect('opacity', {duration: 1000, onComplete: function(){this.lock = false;}.bind(this)}).start(0,1);

	},
	hideAll: function(exceptionEl){
		for(var i=0, l=this.images.length; i < l; i++){
			this.images[i].setStyle('display', 'block');
			if(i != exceptionEl){
				this.images[i].setOpacity(0);
			} else {
				this.images[i].setOpacity(1);
				this.togglers[exceptionEl].addClass('active');
			}
		}
	},
	play: function(){
		this.timer = this.next.periodical(this.delay, this);
	},
	pauseShow: function(){
		$clear(this.timer);
	}
});
