/*Scroller
Takes a list of items, places into divs, animates and destroys

Dependencies
Yahoo.util.Dom, Yahoo.util.Motion
*/

function Scroller(elements, container) {
	this.activeElements = new Array();
	this.elements = new Array(); //holds the div elements to be scrolled
	this.container = container; //Container of items to be placed.
	this.interval = 500;
	this.scrollerTaskID = null;
	var YDOM = YAHOO.util.Dom;
	
	var loop = this; //Making reference to "this" on interval calls.
	/*Function for checking each frame.
	Because this is called from an interval, it needs to be defined in the function
	And "this" is referred to as a 3rd party (loop = this)*/
	this.checkFrame = function() {
		var lastIndex = loop.activeElements.length - 1;
		if (lastIndex < 0) {	
			loop.addNewActive();
		} else {
			var div = loop.activeElements[lastIndex];
			var divY = YDOM.getY(div);
			var divHeight = div.offsetHeight;
			var containerY = YDOM.getY(loop.container);
			var containerHeight = loop.container.offsetHeight;
			var limit = containerY + containerHeight;
			//Basically to keep contents from running into each other or being too spread apart.
			if ((divY + divHeight) <= limit) {
				loop.addNewActive();
			}
		}
	}
	
	//Moves the top
	this.addNewActive = function() {
		if (!this.elements.length) {
			return;
		}
		var el = this.elements.shift();
		var YDOM = YAHOO.util.Dom;
		if (!el) { //Error
			loop.end();
			return;
		}
		this.activeElements.push(el);
		var posX = 0;
		var posY = loop.container.offsetHeight;
		if (el.parentNode != this.container) {
			this.container.appendChild(el);
		}
		var containerX = YDOM.getX(loop.container);
		var containerY = YDOM.getY(loop.container);
		YDOM.setY(el, containerY + container.offsetHeight);
		var stopY = containerY - (el.offsetHeight);
		var elX = YDOM.getX(el);
		var elY = YDOM.getY(el);
		var attributes = {
      points: {
								from: [elX, elY],
			          to: [elX, stopY]
							}
    };
		var anim = new YAHOO.util.Motion(el, attributes, Scroller.speed);
		anim.onComplete.subscribe(function() {loop.remove(el)});
		anim.animate();
		
	}
	
	this.init(elements, container);
}

Scroller.speed = 10;

Scroller.prototype = {
	//Builds div tags and insert it into this.elements (defined in init)
	buildElementList: function(elements) {
		for (var i = 0; i < elements.length; i++) {
			var div = document.createElement("div");
			div.id = "scrollElement" + i;
			div.className = "scrollElement";
			div.innerHTML = elements[i];
			this.elements.push(div);
		}
	},
	
	end: function() {
		clearInterval(this.scrollerTaskID);
		this.scrollerTaskID = null;
	},
	
	//Remove from active and move element back over to element property.
	remove: function(el) {
		var findID = el.id;
		for (i = 0; i < this.activeElements.length; i++) {
			var thisEl = this.activeElements[i];
			if (thisEl.id == findID) {
				this.activeElements.splice(i, 1);
			}
		}
		this.elements.push(el);
	},
	
	start: function() {
		if (this.scrollerTaskID) {
			this.end();
		}
		this.scrollerTaskID = setInterval(this.checkFrame, this.interval);
	},
	
	init: function(elements, container) {		
		//Set the element list (expected array)
		this.buildElementList(elements);
	}
}
