﻿Scroller = {
    scrolling: false,
    container: "",
    duration: 1000,
    items: 2,
    itemWidth: 0,
    init: function (container, items) {
        Scroller.container = $(container);
        if (items)
            Scroller.items = items;

        Scroller.itemWidth = Scroller.container.width();
        Scroller.container.width(Scroller.container.width() * items);

        //hide start arrow
        Scroller.bindEvents();
        Scroller.scrollEnd();
    },
    startScroll: function (direction) {
        var canScroll = true;
        switch (direction) {
            case "+":
                if ($(".left").children().attr("src").indexOf('disabled') > -1)
                    canScroll = false;
                break;
            case "-":
                if ($(".right").children().attr("src").indexOf('disabled') > -1)
                    canScroll = false;
                break;
        }
        if (canScroll && !Scroller.scrolling) {
            Scroller.scrolling = true;
            Scroller.container.animate({ left: direction + "=" + Scroller.itemWidth }, Scroller.duration, function () {
                Scroller.scrollEnd();
            });
        }
    },
    scrollEnd: function () {
        Scroller.scrolling = false;
        //check position of scroller
        if ((parseInt(Scroller.container.css("left")) - Scroller.itemWidth) + parseInt(Scroller.container.width()) <= 0) {
            $(".right").children().attr("src", $(".right").children().attr("src").replace(".gif", "-disabled.gif"));
        }
        else {
            $(".right").children().attr("src", $(".right").children().attr("src").replace("-disabled", ""));
        }

        if (parseInt(Scroller.container.css("left")) == 0) {
            $(".left").children().attr("src", $(".left").children().attr("src").replace(".gif", "-disabled.gif"));
        }
        else {
            $(".left").children().attr("src", $(".left").children().attr("src").replace("-disabled", ""));
        }
    },
    bindEvents: function () {
        $(document).ready(function () {
            $(".right").click(function (e) {
                e.preventDefault();

                Scroller.startScroll('-');
            });
            $(".left").click(function (e) {
                e.preventDefault();

                Scroller.startScroll('+');
            });
        });
    }
}
