﻿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();
        }
    },
    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 - 1); 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() {
        //debugger;
        var _self = this;
        this.slideShowInterval = setTimeout(function() { _self.fadeIn() }, 100);
    },
    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();
    },
    showCurtain: function() {
        Effect.Appear($('BannerCurtain'), { duration: 1, from: 0.0, to: 1.0 });
        //this.fadeOut();
    },
    fadeCurtain: function() {
        var _self = this;
        Effect.Fade($('BannerCurtain'), { duration: 1, from: 1.0, to: 0.0 });
        //this.fadeIn();
    }
    , fadeOut: function() {
        Effect.Fade(this.elements[this.count], { duration: 1, from: 1.0, to: 0.0 });
        try {
            $(this.opts.bannerCleanup[this.count]).replace("<span id='" + this.opts.bannerCleanup[this.count] + "'></span>");
        } catch (e) { }

    },
    fadeIn: function() {
        Effect.Appear(this.elements[this.count], { duration: 1, from: 0.0, to: 1.0 });
        try {
            setTimeout(this.opts.banners[this.count], 200);
        } catch (e) { }
        var _self = this;
        clearTimeout(this.slideShowInterval);
        var timeout = parseInt(this.elements[this.count].getAttribute('rel'));
        if (timeout == NaN) {
            timeout = 10000;
            alert(timeout);
        }
        this.slideShowInterval = setTimeout(function() { _self.slideShow() }, timeout);
    },
    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() {
        var _self = this;
        this.showCurtain();
        setTimeout(function() { _self.resumeSlideShow() }, 1000);
    }
    , resumeSlideShow: function() {
        this.fadeOut();
        this.nextCount();
        this.fadeCurtain();
        this.fadeIn();
    }
});