/*
 *  Infinite list elements scroller - coded by Thibaut De Muynck 2009
 *  inspired by code from Hasten Technologies Pvt Ltd
 *
 * v20090831
 *
 * wrapper element must contain a list (<ul>) of elements to scroll
 *
 * requires the mootools library (1.2+) with FX.Scroll and Fx.Scroll2.js correction
 *
 */

var iScroller = new Class ({
	Implements: [Events, Options],
	
	options: {
		'wrapper':		"",		// wrapper element's id
		'direction':	'up',	// up (or down not yet available)
		'transition':	Fx.Transitions.Quad.easeInOut,
		'speed:':		false,	// if this value is defined, Linear transition will be forced
		'pause':		2500,	// time between scrolls (must be higher than 'time' option)
		'time':			2000	// scrolling animation duration
		},
		
	initialize: function(options) {
		// set options
		this.setOptions(options);
		// identify main ul element
		this.list = $(this.options.wrapper).getElement('ul');
		// get list of li elements in wrapper
		this.el_list = $$('#'+this.options.wrapper+' li');
		// instantiate element effect
		if (this.options.speed) { 
			this.options.transition = Fx.Transitions.Linear;
			this.options.time = this.options.speed;
			this.options.pause = this.options.speed;
			 }
		this.scroll = new Fx.Scroll2(this.options.wrapper, {
							link:		'cancel',
							duration:	this.options.time,
							transition: this.options.transition
						});
		this.instant_scroll = new Fx.Scroll2(this.options.wrapper, {
							link:		'chain',
							duration:	0
						});
		// copy original list
		this.orig_clone = this.list.clone(true,true);
		// launch cycle
		this.pass = 1;
		this.max = this.el_list.length;
		this.scrollMe();
		this.cycle = this.scrollMe.periodical(this.options.pause, this);
		},
		
		
/* --- Methods --- */

	scrollMe: function() { /* TO BE COMPLETED (down) */
		switch (this.options.direction) {
			case 'up':
				// duplicate current element at end of list
				this.el_list.push(this.el_list[this.pass-1].clone().inject(this.list));
				if (this.pass == this.max) {
					// if last element reached prepare re-init function
					this.scroll.toElement(this.el_list[this.pass]).chain(
						function() {
							this.cycle = $clear(this.cycle);
							this.instant_scroll.toElement(this.el_list[0]);
							this.orig_clone.replaces(this.list);
							var timeoutID = this.initialize.delay(this.options.pause-this.options.time, this);
							}.bind(this)
						);
				} else {
					// scroll to next element
					this.scroll.toElement(this.el_list[this.pass]);
				}
				
				this.pass++;
				break;
			}
		}
	
	});