﻿//Slideshow, Copyright (c) 2008 Ross Merrigan, MIT Style License.

Direction = {
	forwards: 'forwards',
	backwards: 'backwards'
};

var Slideshow = new Class({
    Implements: [Options],

    options: {
        speed: 1000,
        delay: 2000,
        advance: Direction.forwards,
        transition: Fx.Transitions.Bounce.easeOut,
        autostart: true,
        debug: false,
        hoverWait: true,
	    buttons: {
		    previous: null,
		    next: null,
		    play:  null,
		    stop: null
	    }        
    },

    initialize: function(el, options) {
        this.setOptions(options);
        this.el = $(el);
        this.items = this.el.getElements('li');
        this.current = 0;
        this.timer = null;
        this.active = false;
        
        var height = this.el.getSize().y;
        var width = 0;
        
        this.items.each(function(li, index) {width += li.getSize().x;});
        
        this.fx = new Fx.Morph(this.el, { link: 'ignore', duration:  this.options.speed,
            transition: new Fx.Transition(this.options.transition),
		    onComplete: function() { this.move() } .bind(this)
        });       
        
        this.button();
        
        if (this.options.hoverWait) {
            this.el.addEvent('mouseenter', this.wait.bind(this));
            this.el.addEvent('mouseleave', this.restart.bind(this));            
        }
        
		this.left = this.items[this.current].getSize().x;				
		//this.el.setStyles({ position: 'absolute', top: 0, left: -this.left, width: width, height: height});		
		this.el.setStyles({ top: 0, left: -this.left, width: width, height: height});			
        this.restart();
    },   
    
    loop: function(count) {
    	this.current +=count;
        if (this.current == this.items.length) {
             this.current = 0;
        } else if (this.current < 0) {
            this.current = this.items.length - 1;
        }	
        return this.items[this.current].getSize().x;     
    },     
    
    move: function() {  
        if (this.options.advance == Direction.forwards) {
    	    this.loop(-1);      
	        this.items[this.current].inject(this.el, 'bottom');			      
	        var w = this.loop(1);		                                            
		    this.el.setStyles({ left: -(w)});        
		} 
		this.active = false;
    },
        	 
    animate: function() {
        this.active = true;
        var w = 0;
        var n = 0;
        if (this.options.advance == Direction.forwards) {
	        w =  this.loop(1);    
	        n = this.el.getStyle('left').toInt();  
	        this.fx.start({left: (n - w)}); 
	    } else {
	        n =  this.items[this.current].getSize().x; 
	        w =  this.loop(-1);  		
	        this.items[this.current].inject(this.el, 'top');	                  
	        this.el.setStyles({ left: -(w + n)});
	        this.fx.start({left: -(w)}); 
	    }	    
	    this.restart();    
    },    
    
    wait: function() {
        $clear(this.timer);
    },
    
    restart: function() {
        if (this.options.autostart)
            this.timer = this.animate.delay(this.options.delay, this);           
    },
    
    button: function() {
        if ($chk(this.options.buttons.previous)) {
            $(this.options.buttons.previous).addEvent('click', function(){
                this.options.advance = Direction.backwards;
                if (!this.options.autostart) {
                    if (!this.active) this.animate();
                 }
            }.bind(this));
        }
        
        if ($chk(this.options.buttons.next)) {
            $(this.options.buttons.next).addEvent('click', function(){
                this.options.advance = Direction.forwards;
                if (!this.options.autostart) {
                    if (!this.active) this.animate();
                }
            }.bind(this));
        }        
        
        if ($chk(this.options.buttons.play)) {
            $(this.options.buttons.play).addEvent('click', function(){
                this.options.autostart = true;
                this.restart();
            }.bind(this));
        }        
        
        if ($chk(this.options.buttons.stop)) {
            $(this.options.buttons.stop).addEvent('click', function(){
                this.options.autostart = false;
            }.bind(this));
        }            
    },
    
    log: function(text, args) {
        if (this.options.debug && window.console) console.log(text.substitute(args || {}));
    }		        
});
	    
