var tkSlideShow = new Class({
	
	options: {
		autoPilot: false,
		startAt: 0,
		timer: 5000
	},
	
	initialize: function(slides, handles, defenitions, options){
		this.options = options;
		this.setOptions(this.options);
		this.slides = slides;

		this.handles = handles;
				
		if (this.options.autoPilot)
		{
			this.handles.each(function(handle, i){
				handle.addEvents({
					'mouseover': this.showSlide.bind(this, i),
					'mouseenter': (function(){ this.mouseOn = true }).bind(this),
					'mouseleave': (function(){ this.mouseOn = false }).bind(this)
				})
			},this);
		}
		else 
		{
			this.handles.each(function(handle, i){
				handle.addEvents({
				 	'click': this.showSlide.bind(this, i) 
				})
			},this);			
			
		}
		/*
		this.handles.each(function(handle, i){
			
			if(this.options.autoPilot == false)
			{
			handle.addEvents({
				
			 'click': this.showSlide.bind(this, i) 
			})
		}
		 else 
			{
			handle.addEvents({

				'mouseover': this.showSlide.bind(this, i),
				'mouseenter': (function(){ this.mouseOn = true }).bind(this),
				'mouseleave': (function(){ this.mouseOn = false }).bind(this)
			})
		}

				
				
				
			});
		}, this);
		*/
		this.defenitions = defenitions;
		this.dl = [];
		$ES('dt', this.defenitions).each(function(dt, i){
			this.dl[i] = { 
				'dt':dt,
				'dd':dt.getNext()
			};
		}, this);
		this.hideStuff();
		this.showSlide(this.options.startAt);
		if(this.options.autoPilot)
			this.autoPilot.periodical(this.options.timer, this);
		return this;
	},
	
	hideStuff: function(){
		this.slides.each(function(slide){
			slide.setStyle('display', 'none');
		});
		this.dl.each(function(dl){
			dl.dt.setStyle('display', 'none');
			dl.dd.setStyle('display', 'none');
		});
	},
	
	showSlide: function(index){
		this.handles[index].toggleClass('active');
		this.handles.each(function(handle, i){
			if(i != index)
				if(handle.hasClass('active'))
					handle.removeClass('active');
		}, this);
		this.slides[index].setStyle('display', '');
		this.slides.each(function(slide, i){
			if(i != index)
				slide.setStyle('display', 'none');
		});
		this.dl.each(function(dl, i){
			this.dl[index].dt.setStyle('display', '');
			this.dl[index].dd.setStyle('display', '');
			if (i != index) {
				dl.dt.setStyle('display', 'none');
				dl.dd.setStyle('display', 'none');
			}
		}, this);
		this.showing = index;
	},
	
	autoPilot: function(){
		if(this.mouseOn)
			return;
		if(this.showing == this.slides.length - 1)
			this.showSlide(0);
		else
			this.showSlide(this.showing + 1);
	}
	
});
tkSlideShow.implement(new Options, new Events);
