function Switcher(elements, interval) {
    this.elements = new Array();
    for (var i = 0; i < elements.length; i++) {
        this.elements[i] = document.getElementById(elements[i]);
        if (i != 0) this.setTransparent(0, this.elements[i]);
    }
    this.old = null;
    this.step = 0;        //current state of fading
    this.stepSize = 1;
    this.fadeTime = 1000;
    this.paused = new Boolean(false);
    this.fadeTimeout = null;
    this.stepTimeout = null;
    this.stepInterval = interval;
    if (elements.length != 0) {
        this.current = 0;
        if (elements.length > 1) this.timedLoopStep();
    }
    else this.current = null;
}

function SwitcherSetTransparent(transparent, element) {
    if (transparent == 0) element.style.display = "none";
    else element.style.display = "block";
    try {
        if (element.style.filter == null) throw new Exception();
        element.style.filter = "alpha(opacity=" + transparent + ")";
        return 1;
    }
    catch (e) {
        element.style.opacity = transparent / 100;
        return 1;
    }
}

function SwitcherFadeStep() {
    if (this.step > 100) return;
    this.setTransparent(100 - this.step, this.elements[this.old]);
    var modifier = this.setTransparent(this.step, this.elements[this.current]);
    this.step += this.stepSize;
    var element = this;
    clearTimeout(this.fadeTimeout);
    this.fadeTimeout = setTimeout(function() { element.fadeStep(); }, this.fadeTime / 100 * this.stepSize * modifier);
}

//sets element number `number' as the current one, turns starts fading procedure
function SwitcherSetElement(number) {
    if (number < 0 || number > this.elements.length) return;
    this.old = this.current;
    this.current = number;
    this.step = 0;
    this.fadeStep();
}

function SwitcherTimedLoopStep() {
    var element = this;
    clearTimeout(this.stepTimeout);
    this.stepTimeout = setTimeout(function() { element.loopStep(); }, this.stepInterval + this.fadeTime);
}

function SwitcherLoopStep() {
    if (this.paused == true) return;
    var number = this.current + 1;
    if (number >= this.elements.length) number = 0;
    this.setElement(number);
    this.timedLoopStep();
}

function SwitcherPause() {
    this.paused = true;
}

function SwitcherResume() {
    this.paused = false;
    this.timedLoopStep();
}

function SwitcherSwitchPause() {
    if (this.paused == true) this.resume();
    else this.pause();
}

Switcher.prototype.setTransparent = SwitcherSetTransparent;
Switcher.prototype.fadeStep = SwitcherFadeStep;
Switcher.prototype.setElement = SwitcherSetElement;
Switcher.prototype.timedLoopStep = SwitcherTimedLoopStep;
Switcher.prototype.loopStep = SwitcherLoopStep;
Switcher.prototype.pause = SwitcherPause;
Switcher.prototype.resume = SwitcherResume;
Switcher.prototype.switchPause = SwitcherSwitchPause;

