﻿var SlideShow = Class.create({
    initialize: function(Container, Options) {
        if (typeof Container == "string") {
            this.container = $(Container);
        } else {
            this.container = Container;
        }
        this.opts = Options;
        this.count = 0;
        this.fullRotationCount = 0;
        this.imageCount = 0;
    },
    addImage: function(vars) {
        if (this.opts.template) {
            var tmpl = new Template(this.opts.template);
            this.container.insert(tmpl.evaluate(vars));
        }
        this.elements = $(this.container).childElements();
        this.imageCount++;
    },
    setUpControls: function() {
        var _self = this;
        var controls = $(this.container).select(".controls")[0];
        if (controls) {
            var ul = new Element('ul');
            for (var i = 0; i < this.elements.length; i++) {
                var title = i + 1;  //this.elements[i].title;
                ul.insert(
                         new Element('li').addClassName('c' + title).update(
                            new Element('a', { 'href': 'javascript:void(0);', 'title': i })
                            .observe('click', function() {
                                _self.goTo(this.title);
                            }).update(title)
                         )
                    );
            }
            controls.update(ul);
        }
    },
    start: function() {
        var _self = this;
        for (var i = 0; i < this.elements.length; i++) {
            if ( i > 0 ) this.elements[i].hide();
        }
        this.slideShowInterval = setInterval(function() { _self.slideShow() }, (this.opts.delay * 1000));
    },
    pause: function() {
        clearInterval(this.slideShowInterval);
    },
    goTo: function(elementNum) {
        this.pause();
        this.fadeOut();
        this.count = elementNum;
        this.fadeIn();
        this.start();
    },
    goToNext: function() {
        this.pause();
        this.fadeOut();
        this.nextCount();
        this.fadeIn();
        this.start();
    },
    goToPrev: function() {
        this.pause();
        this.fadeOut();
        this.prevCount();
        this.fadeIn();
        this.start();
    },
    fadeOut: function() {
        Effect.Fade(this.elements[this.count], { duration: 1, from: 1.0, to: 0.0 });
    },
    fadeIn: function() {
        Effect.Appear(this.elements[this.count], { duration: 1, from: 0.0, to: 1.0 });
    },
    nextCount: function() {
        this.count++;
        if (this.count == this.elements.length) {
            this.count = 0;
            //this.pause();
        }
    },
    prevCount: function() {
        if (this.count == 0) {
            this.count = this.elements.length - 1;
        } else {
            this.count--;
        }
    },
    slideShow: function() {
        this.fadeOut();
        this.nextCount();
        this.fadeIn();
    }
});