/*

	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.

*/
var strAnim = ""
Rotator = function (scrollableElement, itemContainer, itemTagName, buttonContainer, visibleItems, scrollDistance, initialDelay) {
	this.itemSize = scrollDistance;
	this.visibleItems = visibleItems;
	this.orientation = 0;

	this.buttons = buttonContainer.getElementsByTagName("a");
	this.currentButton = 1;
	this.buttons[this.currentButton].className = "current";
	
	this.scrollable = scrollableElement;
	this.itemContainer = itemContainer
	this.items = itemContainer.getElementsByTagName(itemTagName);
	this.totalItems = this.items.length;
	if (this.totalItems <= this.visibleItems) return;

	var rotator = this;//Needed for closures below

	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;

	EventListener.addEvent(window.document, 'mouseover', this.method(this.automatic));
	EventListener.addEvent(itemContainer, 'mouseover', function(event){rotator.manual();cancelBubble(event)})
	EventListener.addEvent(buttonContainer, 'mouseover', function(event){rotator.manual();cancelBubble(event)})
	EventListener.addEvent(this.buttons[0],"click",function () {this.blur();rotator.manual();rotator.back();return false})
	for(var i =1;i<this.buttons.length-1;i++){
		EventListener.addEvent(this.buttons[i],"click",function () {this.blur();rotator.manual();rotator.goToItem(this.name);return false})
	}
	EventListener.addEvent(this.buttons[this.buttons.length-1],"click", function () {this.blur();rotator.manual();rotator.forward();return false})
	setTimeout(this.method(this.automatic), initialDelay);
}

Rotator.prototype.setHorizontalMode = function () {this.orientation = 0}
Rotator.prototype.setVerticalMode = function () {this.orientation = 1}

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;
	this.setCurrentTab();
}

Rotator.prototype.setCurrentTab = function () {
	this.buttons[this.currentButton].className = "";
	this.currentButton = this.index;
	this.buttons[this.currentButton].className = "current";
}

Rotator.prototype.forward = function (intStep) {
	if (this.animating) return;
	if(isNaN(intStep)){intStep=1}
	if (this.orientation) this.scrollForward = new Animator(this.scrollable.scrollTop, this.scrollable.scrollTop+(intStep*this.itemSize), this.method(this.animateForward), this.method(this.checkIndex));
	else this.scrollForward = new Animator(this.scrollable.scrollLeft, this.scrollable.scrollLeft+(intStep*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.animating = this.scrollForward;
	this.index+=intStep;
	this.scrollForward.start();
}

Rotator.prototype.animateForward = function (value) {
	if (this.orientation) this.scrollable.scrollTop = value;
	else this.scrollable.scrollLeft = value;
}

Rotator.prototype.back = function (intStep) {
	if (this.animating) return;
	if(isNaN(intStep)){intStep=-1}
	if (this.orientation) this.scrollBack = new Animator((this.scrollable.offsetHeight-this.scrollable.scrollTop), (this.scrollable.offsetHeight-this.scrollable.scrollTop-intStep*this.itemSize), this.method(this.animateBack), this.method(this.checkIndex));
	else this.scrollBack = new Animator((this.scrollable.offsetWidth-this.scrollable.scrollLeft), (this.scrollable.offsetWidth-this.scrollable.scrollLeft-intStep*this.itemSize), this.method(this.animateBack), this.method(this.checkIndex));
	this.scrollBack.setStep(3);
	this.scrollBack.setType(this.scrollBack.EASEINOUT);
	this.scrollBack.setRate(20);
	this.animating = this.scrollBack;
	this.index+=intStep;
	this.scrollBack.start();
}
Rotator.prototype.animateBack = function (value) {
	if (this.orientation) this.scrollable.scrollTop = this.scrollable.offsetHeight - value;
	else this.scrollable.scrollLeft = this.scrollable.offsetWidth - value;
}

Rotator.prototype.automatic = function () {
	if(!this.auto){
		//this.forward();
		this.auto = setInterval(this.method(this.forward), 4000,1);
	}
}

Rotator.prototype.manual = function () {
	if (this.auto) {
		clearInterval(this.auto);
		this.auto = null;
	}
}

Rotator.prototype.goToItem = function(itemIndex) {
	var diff = itemIndex-this.index;
	if(diff<0){this.back(diff)}
	if(diff>0){this.forward(diff)}
	return false;
}

function cancelBubble(e) {
	if (!e) var e = window.event;
	e.cancelBubble = true;
	if (e.stopPropagation) e.stopPropagation();
}
