﻿$(document).ready(
            function() {
                var parentWidth = $("div.div_for_scroll").width() - 90;
                $("div.scroll_box").width(parentWidth);
                var itemsCount = $("div.mainPageCat").length;
                var itemsWidth = GetCurrentWidth(itemsCount);
                var currentItem = 0;
                var maxItem = 0;
                $("span.button_forward").click(
            function() {
                if (maxItem == 0 || maxItem > currentItem) {
                    currentItem++;
                    var currWidth = GetCurrentWidth(currentItem);

                    if (Math.abs(parseInt($("div.wrap_mainPageCats").css("marginLeft"))) + parentWidth < itemsWidth) {
                        $("span.button_back").removeClass("disabled");
                        $("div.wrap_mainPageCats").animate({ marginLeft: -currWidth }, 350);
                    }
                    else {
                        maxItem = currentItem;
                        $(this).addClass("disabled");
                    }
                }
                else
                    $(this).addClass("disabled");
            }
                );
                $("span.button_back").click(
            function() {
                if (currentItem > 0) {
                    $("span.button_forward").removeClass("disabled");
                    if (currentItem == maxItem)
                        currentItem -= 2;
                    else
                        currentItem--;
                    var currWidth = GetCurrentWidth(currentItem);

                    //if (Math.abs(parseInt($("div.wrap_mainPageCats").css("marginLeft"))) + parentWidth < itemsWidth)
                    $("div.wrap_mainPageCats").animate({ marginLeft: -currWidth }, 350);
                } else
                    $(this).addClass("disabled");
            }
                );
                function GetCurrentWidth(pos) {
                    var result = 0;
                    for (var i = 0; i < pos; i++)
                        result += $("div.wrap_mainPageCats div.mainPageCat:eq(" + i + ")").width() + 23;
                    return result;
                }
            }
            );
