﻿/*

	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.

*/

Animator = function (startValue, endValue, f, t, r) {
    this.step  = 10;            //The step by which timer counts from 0 to 100
    this.rate  = 10;        //The rate in ms at which steps are taken
    this.power = 2;         //The steepness of the animation curve
    this.type  = this.NONE; //The type of easing

    this.min = startValue;
    this.max = endValue;
    this.isReversing = false;
    this.animateFunction = f;
    this.terminateFunction = t;
    this.reversalFunction = r;

    this.precalc = new Array(100);
    for (var i=0; i <= 100; i++) this.precalc[i] = this.calculate(i);
}
Animator.prototype.NONE      = 0;
Animator.prototype.EASEIN    = 1;
Animator.prototype.EASEOUT   = 2;
Animator.prototype.EASEINOUT = 3;

Animator.prototype.setStep = function (step) {
    this.step = step;
}
Animator.prototype.setRate = function (rate) {
    this.rate = rate;
}
Animator.prototype.setPower = function (power) {
    this.power = power;
    for (var i=0; i <= 100; i++) this.precalc[i] = this.calculate(i);
}
Animator.prototype.setType = function (type) {
    this.type = type;
    for (var i=0; i <= 100; i++) this.precalc[i] = this.calculate(i);
}
Animator.prototype.setAnimateFunction = function (f) {
    this.animateFunction = f;
}
Animator.prototype.start = function () {
    if (this.interval) return;
    this.time = 0;
    this.isReversing = false;
    this.interval = setInterval(this.method(this.animate), this.rate);
}
Animator.prototype.reverse = function () {
    this.isReversing = (this.isReversing) ? false : true;
}
Animator.prototype.stop = function () {
    if (this.interval) clearInterval(this.interval);
    this.interval = null;
}
Animator.prototype.pause = function () {
    //this.paused = true;
    if (this.interval) clearInterval(this.interval);
}
Animator.prototype.resume = function () {
    //this.paused = false;
    this.interval = setInterval(this.method(this.animate), this.rate);
}
Animator.prototype.isAnimating = function () {
    return Boolean(this.interval);
}
Animator.prototype.animate = function () {
    if (this.time >= 0 && this.time <= 100) {
        this.animateFunction(this.precalc[this.time]);
        this.time = (this.isReversing) ? this.time - this.step : this.time + this.step;
    } else {
        clearInterval(this.interval);
        this.interval = null;
        if (!this.isReversing && this.terminateFunction) this.terminateFunction();
        if (this.isReversing && this.reversalFunction) this.reversalFunction();
    }
}
Animator.prototype.calculate = function (t) {
    t = t/100;
    var factor;
    switch (this.type) {
        case this.NONE      : factor = this.easeNone(t);break;
        case this.EASEIN    : factor = this.easeIn(t);break;
        case this.EASEOUT   : factor = this.easeOut(t);break;
        case this.EASEINOUT : factor = this.easeInOut(t);break;
    }
    return parseInt(this.min + factor*(this.max-this.min));
}
Animator.prototype.easeNone = function (t) {
    return t;
}
Animator.prototype.easeIn = function (t) {
    return Math.pow(t,this.power);
}
Animator.prototype.easeOut = function (t) {
    return 1-this.easeIn(1-t);
}
Animator.prototype.easeInOut = function (t) {
    if (t < 0.5) return this.easeIn(t*2)/2;
    return 0.5+this.easeOut((t-0.5)*2)/2;
}

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), 5000,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();
}

Object.prototype.method = function (method) {
	var context = this;
	return function () {
		method.apply(context, arguments);
		return false;
	}
}
function isChild (ancestor, candidate) {
	while (candidate && candidate != ancestor.parentNode) {
		if (candidate == ancestor) return true;
		try {
			candidate = candidate.parentNode;
		} catch (c) {return false}
	}
	return false;
}

