/**
 * @author Kevin Thompson
 * @version 1.0.1
 * @classDescription Takes several images and slides them into an area.
 * @copyright 2007 theAutomaters
 * 
 * Usage:
 * 
 *  var options = {
 * 	      startPos: {x:'0px',y:'0px'},
 *		  moveBy: {x:0,y:-450},
 *		  wait: 3000
 *	};
 *  
 *  new imageSlider(elements,options);
 * 
 */

var imageSlider = Class.create();
imageSlider.prototype = {
	initialize: function(elements,options){
		this.elements = $(elements);
		this.elements.each(function(el){
			new Draggable(el,{revert:true});	
		});
		
		this.options = {startPos: {x:0,y:0},moveBy: {x:100,y:100},wait: 3000};
		
		Object.extend(this.options, options || {});
			
		this.activeElementIndex = -1;
		this.resetElements();
		this.start();						
	},
	resetElements: function(){
		//called after the previous effect is complete
		for(var i=0;i<this.elements.length;i++){
			if(i!=this.activeElementIndex){
				this.elements[i].setStyle({top: this.options.startPos.y, left: this.options.startPos.x, zIndex:0});						
			}else{
				this.elements[i].setStyle({zIndex:0});
			}										
			
		}
	},
	start: function(){
		var el = this.getNextElement();		
		if(this._canControl(false)==true){
			new Effect.Move(el,{x: this.options.moveBy.x, y: this.options.moveBy.y});	
		}else{
			new Effect.Move(el,{x: this.options.moveBy.x, y: this.options.moveBy.y, afterFinish:  this.afterMove.bind(this)});			
		}						
	},
	_canControl:function(displayAlert){
		if(this.options.wait==-1){
			return true;
		}else{
			if(displayAlert) alert("previous and next controls are not available unless the 'wait' option is '-1'");
			return false;
		}
	},
	next: function(){
		if(!this._canControl(true)) return;
		this.resetElements()
		var el = this.getNextElement();
		new Effect.Move(el,{x: this.options.moveBy.x, y: this.options.moveBy.y});
	},
	prev:function(){
		if(!this._canControl(true)) return;
		this.resetElements()
		var el = this.getPrevElement();
		new Effect.Move(el,{x: this.options.moveBy.x, y: this.options.moveBy.y});	
	},
	getPrevElement: function(){
		this.activeElementIndex--;
		if(this.activeElementIndex == -1){
			this.activeElementIndex = this.elements.length - 1;
		}				
		var el = this.element();
		el.setStyle({zIndex:1})				
		return el;
	},
	getNextElement: function(){
		this.activeElementIndex++;
		if(this.activeElementIndex == this.elements.length){
			this.activeElementIndex = 0;
		}
		var el = this.element();
		el.setStyle({zIndex:1})				
		return el;
	},
	afterMove: function(effect){
		this.resetElements()
		setTimeout(this.start.bind(this),this.options.wait);								
	},
	element: function(){
		return this.elements[this.activeElementIndex];
	}			
};

