ProspectDirector.Types.KendoExtensions.TabStripScroller = (function (options) {

    var 

        PRIVATE = {

            //---------------------------
            //--- FIELDS
            //---------------------------

            _tabStrip: null,
            _tabsContainer: null,
            _scrollerLeft: null,
            _scrollerRight: null,
            _contentWidth: null,
            _isScrolling: null,

            //---------------------------
            //--- CONSTRUCTOR
            //---------------------------

            init: function () {

                options = $.extend({
                    tabStrip: null
                }, options);

                PRIVATE._tabStrip = $(options.tabStrip);
                PRIVATE._tabsContainer = PRIVATE.get_tabStrip().children(".k-tabstrip-items");
                PRIVATE._contentWidth = PRIVATE.getContentWidth();

                PRIVATE.addStyling();
                PRIVATE.addScrollers();

                PRIVATE.get_scrollerLeft().on("mousedown", $.proxy(PRIVATE.scrollerLeft_onMouseDown, PRIVATE));
                PRIVATE.get_scrollerRight().on("mousedown", $.proxy(PRIVATE.scrollerRight_onMouseDown, PRIVATE));
                PRIVATE.get_scrollerLeft().on("mouseup", $.proxy(PRIVATE.scrollerLeft_onMouseUp, PRIVATE));
                PRIVATE.get_scrollerRight().on("mouseup", $.proxy(PRIVATE.scrollerRight_onMouseUp, PRIVATE));

                PRIVATE.update();

            },

            //---------------------------
            //--- PROPERTIES
            //---------------------------

            get_tabStrip: function () {
                return PRIVATE._tabStrip;
            },

            get_tabsContainer: function () {
                return PRIVATE._tabsContainer;
            },

            get_tabs: function () {
                return PRIVATE.get_tabsContainer().children(".k-item");
            },

            get_scrollerLeft: function () {
                return PRIVATE._scrollerLeft;
            },

            get_scrollerRight: function () {
                return PRIVATE._scrollerRight;
            },

            //---------------------------
            //--- EVENT HANDLERS
            //---------------------------

            scrollerLeft_onMouseDown: function (e) {

                PRIVATE._isScrolling = true;
                PRIVATE.tabShuffle(false, 150);

            },

            scrollerRight_onMouseDown: function (e) {

                PRIVATE._isScrolling = true;
                PRIVATE.tabShuffle(true, 150);

            },

            scrollerLeft_onMouseUp: function (e) {
                PRIVATE._isScrolling = false;
            },

            scrollerRight_onMouseUp: function (e) {
                PRIVATE._isScrolling = false;
            },

            //---------------------------
            //--- METHODS
            //---------------------------

            addStyling: function () {

                PRIVATE.get_tabStrip().css({
                    position: "relative"
                });

            },

            addScrollers: function () {

                PRIVATE._scrollerLeft = PRIVATE.get_tabStrip().append("<span class=\"k-tabscroll-left\">&nbsp;</span>").find(".k-tabscroll-left");

                PRIVATE._scrollerRight = PRIVATE.get_tabStrip().append("<span class=\"k-tabscroll-right\">&nbsp;</span>").find(".k-tabscroll-right");

                PRIVATE.get_scrollerLeft().disableTextSelection();
                PRIVATE.get_scrollerRight().disableTextSelection();

                PRIVATE.get_scrollerLeft().css({
                    cursor: "pointer"
                });

                PRIVATE.get_scrollerRight().css({
                    cursor: "pointer"
                });

            },

            update: function () {

                PRIVATE.setScrollerBehavior();

            },

            setScrollerBehavior: function () {

                if (PRIVATE.hasTabOverflow()) {
                    PRIVATE.get_scrollerLeft().show();
                    PRIVATE.get_scrollerRight().show();
                }
                else {
                    PRIVATE.get_scrollerLeft().hide();
                    PRIVATE.get_scrollerRight().hide();
                }

            },

            tabShuffle: function (scrollRight, delay) {

                setTimeout($.proxy(function () {

                    //var increment = PRIVATE._isScrolling ? 5 : 50;
                    var increment = 50;

                    if (scrollRight) {
                        PRIVATE.get_tabsContainer().scrollLeft(PRIVATE.get_tabsContainer().scrollLeft() + increment);
                    }
                    else {
                        PRIVATE.get_tabsContainer().scrollLeft(PRIVATE.get_tabsContainer().scrollLeft() - increment);
                    }

                    if (PRIVATE._isScrolling) {
                        PRIVATE.tabShuffle(scrollRight, 100);
                    }

                }, PRIVATE), delay);

            },

            getContentWidth: function () {

                var result = 0;

                PRIVATE.get_tabs().each(function (i, e) {
                    result += $(e).outerWidth(true);
                });

                return result;

            },

            hasTabOverflow: function () {
                return PRIVATE.get_tabsContainer().outerWidth() < PRIVATE._contentWidth;
            },

            scrollIntoView: function (element) {

                if (element !== undefined
                    && element !== null 
                    && element.length > 0) {
                    PRIVATE.get_tabsContainer().scrollLeft(element.position().left);
                }

            }

        },

        PUBLIC = {
            update: PRIVATE.update,
            scrollIntoView: PRIVATE.scrollIntoView
        };

    PRIVATE.init();

    return PUBLIC;

});