/**
 * abSlider v 0.1
 * @uses: MooTools 1.2
 * Abdellah abdellah@3wkom.net
 * Original Script:  Showcase v1.0
 * @author: Constantin Boiangiu ( constantin.b[at]gmail.com )
 * Updated to work with mootools 1.2
 * Various mods and tweaks (default values, test cases...)
 */
var abSlider = new Class({
    Implements: [Options],
    options: {
        outer: 'abOuter',
        inner: 'abInner',
        slidesContainer: 'abSlides',
        itemsSelector: '.abSlide',
        elemsSlide: 1,
        duration: 500,
        direction: 1,
        autoSlide: 5000,
        itemWidth: null,
        itemsVisible: null,
        navigationContainer: null,
        navigationElem: null
    },
    initialize: function(options){
        this.setOptions(options);
		if($type($(this.options.slidesContainer)) != 'element') return;
        this.elements = $(this.options.slidesContainer).getElements(this.options.itemsSelector);
        this.totalElements = this.elements.length;
        if (this.totalElements == 0) 
            return;
        if (this.options.itemsVisible == null) 
            this.options.itemsVisible = this.elements.count;
        if (this.totalElements <= this.options.itemsVisible) {
            return
        }
        this.elementWidth = this.options.itemWidth || this.elements[0].getSize().x;
        this.currentElement = 0;
        this.direction = this.options.direction;
        this.autoSlideTotal = this.options.autoSlide + this.options.duration;
        if (this.options.elemsSlide == 1) {
            this.options.elemsSlide = null
        }
        this.start()
    },
    start: function(){
        this.setSizes();
        this.myFx = new Fx.Morph(this.options.slidesContainer, {
            wait: false,
            transition: Fx.Transitions.easeInOut,
            duration: this.options.duration
        });
        if (this.options.autoSlide) {
            this.autoStart()
        }
        if (this.options.navigationContainer != null) {
            this.navs = $(this.options.navigationContainer).getElements(this.options.navigationElem);
            this.navs.each(function(el, i){
                if (i == this.currentElement) {
                    el.addClass("selected")
                }
                el.addEvent("click", function(c){
                    new Event(c).stop();
                    if (this.endingElem) {
                        return
                    }
                    if (this.options.autoSlide) {
                        $clear(this.autoSlide);
                        this.autoSlide = this.startIt.periodical(this.autoSlideTotal, this)
                    }
                    if (i == this.currentElement) {
                        return
                    }
                    var d = i < this.currentElement ? -1 : 1;
                    this.direction = d;
                    this.options.elemsSlide = (i - this.currentElement) * d;
                    this.slide(d)
                }.bind(this))
            }.bind(this))
        }
    },
    setSizes: function(){
        $(this.options.outer).set({
            styles: {
                width: this.options.itemsVisible * this.elementWidth
            }
        });
        $(this.options.inner).set({
            styles: {
                width: this.options.itemsVisible * this.elementWidth
            }
        });
        $(this.options.slidesContainer).set({
            styles: {
                width: this.totalElements * (this.elementWidth ) + 100
            }
        })
    },
    slide: function(current){
        if (this.started) {
            return
        }
        this.direction = current;
        var a = this.currentIndex();
        if (this.options.elemsSlide && this.options.elemsSlide >= 1 && this.endingElem == null) {
            this.endingElem = this.currentElement;
            for (var b = 0; b < this.options.elemsSlide; b++) {
                this.endingElem += current;
                if (this.endingElem >= this.totalElements) {
                    this.endingElem = 0
                }
                if (this.endingElem < 0) {
                    this.endingElem = this.totalElements - 1
                }
            }
        }
        if (this.direction == -1) {
            this.rearange();
            $(this.options.slidesContainer).setStyle("margin-left", -this.elementWidth)
        }
        this.started = true;
        this.myFx.start({
            "margin-left": this.direction == 1 ? -this.elementWidth : 0
        }).chain(function(){
            this.rearange(true);
            if (this.options.elemsSlide) {
                if (this.endingElem !== this.currentElement) {
                    this.slide(this.direction)
                }
                else {
                    this.endingElem = null
                }
            }
            if (this.options.navigationContainer != null) 
                this.highlightNavs();
            this.options.elemsSlide = 1
        }
.bind(this))
    },
    rearange: function(state){
        if (state) {
            this.started = false
        }
        if (state && this.direction == -1) {
            return
        }
        this.currentElement = this.currentIndex(this.direction);
        $(this.options.slidesContainer).setStyle("margin-left", 0);
        if (this.currentElement == 1 && this.direction == 1) {
            this.elements[0].injectAfter(this.elements[this.totalElements - 1]);
            return
        }
        if ((this.currentElement == 0 && this.direction == 1) || (this.direction == -1 && this.currentElement == this.totalElements - 1)) {
            this.rearrangeElement(this.elements.getLast(), this.direction == 1 ? this.elements[this.totalElements - 2] : this.elements[0]);
            return
        }
        if (this.direction == 1) {
            this.rearrangeElement(this.elements[this.currentElement - 1], this.elements[this.currentElement - 2])
        }
        else {
            this.rearrangeElement(this.elements[this.currentElement], this.elements[this.currentElement + 1])
        }
    },
    rearrangeElement: function(item1, item2){
        this.direction == 1 ? item1.injectAfter(item2) : item1.injectBefore(item2)
    },
    currentIndex: function(){
        var a = null;
        switch (this.direction) {
            case 1:
                a = this.currentElement >= this.totalElements - 1 ? 0 : this.currentElement + this.direction;
                break;
            case -1:
                a = this.currentElement == 0 ? this.totalElements - 1 : this.currentElement + this.direction;
                break
        }
        return a
    },
    autoStart: function(){
        this.startIt = this.slide.bind(this).pass(this.direction || 1);
        this.autoSlide = this.startIt.periodical(this.autoSlideTotal, this);
        this.elements.addEvents({
            mouseover: function(){
                $clear(this.autoSlide)
            }
.bind(this)            ,
            mouseout: function(){
                this.autoSlide = this.startIt.periodical(this.autoSlideTotal, this)
            }
.bind(this)
        })
    },
    highlightNavs: function(){
        this.navs.removeClass("selected");
        this.navs[this.currentElement].addClass("selected")
    }
});
