/*
---

name: slidEr

description: Standard Slider implementation

authors: Stéphane P. Péricat (@sppericat)

license: MIT-style license.

requires: [Core]

provides : slidEr

...
*/

/**
 * Class slidEr()
 * Standard Slider implementation
 * Author: Stephane P. Pericat
 * License: MIT
 * version: 1.2
 *
 * log:
 * 1.2: removed window event trigger, allowing multiple sliders in one page
 * 1.1: added slide width option
 * 1.0: first version
 */
var slidEr = new Class({
    Implements: [Options, Events],
    
    options: {
        'wrapper': null,
        'slides': null,
        'time': 2500,
        'direction': 'left',
        'nextButton': null,
        'previousButton': null,
    	'slideWidth': 960,
    	'sliderId': null
    },
    slides: [],
    currentItem: 0,
    timer: null,
    wrapper: null,

    initialize: function (options) {
        if(options) this.setOptions(options);
        this.wrapper = $(this.options.wrapper);
        
        if($$(this.options.slides)) {
            this.slides = $$(this.options.slides);
            $(this.options.wrapper).empty();
        }
        if(this.slides.length > 0) {
            this.slides[this.currentItem].setStyles({
                'display': 'inline-block',
                'opacity': 0
            });
            this.wrapper.grab(this.slides[this.currentItem].fade('in'));
           /* this.timer = (function() {
            	this.loop(this.options.direction);
            }.bind(this)).periodical(this.options.time);         */
        }
        
        if($(this.options.previousButton)) {
            $(this.options.previousButton).addEvent('click', function(evt) {
                evt.stop();
                this.previous();
                this.reset();
            }.bind(this));
        }
        
        if($(this.options.nextButton)) {
            $(this.options.nextButton).addEvent('click', function(evt) {
                evt.stop();
                this.next();
                this.reset();
            }.bind(this));
        }
    },
    
    loop: function(direction) {
        if(direction == 'left') {
            this.next();
        } else if(direction == 'right') {
            this.previous();
        }
    },
    
    next: function() {
        if(this.currentItem < this.slides.length - 1) {
            var next = this.currentItem + 1;
        } else {
            var next = 0;
        }
        
        this.slides[next].setStyles({
            'display': 'inline-block',
            'opacity': 0,
            'marginLeft': 0
        });
        $(this.options.wrapper).grab(this.slides[next]);
        
        var out = new Fx.Morph(this.slides[this.currentItem], {
            'duration': 5,
            'transition': Fx.Transitions.Sine.easeOut
        });
        out.start({
            'opacity': 0,
            'marginLeft': -this.options.slideWidth
        }).chain(function() {
            this.slides[next].fade('in');
            this.slides[this.currentItem].dispose();
            this.currentItem = next;
        }.bind(this));
    },
    
    previous: function() {
        if(this.currentItem == 0) {
            var previous = this.slides.length - 1;
        } else {
            var previous = this.currentItem - 1;
        }
        
        this.slides[previous].setStyles({
            'display': 'inline-block',
            'opacity': 0,
            'marginLeft': -this.options.slideWidth
        });
        $(this.options.wrapper).grab(this.slides[previous], 'top');
        
        var out = new Fx.Morph(this.slides[this.currentItem], {
            'duration': 5,
            'transition': Fx.Transitions.Sine.easeOut
        });
        out.start({
            'opacity': 0,
            'marginLeft': this.options.slideWidth
        }).chain(function() {
            this.slides[previous].setStyle('marginLeft', 0);
            this.slides[previous].fade('in');
            this.slides[this.currentItem].dispose();
            this.currentItem = previous;
        }.bind(this));
    },
    
    reset: function() {
        if(this.timer) {
            window.clearInterval(this.timer);
            this.timer = (function() {
            	this.loop(this.options.direction);
            }.bind(this)).periodical(this.options.time);
        }
    }
});
