slideshow = function (instance) {
    this.instance = instance;
};

slideshow.prototype = {
    start: function (element, slides, begin, timeout, autostart) {
        this.element = element;
        //document.getElementById(this.element).onLoad = this.instance+".nextDelayed()";
        this.slides = slides;
        this.buffer = new Image;
        this.timeId = null;
        this.begin = begin;
        this.counter = begin;
        this.timeout = timeout*1000;
        if (autostart) {
            this.action = 'play';
        } else {
            this.action = 'stop';
        }
        
        this.load ();
    },
    
    load: function () {
        if (this.action == "play") {
            if (document.getElementById(this.element)) {
                document.getElementById(this.element).src = this.slides[this.counter]['file'];
            } else {
                clearTimeout (this.timeId);
            }
            if (document.getElementById(this.element+"Name")) {
                document.getElementById(this.element+"Name").innerHTML = this.slides[this.counter]['name'];
            }
            if (document.getElementById(this.element+"Link")) {
                document.getElementById(this.element+"Link").href = this.slides[this.counter]['url'];
            }
        }
    },
    
    nextDelayed: function () {
        if (this.action == "play") {
            this.counter++;
            if (this.counter >= this.slides.length) {
                this.counter = 0;
            }
            this.preload ();
            this.timeId = setTimeout (this.instance+".load()", this.timeout);
        }
    },
    
    next: function () {
        if (this.action == "play") {
            this.counter++;
            if (this.counter >= this.slides.length) {
                this.counter = 0;
            }
            this.load ();
            clearTimeout (this.timeId);
        }
    },
    
    previous: function () {
        if (this.action == "play") {
            this.counter--;
            if (this.counter < 0) {
                this.counter = this.slides.length-1;
            }
            this.load ();
            clearTimeout (this.timeId);
        }
    },
    
    preload: function () {
        this.buffer.src = this.slides[this.counter]['file'];
    },

    stop: function  () {
        if (this.action != "stop") {
            this.action = "stop";
            this.counter = this.begin;
            this.load ();
            clearTimeout (this.timeId);
        }
    },
    
    pause: function () {
        if (this.action == "play") {
            this.action = "pause";
            clearTimeout (this.timeId);
        }
    },
    
    play: function () {
        if (this.action != "play") {
            this.action = "play";
            this.nextDelayed ();
        }
    },
    
    getName: function () {
        for (var i in window) {
            if (window[i] == this) {
                alert (i);
                return i;
            }
        }
        return null;
    }
}