/*

	Lost Boys 2005.
	
	De inhoud van dit bestand is in opdracht vervaardigd en eigendom van onze opdrachtgever.
	Niet hergebruiken zonder toestemming.
	Neem voor vragen contact op met Lost Boys, www.lostboys.nl.

	The contents of this file have been produced for and are the property of our client.
	Do not reuse without permission.
	Any questions? Please contact Lost Boys, www.lostboys.nl.

*/

Rotator = function (scrollableElement, itemContainer, itemTagName, buttonContainer, visibleItems, scrollDistance, initialDelay) {
	this.itemSize = scrollDistance;
	this.visibleItems = visibleItems;
	this.orientation = 0;

	this.scrollable = scrollableElement;
	this.items = itemContainer.getElementsByTagName(itemTagName);
	this.totalItems = this.items.length;
	if (this.totalItems <= this.visibleItems) return;

	var rotator = this;//Needed for closures below

	var leftbutton = document.createElement("a");
	leftbutton.className = "leftbutton";
	leftbutton.href = "#";
	leftbutton.onclick = function () {this.blur();rotator.manual();rotator.back();return false}
	buttonContainer.appendChild(leftbutton);

	var rightbutton = document.createElement("a");
	rightbutton.className = "rightbutton";
	rightbutton.href = "#";
	rightbutton.onclick = function () {this.blur();rotator.manual();rotator.forward();return false}
	buttonContainer.appendChild(rightbutton);

	var bottomClones = new Array(this.visibleItems);
	var topClones = new Array(this.visibleItems);
	/* Duplicate head and tail of the item list */
	for (var i = 0; i < this.visibleItems; i++) {
		bottomClones[i] = this.items[this.totalItems - 1 - i].cloneNode(true);
		topClones[i] = this.items[i].cloneNode(true);
	}
	/* Grow the list */
	for (var i = 0; i < this.visibleItems; i++) {
		itemContainer.insertBefore(bottomClones[i], this.items[0]);//items is dynamic, so items[0] always refers to first item
		itemContainer.appendChild(topClones[i]);
	}

	this.index = this.visibleItems;
	if (this.orientation) this.scrollable.scrollTop = this.index*this.itemSize; else this.scrollable.scrollLeft = this.index*this.itemSize;

	this.scrollForward = new Animator(0, this.itemSize, this.method(this.animateForward), this.method(this.checkIndex));
	this.scrollForward.setStep(3);
	this.scrollForward.setType(this.scrollForward.EASEINOUT);
	this.scrollForward.setRate(20);
	this.scrollBack = new Animator(0, this.itemSize, this.method(this.animateBack), this.method(this.checkIndex));
	this.scrollBack.setStep(3);
	this.scrollBack.setType(this.scrollForward.EASEINOUT);
	this.scrollBack.setRate(20);

	itemContainer.controller = this;
	itemContainer.onmouseover = function (){this.controller.pause()};
	itemContainer.onmouseout = function (){if (!isChild(itemContainer,this) || this == itemContainer) this.controller.resume()};

	this.auto = setTimeout(this.method(this.automatic), initialDelay);
}

Rotator.prototype.setHorizontalMode = function () {this.orientation = 0}
Rotator.prototype.setVerticalMode = function () {this.orientation = 1}

Rotator.prototype.automatic = function () {
	if (this.isPaused) {
		this.restart = setTimeout(this.method(this.automatic), 5000);
		return;
	}
	this.forward();
	if (this.restart) clearInterval(this.restart);
	this.restart = null;
	this.auto = setInterval(this.method(this.forward), 14000);
}
Rotator.prototype.manual = function () {
	if (!this.auto || this.isPaused) return;
	if (this.animating) {
		this.scrollForward.stop();
		this.checkIndex();
		this.animating = false;
	}
	if (this.auto) clearInterval(this.auto);
	this.auto = null;
	if (this.restart) clearInterval(this.restart);
	this.restart = setTimeout(this.method(this.automatic), 5000);
}
Rotator.prototype.forward = function () {
	if (this.animating) return;
	this.animating = this.scrollForward;
	this.index++;
	this.scrollForward.start();
}
Rotator.prototype.animateForward = function (value) {
	if (this.orientation) this.scrollable.scrollTop = (this.index-1)*this.itemSize + value;
	else this.scrollable.scrollLeft = (this.index-1)*this.itemSize + value;
}
Rotator.prototype.back = function () {
	if (this.animating) return;
	this.animating = this.scrollBack;
	this.index--;
	this.scrollBack.start();
}
Rotator.prototype.animateBack = function (value) {
	if (this.orientation) this.scrollable.scrollTop = (this.index+1)*this.itemSize - value;
	else this.scrollable.scrollLeft = (this.index+1)*this.itemSize - value;
}
Rotator.prototype.checkIndex = function () {
	if (this.index == 0) this.index = this.totalItems;
	if (this.index == this.totalItems + this.visibleItems) this.index = this.visibleItems;
	if (this.orientation) this.scrollable.scrollTop = this.index*this.itemSize; else this.scrollable.scrollLeft = this.index*this.itemSize;
	this.animating = false;
}
Rotator.prototype.pause = function () {
	if (this.animating) {
		this.animating.pause();
	} else {
		this.manual();
	}
	this.isPaused = true;
}
Rotator.prototype.resume = function () {
	if (this.animating) {
		this.animating.resume();
	}
	this.isPaused = false;
}