(function($){
    var scroll = {};

    var scrollToItem = function(elem) {
        if($(elem).length > 0 ) {
            $('.scrollItemsWrapper').scrollTo( elem, { 
                duration: 100, 
                axis: 'x'
            } );
            scroll.cur = elem;
        }else {
            console.log("Can't scroll because there was nothing there.");
        }
    };

    var initSideScroll = function() {
        try {
            scroll.numItems = $('.scrollItems .scrollItem').length;
            scroll.startIdx = scroll.numItems >= 3 ? 3 : 1;

            scroll.cur = $('.scrollItems .scrollItem:nth-child('+scroll.startIdx+')');

            if(scroll.numItems < 4) {
                $('#ScrollNext,#ScrollPrev').hide();
            }

            $('#ScrollNext').click( function(e) {
                try {
                    scroll.next = $(scroll.cur).next('.scrollItem');
                    scrollToItem(scroll.next);
                }catch(e){ console.error(e.message); }

                e.preventDefault();
            });

            $('#ScrollPrev').click( function(e) {
                try {
                    scroll.prev= $(scroll.cur).prev('.scrollItem');
                    scrollToItem(scroll.prev);
                }catch(e){ console.error(e.message); }

                e.preventDefault();
            });
        }catch(e){ console.error(e.message); }
    };

    $(document).ready( function() {
        initSideScroll();
    });
})(window.jQuery);

