ProspectDirector.Web.UI.Scroll = Class.extend({

    init: function (options) {

        this.options = $.extend({
            container: null,
            position: {
                x: 0,
                y: 0
            },
            isScrolling: false
        }, options);

    },

    get_container: function () {
        return this.options.container;
    },

    scroll: function (selfCall) {

        var info = this.getScrollDirection(),
            position = null,
            that = this;

        if (selfCall === undefined) {
            this.options.isScrolling = true;
        }

        switch (info.direction) {

            case "up":
                position = this.get_container().scrollTop() - info.factor;
                break;

            case "down":
                position = this.get_container().scrollTop() + info.factor;
                break;

            case "left":
                position = this.get_container().scrollLeft() - info.factor;
                break;

            case "right":
                position = this.get_container().scrollLeft() + info.factor;
                break;

            default:
                break;

        }

        if (info.direction !== null && this.options.isScrolling) {

            if (position !== null && (info.direction == "up" || info.direction == "down")) {
                this.get_container().scrollTop(position);
            }
            else if (position !== null && (info.direction == "left" || info.direction == "right")) {
                this.get_container().scrollLeft(position);
            }

            setTimeout(function () {
                that.scroll(true)
            }, 300);

        }

    },

    stop: function () {
        this.options.isScrolling = false;
    },

    position: function (e, y) {

        if (e === undefined || e === null) {
            return this.options.position;
        }
        else if (typeof e === "number") {

            this.options.position = {
                x: e,
                y: y
            }

        }
        else {

            this.options.position = {
                x: e.x.location,
                y: e.y.location
            }

        }

    },

    boundaries: function () {

        return {
            top: this.get_container().offset().top,
            bottom: this.get_container().offset().top + this.get_container().height(),
            left: this.get_container().offset().left,
            right: this.get_container().offset().left + this.get_container().width()
        }

    },

    getScrollDirection: function () {

        var bounds = this.boundaries(),
            position = this.position();

        switch (true) {

            case bounds.top > position.y:
                return { direction: "up", factor: Math.abs((position.y - bounds.top) / 5) };
                break;

            case bounds.bottom < position.y:
                return { direction: "down", factor: Math.abs((position.y - bounds.bottom) / 5) };
                break;

            case bounds.left > position.x:
                return { direction: "left", factor: Math.abs((position.x - bounds.left) / 5) };
                break;

            case bounds.right < position.x:
                return { direction: "right", factor: Math.abs((position.x - bounds.right) / 5) };
                break;

        }

        return {
            direction: null,
            factor: null
        };

    }

});