/*
Script: ImageStrip.js
	Extends the MooTools Scroller class to make a looping strip

	Authors:
		Anthony McLin
*/

var ImageStrip = new Class({

	Implements: [Scroller],
	
	options: {
	},

	wrap: function(el) {
		//Push the selected element to the opposite end of the list
		el.inject(this.imageList.getLast(),'after');
		//Re-Cache the image list
		this.imageList = this.tray.getChildren();
		//Reset the strip scroll position
		this.fireEvent('change', 0,0);
	},
	
	//Modified start function to make sure the scrolling starts as soon as the page loads
	start: function(){
		this.listener.addEvents({
			mouseenter: this.bound.detach,
			mouseleave: this.bound.attach
		});
		//Start the motion
		if (!this.timer) this.timer = this.scroll.periodical(Math.round(1000 / this.options.fps), this);
		//Cache the image list
		this.tray = this.element.getFirst();
		this.imageList = this.tray.getChildren();
	},
	
	//Reverse the mouseover/mouseleave behavior
	detach: function(){
		//stop the motion
		this.timer = $clear(this.timer);
	},
	//Reverse the mouseover/mouseleave behavior
	attach: function(){
		//Start the motion
		if (!this.timer) this.timer = this.scroll.periodical(Math.round(1000 / this.options.fps), this);
	},

	//Modified scrollbehavior to not be dependent on zones of scrolling
	scroll: function(){
		var scroll = this.element.getScroll(),
			firstSize = this.imageList[0].getSize(),
			change = {x: this.options.velocity, y: 0}; //Force change to happen irregardless of "hover areas"
		
		//Check for the need to wrap
		if(scroll.x > firstSize.x || scroll.y > firstSize.y) {
			this.wrap(this.imageList[0]);
		} else {
			this.fireEvent('change', [scroll.x + change.x, scroll.y + change.y]);
		}
	}

	
});
