/*
 * Pour le scroll de liste en mode 'spy'
 */
/**
 * Constructor
 */
function Scroller(objName, speed) {
	this.name = objName;
	this.speed = speed;
	this.initList();
	this.setSpeed(this.speed);
}

/**
 * Methods
 */
Scroller.prototype = {
	name : "",
	speed : 0,
	currPos : 0,
	totalHeight : '0px',
	action : false,

	/**
	 * Get all list elements and move them.
	 */
	scrollList : function() {
		if (this.action) {
			var list = $(this.name);
			var listElements = $$('#' + list.id + ' li');
			var h = listElements[this.currPos].offsetHeight;
			for ( var i = 0; i < listElements.length; i++) {
				var currE = listElements[i];
				this.moveElement(currE, -h);
			}
			this.currPos++;
			if (this.currPos >= listElements.length) {
				this.currPos = 0;
			}
		}
	},

	/**
	 * Move up an element if it's not the first. Else put it to the end of the
	 * list. .
	 */
	moveElement : function(element, step) {
		var tempH = this.totalHeight; // create because afterFinish method
										// can't
	// used this.totalHeight ?!
	if (element.style.top.split("px")[0] <= 0) {
		new Effect.Opacity(element, {
			from : 1.0,
			to : 0,
			duration : 0.5,
			afterFinish : function() {
				element.style.top = tempH - element.offsetHeight + 'px';
				Effect.Appear(element, {
					duration : 0.5
				});
			}
		});
	} else {
		new Effect.Move(element, {
			x : 0,
			y : step,
			mode : 'relative'
		});
	}
},

/**
 * init list elements positions.
 */
initList : function() {
	var list = $(this.name);
	var elements = $$('#' + list.id + ' li');

	var pos = 0;
	for ( var i = 0; i < elements.length; i++) {
		elements[i].style.top = pos + 'px';
		pos = pos + elements[i].offsetHeight;
	}
	this.totalHeight = pos;
},

/**
 * Allow to adjust the scrolling speed speed : speed in milliseconds
 */
setSpeed : function(speed) {
	setInterval(this.name + '.scrollList()', speed);
},

/**
 * Allow to start the list scrolling
 */
startScrolling : function() {
	this.action = true;
},

/**
 * Allow to stop the list scrolling
 */
stopScrolling : function() {
	this.action = false;
}
}

// Hack to draw a correct list when Javascript is disabled
document.documentElement.className += " hasJS";