var Scroller = new Class({
	
	Implements: [Options, Events],
	
	options: {
		
		scrollerId: "content_0",
		showText: false,
		effectTransition: Fx.Transitions.Sine.easeOut,
		effectDuration: 2000,
		wait: 5000,
		allowClicks: false
		
	},
	
	bits: "",
	timer: "",
	
	initialize: function(options){
		
		this.setOptions(options);
		
		this.bits = $$("#"+this.options.scrollerId+" .galleryBit");
		this.init = true;
		
		// Give each element a Fade function
		if(this.bits.length > 0){
			
			this.bits.each(function(e){
			
				e.options = this.options; // Pass options to the div
				//e.currentValue = 

				e.fade = function(reset){
				
					var targetOpac = 0;
					var targetWidth = 0;
				
					if(this.getStyle('opacity') == 0){
					
						targetOpac = 1;
						targetWidth = 100;
					
					}
				
					var fade = new Fx.Morph(this, {duration: this.options.effectDuration, transition: this.options.effectTransition, unit: "%", onComplete:function(e){
					
						// Reset
						if(reset == true){
						
							var bits = $$("#"+e.options.scrollerId+" .galleryBit");
						
							bits.each(function(e){

								e.setStyle('opacity', 1);

							});
						
						}
					
					}}, this);
				
					fade.start({
				
						'opacity' : targetOpac
					
					});
				
				}
			
			}, this);
			
		}
		
	},
	
	run: function(){
	
		if(this.bits.length > 0){
	
			var index = -1;
		
			this.bits.each(function(e){
		
				if(e.getStyle('opacity') == 1) index++;
			
			});
		
			// Reset to top of stack
			if(this.init){
				
				this.bits[(this.bits.length-1)].fade(true);
				this.init = false;
				
			}
			else if(index == 0){
			
				this.bits[(this.bits.length-1)].fade(true);
			
			}
			// Fade
			else{
			
				this.bits[index].fade();
			
			}
		
			// Show Text
			if(this.options.showText == true){
		
				// Figure out which is the new image
				newindex = this.bits.length - 1;
				if(index < (this.bits.length) && index > 0) newindex = index - 1;
		
				var image = $(this.bits[newindex].id).getElement("a").getElement("img");
		
				n0 = image.alt.split(".");
				n1 = n0[0].split("|");
			
				// Get Slide duration
				var newDuration = this.options.effectDuration/2;
			
				// Slide out old image
				var fade = new Fx.Morph($('dHeroImageText'), {duration: newDuration, transition: this.options.effectTransition, onComplete:function(){
				
					// Replace text
					$('dHeroImageText').innerHTML="<h1>"+n1[0]+"</h1><h2>"+n1[1]+"</h2>";
				
					// Slide back in
					var fade2 = new Fx.Morph($('dHeroImageText'), {duration: newDuration, transition: this.options.effectTransition});

					fade2.start({

						'opacity' : 1

					});

				
				}});
			
				fade.start({
			
					'opacity' : 0
				
				});
			
			}
			
			// ReRun
			this.run.delay(this.options.effectDuration+this.options.wait, this);
			
		}
		
	}
	
});
