var Scroller = new Class({
	Implements:[Options, Events],
	options:{
		'slideTimer' : 5000,  //time between slides (1 second = 1000), a.k.a. the interval duration
		'transitionTime' : 1500, //transition time (1 second = 1000)
		'items' : [],  //Get array of elements for sliding
		'prevBtn' : null,
		'playBtn' : null, 
		'nextBtn' : null,
		'itemNum' : 0,  //initialize a variable to hold the current slide index
		'isPaused' : true
	},
	
	initialize: function(items,options){
		this.setOptions(options);	
		this.setButtons();
		this.items = items;
		
		this.itemWidth = items[0].getSize().x.toString();
		
		//Setup positions
		this.items.each(function(element, index) {
			
			//since the viewer obviously has javascript on, we can remove the 'first_item' class
			if(index == 0){
				element.removeClass('home_doc_slide_first');
				element.setStyle('left', "0");
			}
			else{
				element.setStyle('left', this.itemWidth);
			}
		
		})
		
		this.numItems = items.length;  //get number of slider items

	},
	//end setup
	
	
	
	//Slider Stuff
	slideForward: function(){ 

		//get item to slide out
		var curItem = this.items[this.options.itemNum];  

		//change index
		if(this.options.itemNum < (this.numItems - 1)){
			this.options.itemNum++; 
		}
		else{
			this.options.itemNum = 0;
		}
		
		//now get item to slide in using new index
		var newItem = this.items[this.options.itemNum];
		
		
		//set up our animation stylings for out and in motions (note:  Fx.Styles does NOT exist in moo 1.2, so we must use Fx.Morph or Fx.Tween)
		var item_in = new Fx.Morph(newItem, {
			 duration: this.options.transitionTime, 
			 transition: Fx.Transitions.Quad.easeInOut, 
			 wait:false
		});
		
		var item_out = new Fx.Morph(curItem, {
			 duration: this.options.transitionTime, 
			 transition: Fx.Transitions.Quad.easeInOut, 
			 wait:false
		});
		
		//we will set a beginning value here
		//this is so that it gives the illusion of continuous motion from one direction, even after the first cycle of items
		item_in.start({
			'left': [-this.itemWidth, 0]
		});
		
		//no beginning values needed, since we always want to push the old item out to the left
		item_out.start({
			'left': this.itemWidth
		});
		
	},
		
		
	slideBackward: function(){ 
	
		//get item to slide out
		var curItem = this.items[this.options.itemNum];  
		
		//change index for reverse movement
		if(this.options.itemNum > 0){
			this.options.itemNum--; 
		}
		else{
			this.options.itemNum = (this.numItems - 1);
		}
		
		//now get item to slide in using new index
		var newItem = this.items[this.options.itemNum];
		
		
		var item_in = new Fx.Morph(newItem, {
			 duration: this.options.transitionTime, 
			 transition: Fx.Transitions.Quad.easeInOut, 
			 wait:false
		});
		
		var item_out = new Fx.Morph(curItem, {
			 duration: this.options.transitionTime, 
			 transition: Fx.Transitions.Quad.easeInOut, 
			 wait:false
		});
		
		//we will set a beginning value here too, but this time to make it come from left to right
		item_in.start({
			'left': [this.itemWidth, 0]
		});
		
		//no beginning values needed
		item_out.start({
			'left': '-' + this.itemWidth
		});
		
	},
	//end slideBackward
		
	setButtons: function(){	
	
		//call the slider function periodically
		if(!this.options.isPaused){
			var theTimer = this.slideForward.periodical(this.options.slideTimer, this);
		}
		
		if (this.options.nextBtn){
			this.options.nextBtn.addEvent('click', /*function(){
				if(theTimer){
					$clear(theTimer);
					theTimer = this.slideForward.periodical(this.options.slideTimer);
				}*/
				this.slideBackward.bindWithEvent(this)
			/*}*/);
		}
		
		if (this.options.prevBtn){
			this.options.prevBtn.addEvent('click',/* function(){
				if(theTimer){
					$clear(theTimer);
					theTimer = this.slideForward.periodical(this.options.slideTimer);
				}*/
				this.slideForward.bindWithEvent(this)
			/*}*/);
		}
		
		if (this.options.playBtn){
			this.options.playBtn.addEvent('click', function(){
				if(theTimer){
					this.options.isPaused = 1;
					$clear(theTimer);
					this.set('html', 'play');
				}
				else{
					this.options.isPaused = 0;
					this.slideForward();
					theTimer = this.slideForward.periodical(this.options.slideTimer);
					this.set('html', 'pause');
				}
			 });
		}
	}
	
});
