(function(){

    var $ = document.id;


	this.WriterSlider = new Class({
	
		Implements: [Options, Events],
	
		options:{
			start : 0,
			controlContainer: 'controls',
			slide_duration: 1000,
			slideLength: 6000
		},
	
		initialize: function(text, options){
			this.setOptions(options);
			this.buildObject($$(text));
			this.buildControls();
			this.attachControls();
		
			this.start();
		
			this.timer = this.slide.periodical(6000, this);	
		},
	
		start: function(){
			this.key = 0;
			this.total = this.slides.length;
			this.currentKey = 0;
		
			this.slides[this.key]['text'].show();
		},
	
		buildObject: function(text){
			this.slides = [];
			text.each(function(image, key){
				var slide = [];
				slide['text'] = text[key];
				this.slides.push(slide);
			}.bind(this));
		},
	
		buildControls: function(){
			this.previousButton = $('prev_writer');
			this.nextButton = $('next_writer');
		},
	
		attachControls: function(control){
			this.nextButton.addEvent('click', function(){
				this.slideTo(this.getKey('next'));
				$clear(this.timer);
				return false;
			}.bind(this));
		
			this.previousButton.addEvent('click', function(){
				this.slideTo(this.getKey('previous'));
				$clear(this.timer);
				return false;
			}.bind(this));
		},
	
		incrementKey: function(){
			if(this.key >= this.total - 1){
				this.key = 0;
			}
			else{
				this.key += 1;
			}
		},
	
		// Used to grab the key before (or maybe after.)
		getKey: function(which){
			switch(which){
				case 'previous':
					if(this.key <= 0){
						return this.total - 1;
					}else{
						return this.key - 1;
					}
				break;
			
				case 'next':
					if(this.key >= this.total - 1){
						return 0;
					}else{
						return this.key + 1;
					}
				break;
			}
		},
	
		slide: function(){
			this.incrementKey();
			var old = this.getKey('previous');
	
			this.hideSlide(old);
			this.showSlide(this.key);
		},
	
		slideTo: function(slide){
			this.key = slide;
		
			var old = this.currentKey;

			this.hideSlide(old);
			this.showSlide(this.key);
		},
	
		hideSlide: function(slide){
			var text = this.slides[slide]['text'];
		
			text.set('tween', {duration:500});
			text.fade('out');
		},
	
		showSlide: function(slide){
			var text = this.slides[slide]['text'];
		
			text.set('tween', {duration:1000});
			text.setStyle('opacity', '0').show().fade('in');
		
			this.currentKey = slide;
		}
	});
	
})();

