/***************************************************************/
/* MZ Scroller (ver 2.0)                       			       */
/* Javascript scrollbar (horizontal or vertical).              */
/*                                                             */
/* Mootools 1.2.1 Required (Core, Slider)                      */
/*                                                             */
/* Copyright Maurizio Ziraldo 2009                             */
/***************************************************************/

/*
mzScroll
Main object: create the scroll.
*/

var mzScroller = new Class({

    Implements: [Options],

    options: {
        knobClass: "knobScroller",
        wrapperClass: "divScroller",
        cmdScrollbar: true,
        cmdUporLeft: null,
        cmdDownorRight: null,
        wrapperwidth: null,
        wrapperheight: null,
        speed: 100,
        step: 20,
        mode: "vertical",
        cmdType: 'click'
    },

    initialize: function(elementdiv, elementscroll, options) {

        this.setOptions(options);

        var cmdType = (this.options.cmdType == 'click') ? ['mousedown', 'mouseup'] : ['mouseenter', 'mouseleave'];

        this.content = $(elementdiv);
        this.wrapper = this.content.getElement("div[class=" + this.options.wrapperClass + "]");
        if (this.options.wrapperwidth) this.wrapper.setStyle('width', this.options.wrapperwidth + 'px');
        if (this.options.wrapperheight) this.wrapper.setStyle('height', this.options.wrapperheight + 'px');
        this.scroller = $(elementscroll);
        this.uporleft = (this.options.cmdUporLeft) ? $(this.options.cmdUporLeft) : null;
        this.downorright = (this.options.cmdDownorRight) ? $(this.options.cmdDownorRight) : null;
        this.knob = this.scroller.getElement("div[class=" + this.options.knobClass + "]");

        if (this.uporleft) {
            this.uporleft.addEvent(cmdType[0], function() {
                this.startScroll(-1);
            } .bind(this));
            this.uporleft.addEvent(cmdType[1], function() {
                this.endScroll();
            } .bind(this));
        }

        if (this.downorright) {
            this.downorright.addEvent(cmdType[0], function() {
                this.startScroll(1);
            } .bind(this));
            this.downorright.addEvent(cmdType[1], function() {
                this.endScroll();
            } .bind(this));
        }

        this.content.addEvents({
            'mousewheel': function(e) {

                if (e.wheel >= 0) {
                    this.dir = -1
                } else {
                    this.dir = 1
                }

                e = new Event(e).stop();
                this.slider.set(this.slider.step + (this.dir * this.options.step));

            } .bind(this)
        });


        this.reset();

    },


    reset: function() {

        this.necessary = (this.options.mode == 'vertical' && this.content.getScrollSize().y > this.content.getSize().y) ? true : ((this.options.mode == 'horizontal' && this.content.getScrollSize().x > this.content.getSize().x) ? true : false)

        if (this.necessary) {

            if (this.options.cmdScrollbar) this.scroller.setStyle('display', 'block');
            if (this.uporleft) this.uporleft.setStyle('display', 'block');
            if (this.downorright) this.downorright.setStyle('display', 'block');

            this.slider = new Slider(this.scroller, this.knob, {
                steps: (this.options.mode == 'vertical') ? this.content.getSize().y : this.content.getSize().x,
                mode: this.options.mode,
                onChange: function(step) {
                    if (this.content) {
                        (this.options.mode == 'vertical') ? this.content.scrollTo(this.content.getScroll().x, ((this.content.getScrollSize().y - this.content.getSize().y) / this.content.getSize().y) * step) : this.content.scrollTo(((this.content.getScrollSize().x - this.content.getSize().x) / this.content.getSize().x) * step, this.content.getScroll().y);
                    }
                } .bind(this)
            }).set(0);

            this.content.scrollTo(0, 0);

        } else {

            if (this.slider) this.slider.set(0);
            this.content.scrollTo(0, 0);
            this.scroller.setStyle('display', 'none');
            if (this.uporleft) this.uporleft.setStyle('display', 'none');
            if (this.downorright) this.downorright.setStyle('display', 'none');

        }

    },

    startScroll: function(dir) {
        if (this.aniTimer) window.clearTimeout(this.aniTimer)

        this.dir = dir
        this.slider.set(this.slider.step + (this.dir * this.options.step));

        this.aniTimer = window.setTimeout(this.scroll.bind(this), this.options.speed)
    },

    scroll: function() {
        this.aniTimer = window.setTimeout(this.scroll.bind(this), this.options.speed)

        this.slider.set(this.slider.step + (this.dir * this.options.step));
    },

    endScroll: function() {
        if (this.aniTimer) this.aniTimer = window.clearTimeout(this.aniTimer)
    },

    set: function(step) {
        if (this.slider) this.slider.set(step);
    }

});

