var dotMailerSlideShow = Class.create({
    initialize: function(sSliderTable, sPrevious, sNext, sPreviousRollover, sNextRollover, iCurrentIndex) {
        this.CurrentIndex = iCurrentIndex;
        this.SliderTable = $(sSliderTable);
        this.NextButton = $(sPrevious);
        this.PreviousButton = $(sNext);
        this.NextButton_Image = new dotMailerSlideShowButton(this.NextButton.firstDescendant(), sNextRollover);
        this.PreviousButton_Image = new dotMailerSlideShowButton(this.PreviousButton.firstDescendant(), sPreviousRollover);
        this.NextButton.observe('click', this.Next.bind(this));
        this.PreviousButton.observe('click', this.Previous.bind(this));

        this.PageCount = this.SliderTable.getElementsByTagName('td').length - 1;
        this.DisplayWidth = $(this.SliderTable.parentNode).getWidth();

        new Effect.Move(this.SliderTable, { duration: 0.0, x: -(this.CurrentIndex * this.DisplayWidth), y: 0, mode: 'relative' });

        this.SetNavigationImages();

    },
    Next: function(event) {
        Event.stop(event);
        if (!this.Transitioning && this.CurrentIndex < this.PageCount) {
            this.CurrentIndex++;
            this.Transitioning = true;
            this.ChangePage(-this.DisplayWidth);
        }
    },
    Previous: function(event) {
        Event.stop(event);
        if (!this.Transitioning && this.CurrentIndex > 0) {
            this.CurrentIndex--;
            this.ChangePage(this.DisplayWidth);
        }
    },
    ChangePage: function(MoveAmount) {
        this.Transitioning = true;
        new Effect.Move(this.SliderTable, { duration:0.5, x: MoveAmount, y: 0, mode: 'relative', afterFinish: (function() { this.Transitioning = false }).bind(this) });

        this.SetNavigationImages();
    },
    SetNavigationImages: function() {
        if (this.CurrentIndex < this.PageCount) {
            this.NextButton_Image.ShowStandard();
        } else {
            this.NextButton_Image.ShowRollover();
        }

        if (this.CurrentIndex > 0) {
            this.PreviousButton_Image.ShowStandard();
        } else {
            this.PreviousButton_Image.ShowRollover();
        }
    }
});

var dotMailerSlideShowButton = Class.create({
    initialize: function(Element, RolloverImage) {
        this.Button = $(Element);
        this.RolloverImage = RolloverImage;
        this.StandardImage = this.Button.src;
    },
    ShowStandard: function() {
        this.Button.src = this.StandardImage;
    },
    ShowRollover: function() {
        this.Button.src = this.RolloverImage;
    }
});
