//////////////////////////////////////////////////////////
//
//  Script: Slideshow
//  author: Craig Nelson / Classic Labs
//
//

    var MJ = MJ || {};
    
    MJ.SlideShow = function (config) {
        var that = this;
        
        // elements
        this.strip = config.strip;
        this.controls = this.strip.up(".slideshow").getElementsByClassName("control");
        this.slides = this.strip.getElementsByTagName("li");
        
        // configs
        this.currentPosition = 1;
        this.moveInProgress;
        this.timer;
        this.slideTime = config.slideTime || 4000;
        this.slideWidth = config.slideWidth;
        this.totalSlidesWidth = (this.slideWidth * (this.slides.length - 1));
        this.slideHeight = config.slideHeight;
        this.slideDuration = config.slideDuration;
        
        // methods
        this.move = function (control) {
            if (control.hasClassName("next")) {
                if (this.currentPosition == this.slides.length || this.moveInProgress == true) {
                    return;
                }
                
                this.moveNext();
            }

            if (control.hasClassName("back")) {
                if (this.currentPosition == 1 || this.moveInProgress == true) {
                    return;
                }
                
                this.moveBack();
            }
            
            return;
        } // move()
        
        this.moveNext = function () {
            new Effect.Move(this.strip, {
                x: -that.slideWidth,
                y: 0,
                duration: that.slideDuration,
                transition: Effect.Transitions.easing,
                beforeStart: function () {
                    that.moveInProgress = true;
                },
                afterFinish: function () {
                    that.currentPosition++;
                    that.moveInProgress = false;
                }
            });
            
            return;
        }
        
        this.moveBack = function (toBeginning) {
            var slideWidth = toBeginning || that.slideWidth;
            var currentPosition = (toBeginning) ? 1 : (that.currentPosition - 1);
            new Effect.Move(this.strip, {
                x: slideWidth,
                y: 0,
                duration: that.slideDuration,
                transition: Effect.Transitions.easing,
                beforeStart: function () {
                    that.moveInProgress = true;
                },
                afterFinish: function () {
                    that.currentPosition = currentPosition;
                    that.moveInProgress = false;
                }
            });
            
            return;
        }
        
        this.slideTimer = function () {
            this.timer = setInterval(function () {
                if (that.currentPosition === (that.slides.length)) {
                    that.moveBack(that.totalSlidesWidth);
                }
                else {
                    that.moveNext();
                }
            }, that.slideTime);
      
            return;
        }; // slideTimer()
        
        for (var i=0; i<this.controls.length; i++) {
            if (that.slides.length > 1) {
                this.controls[i].style.display = "block";
            }
            Event.observe(this.controls[i], 'click', function (event) {
                this.blur();
                that.move(this);
                clearInterval(that.timer); // clear timer when a slide is clicked
                that.slideTimer(); // restart timer
                event.preventDefault();
            });
        }
        
        this.slideTimer(); // start timer
    } // constructor

    // add easing method to sciptaculous effects
    Effect.Transitions.easing = function (pos) {
        return (pos==1) ? 1 : -Math.pow(2, -10 * pos) + 1;
    }
