/*
 * Author: Scott Steil <scott.steil@imperium.ca>
 * 
 * Create a vertical carousel of images that raise in an endless loop.
 * 
 * TODO: if there are insufficient objects to scroll the carousel, the
 * system doesn't slide all the way up. For the time being, we will just 
 * require enough items to fill the parent object one object height (of 
 * the maximum height... if the objects are of different heights) past 
 * it's own height. In time, there should be two cases of solving this:
 *  1) if objects heights add to less than the parent height, disable
 *     the carousel.
 *  2) if objects heights are greater than the parent height, but less than
 *     the parent height plus the addition of the tallest object, then first
 *     duplicate the top element onto the bottom and remove after scrolling.
 *
 * TODO: add additional directions (ie, down, left, and right)
 */



var ImpCarousel = new Class({
	Implements: [Options, Events],
	
	options: {
		childType: 			'img',
		scrollTransition: 	'sine:in:out',
		scrollDuration: 	500,
		pauseDuration:		3000,
		direction:			'up'		//no other directions yet
	},
	
	initialize: function(parent, options){
		if(!$chk(parent)) {
			return false;
		}
		
		this.setOptions(options);
		this.parent = $(parent);
		
		this.parent.scrollTo(0, 0);
		this.counter = 0;
	},
	
	/**
	 * Automatically flip through the list of elements
	 */
	start: function() {
		//start the next object after the first one is done and we've had a pause
		var pause = this.options.pauseDuration + this.options.scrollDuration
		this.timer = this.next.periodical(pause, this);
	},

	/**
	 * Stop the automated flipping
	 */
	stop: function() {
		if(this.timer) {
			clearInterval(this.timer);
		}
	},
	
	/**
	 * scroll the parent element down the height of the top element, 
	 * then remove it and move it to the end of the queue of items.
	 */
	next: function() {
		this.counter++;
		var child = this.getNext();
		
		this.scroll = new Fx.Scroll(this.parent, {
			duration: this.options.scrollDuration,
			transition: this.options.scrollTransition,
			onComplete: this.recycle.pass(child, this),
			wheelStops: false
		}).start(0, child.getSize().y);
	},
	
	/**
	 * move the passed child onto the end of the list and reset the 
	 * parent by scrolling to the top.
	 */
	recycle: function(child) {
		this.parent.grab(child);
		this.parent.scrollTo(0, 0);
	},
	
	/**
	 * Return the next child to recycle in the list
	 */
	getNext: function() {
		return this.parent.getFirst(this.options.childType);
	}
});

