﻿var Slideshow = Class.create();
Slideshow.prototype = {
    initialize: function(slideshow, vars, callback, callbackParams) {
        // Get the slides
        this.slides = [];
        var nl = $(slideshow).getElementsByTagName('div');
        for (var i = 0; i < nl.length; i++) {
            if (Element.hasClassName(nl[i], 'slide')) {
                this.slides.push(nl[i]);
                //Element.setOpacity(nl[i], 0);
            }
        }
        // Change the z-index
        for (var i = 0; i < this.slides.length; i++) {
            this.slides[i].style.zIndex = this.slides.length - i;
        }

        // Set the current slide
        this.current = 0;
        //Element.setOpacity(this.slides[this.current], 1);

        this.callback = callback;
        this.callbackParams = callbackParams;
        this.vars = vars;
        
        this.playing = true;

        $(slideshow).style.visibility = 'visible';
        this.callback(this.current, this.vars, this.callbackParams);
        Element.show(slideshow);
        setTimeout((function(){this.next();}).bind(this), this.vars[this.current].get('d') * 1000);
    },
    togglePause: function() {
        if (this.playing) {
            clearTimeout(this.timeout);
            this.playing = false;
        } else {
            this.timeout = setTimeout((function(){this.next();}).bind(this), this.vars[this.current].get('d') * 1000);
            this.playing = true;
        }
    },
    next: function() {
        clearTimeout(this.timeout);
        this.reorder();
        Effect.Fade(this.slides[this.current], {
            afterFinish: function(effect) {
                effect.element.style.zIndex = 0;
                Element.show(effect.element);
                Element.setOpacity(effect.element, 1);
            }
        });
        this.current = (this.current + 1) % this.slides.length;
        this.callback(this.current, this.vars, this.callbackParams);
        this.timeout = setTimeout((function(){this.next();}).bind(this), this.vars[this.current].get('d') * 1000);
    },
    manualNext: function() {
        this.moveTo(1);
    },
    manualPrev: function() {
        this.moveTo(this.slides.length - 1);
    },
    moveTo: function(offset) {
        clearTimeout(this.timeout);
        this.current = (this.current + offset) % this.slides.length;
        this.reorder();
        this.callback(this.current, this.vars, this.callbackParams);
        if (this.playing) {
            this.timeout = setTimeout((function(){this.next();}).bind(this), this.vars[this.current].get('d') * 1000);
        }
    },
    reorder: function() {
        for (var i = 0; i < this.slides.length; i++) {
            var slide = this.slides[(this.current + i) % this.slides.length];
            slide.style.zIndex = this.slides.length - i;
        }
    }
};

function slideshowDG(current, vars, params) {
    var title = $(params.get('title'));
    var country = $(params.get('country'));
    title.innerHTML = vars[current].get('title');
    country.innerHTML = '<strong>' + vars[current].get('city') + ', </strong>' + vars[current].get('country');
}

function slideshowHome(current, vars, params) {
    var title = $(params.get('title'));
    var subtitle = $(params.get('subtitle'));
    var text = $(params.get('text'));
    title.innerHTML = vars[current].get('title');
    subtitle.innerHTML = vars[current].get('subtitle');
    text.innerHTML = vars[current].get('text');
    text.href = vars[current].get('url');
}

function slideshowPartner(current, vars, params) {
    var title = $(params.get('title'));
    title.innerHTML = vars[current].get('title');
}

function nullCallback(current, vars, params) {
    // Do nothing
}