/********
* cvhConveyor - A thing that moves things slowly to the left
* (c) 2008 Chris VandenHeuvel (fastballweb.com)
* ver. 1.01 (2008-08-13)
* requires cvhUtils (fastballweb.com/javascripts)
* Fill a container div with inline elements, call the function, and away we go 
********/
function cvhConveyor(containerID, pixelsPerSecond, FPS) {
	var that = {};
	var FPS = FPS || 24; // Set this for smoother motion / better performance
	var interval = parseInt(1000 / FPS);
	var duplicate = true;
	var holding = false;
	var increment = parseInt(pixelsPerSecond / FPS);
	increment = increment || 1;
	var shift = 0;
	var proceedTimeout;
	var container = document.getElementById(containerID);
	var allChildren = container.childNodes;
	var items = [];
	if(!container.style.position || container.style.position == 'static') { container.style.position = 'relative'; }
	if(!container.style.width) { container.style.width = '100%'; }
	container.style.overflow = 'hidden';
	container.style.whiteSpace = 'nowrap';
	// test for ids
	for (var i = 0; i < allChildren.length; i++) {
		if (allChildren[i].nodeType == 1) {
			items.push(allChildren[i]);
			if (allChildren[i].id) {
				duplicate = false;
			}
		}
	}
	if (duplicate) {
		var firstSpan = document.createElement('span');
		for (var i = 0; i < items.length; i++) {
			firstSpan.appendChild(container.removeChild(items[i]));
		}
		var secondSpan = firstSpan.cloneNode(true);
		container.appendChild(firstSpan);
		container.appendChild(secondSpan);
		items = [firstSpan, secondSpan];
	}
	for (var i = 0; i < items.length; i++) {
		var item = items[i];
		item.style.position = 'relative';
		item.style.display = 'inline';
		item.style.left = 0;
	}
	that.proceed = function() {
		if (!holding) {
			shift += increment;
			if (shift > items[0].offsetWidth) {
				flip();	
			} else {
				updatePositions();
			}
		}
		proceedTimeout = window.setTimeout(that.proceed, interval);
	}
	var flip = function() {
		var removed = items.splice(0, 1);
		removed = removed[0];
		var holeSize = removed.offsetWidth;
		shift -= holeSize;
		container.appendChild(container.removeChild(removed));
		items.push(removed);
		updatePositions();
	}
	var updatePositions = function() {
		for (var i = 0; i < items.length; i++) {
			items[i].style.left = '-'+shift+'px';
		}
	}
	that.hold = function(e) {
		if (cvhUtils.checkMouseMove(e, container)) {
			holding = true;
		}
	}
	that.release = function(e) {
		if (cvhUtils.checkMouseMove(e, container)) {
			holding = false;
		}
	}
	if (items.length > 0) {
		container.onmouseover = that.hold;
		container.onmouseout = that.release;
		proceedTimeout = setTimeout(that.proceed, interval);
	}
}
