// JavaScript Document
this.itemHeight = 155;
this.totalHeight = 284;
/*
this.itemHeight = 170;
this.totalHeight = 300;
*/

Rotator = function (rotatorID) {
	this.visibleItems = 3;

	this.rotator = document.getElementById(rotatorID);
	this.area = document.getElementById("scrollarea");
	this.items = this.area.getElementsByTagName("div");
	this.total = this.items.length;
	if (this.total < this.visibleItems) return;

	document.getElementById("rotatorup").rotator = this;
	document.getElementById("rotatorup").onclick = function () {this.rotator.manual();this.rotator.down()}
	document.getElementById("rotatorup").onmouseover = function () {this.className = "over";}
	document.getElementById("rotatorup").onmouseout = function () {this.className = "";}

	document.getElementById("rotatordown").rotator = this;
	document.getElementById("rotatordown").onclick = function () {this.rotator.manual();this.rotator.up()}
	document.getElementById("rotatordown").onmouseover = function () {this.className = "over";}
	document.getElementById("rotatordown").onmouseout = function () {this.className = "";}

	var bottomClones = new Array(this.visibleItems);
	var topClones = new Array(this.visibleItems);
	/* Duplicate head and tail of the newsitem list */
	for (var i = 0; i < this.visibleItems; i++) {
		bottomClones[i] = this.items[this.total - 1 - i].cloneNode(true);
		topClones[i] = this.items[i].cloneNode(true);
	}
	/* Grow the list */
	for (var i = 0; i < this.visibleItems; i++) {
		this.area.insertBefore(bottomClones[i], this.items[0]);//items is dynamic, so items[0] always refers to first item
		this.area.appendChild(topClones[i]);
	}

	this.index = 0;
	this.area.scrollTop = this.index*itemHeight+totalHeight;
	this.scrollUp = new Animator(0, itemHeight, createContextFunction(this, "animateUp"));
	this.scrollDown = new Animator(0, itemHeight, createContextFunction(this, "animateDown"));

	this.auto = setTimeout(createContextFunction(this, "automatic"), 1500);
}
Rotator.prototype.automatic = function () {
	this.up();
	if (this.restart) clearInterval(this.restart);
	this.restart = null;
	this.auto = setInterval(createContextFunction(this, "up"), 4000);
}
Rotator.prototype.manual = function () {
	if (this.auto) clearInterval(this.auto);
	this.auto = null;
	if (this.restart) clearInterval(this.restart);
	this.restart = setTimeout(createContextFunction(this, "automatic"), 7000);
}
Rotator.prototype.up = function () {
	this.index++;
	if (this.index == this.total) this.index = 0;
	this.scrollUp.start();
}
Rotator.prototype.animateUp = function (value) {
	this.area.scrollTop = (this.index-1)*itemHeight + value + totalHeight;
}
Rotator.prototype.down = function () {
	this.index--;
	if (this.index == -this.visibleItems) this.index = this.total - this.visibleItems;
	this.scrollDown.start();
}
Rotator.prototype.animateDown = function (value) {
	this.area.scrollTop = (this.index+1)*itemHeight - value + totalHeight;
}
function createContextFunction(context, method) {
	return (function(){
		eval("context."+method+"(arguments[0],arguments[1],arguments[2])");
		return false;
    });
}
