﻿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;
        if (this.container) {
            this.setUpControls();
            this.start();
            //Event.observe(window, 'load', this.setUpControls.bind(this));
            //Event.observe(window, 'load', this.start.bind(this));
        }
    },
    setUpControls: function() {
        var _self = this;
        this.elements = $(this.container).select(".slide_item");
        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 = 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);
            this.controls = controls;
        }
    },
    start: function() {
        var _self = this;
        this.slideShowInterval = setInterval(function() { _self.slideShow() }, (this.opts.delay));
    },
    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 });
        if ($(this.controls)) {
            $(this.controls).select('li')[this.count].removeClassName('selected');
        }
    },
    fadeIn: function() {
        Effect.Appear(this.elements[this.count], { duration: 1, from: 0.0, to: 1.0 });
        if ($(this.controls)) {
            $(this.controls).select('li')[this.count].addClassName('selected');
        }
    },
    nextCount: function() {
        this.count++;
        if (this.count == this.elements.length) {
            this.count = 0;
            this.pause();
            this.start();
        }
    },
    prevCount: function() {
        if (this.count == 0) {
            this.count = this.elements.length - 1;
        } else {
            this.count--;
        }
    },
    slideShow: function() {
        this.fadeOut();
        this.nextCount();
        this.fadeIn();
    }
});