var slideShow = new Class({
	initialize: function(images, togglers, readmore, iShow, delay){
		this.images = images;
		this.togglers = togglers;
		this.readmore = readmore;
		this.iNow = iShow;
		this.delay = delay;
		this.pause = false;
		this.lock = false;
		
		this.hideAll(iShow);
		this.play();
		this.toggle();
	},
	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: 1000}).start(0,1);
					readmore[iNow].effect('opacity', {duration: 700}).start(1,0)
					readmore[i].effect('opacity', {duration: 1000, 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);
		readmore[this.iNow].effect('opacity', {duration: 800}).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}).start(0,1);
		readmore[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);
				this.readmore[i].setOpacity(0);
			} else {
				this.images[i].setOpacity(1);
				this.readmore[i].setOpacity(1);
				this.togglers[exceptionEl].addClass('active');
			}
		}
	},
	play: function(){
		this.timer = this.next.periodical(this.delay, this);
	}
});

window.addEvent('domready', function(){
	if($('partnerInfo')){
		$('partnerInfo').getElement('a[class=close]').onclick = function(){
			$('partnerInfo').setStyle('display', 'none');
			return false;
		}
		$$('a.partner').each(function(el,i){
			el.onclick = function(){
				$('partnerInfo').setStyle('display', 'block');
				return false;
			}
		});
	}
	
	if($('overlay')){
		$('overlay').setStyle('height', document.body.offsetHeight);
		if($('watch')) $('watch').onclick = function(){
			$('overlay').setStyle('display', 'block');
			$$('.frame')[0].setStyle('display', 'block');
			return false;
		}
		$('overlay').onclick = function(){
			$('overlay').setStyle('display', 'none');
			$$('.frame')[0].setStyle('display', 'none');
			return false;
		}
		$$('.frame')[0].getElement('a[class=close]').onclick = function(){
			$('overlay').setStyle('display', 'none');
			$$('.frame')[0].setStyle('display', 'none');
		}
	}
});

window.onload = function(){	
	var images = $('testimonial').getElements('img');
	var togglers = $('togglers').getElements('a');
	var readmore = $('readMore').getElements('a');
	var testimonial = new slideShow(images, togglers, readmore, 0, 8000);
}