$(document).ready(function () {
    var scrollPosition = 0;
    var currentslide = 1;
    var scrollWidth = $(".scroller-wrapper").width();
    var slidesQuantity = $(".scroller-slides").find("div.scroller-slide").size();
    firstNav = $(".scroller-navigation a:first");
    firstNav.addClass("active");

    //function to show next div
    scrollNext = function () {
        if (currentslide != slidesQuantity) {
            currentslide = currentslide + 1;
            scrollPosition = scrollPosition - scrollWidth;
            currentNav = $(".scroller-navigation a.active");
            $(".scroller-navigation a").removeClass("active");
            currentNav.next().addClass("active");
            $(".scroller-slides").animate({ left: scrollPosition }, 500);
        }
        else {
            scrollPosition = 0;
            currentslide = 1;
            $(".scroller-navigation a").removeClass("active");
            firstNav.addClass("active");
            $(".scroller-slides").animate({ left: scrollPosition }, 500);
        }
    }
    //function to previos next div
    scrollPrev = function () {
        if (currentslide != 1) {
            currentslide = currentslide - 1;
            currentNav = $(".scroller-navigation a.active");
            $(".scroller-navigation a").removeClass("active");
            currentNav.prev().addClass("active");
            scrollPosition = scrollPosition + scrollWidth;
            $(".scroller-slides").animate({ left: scrollPosition }, 500);
        }
        else {
            currentslide = slidesQuantity;
            scrollPosition = scrollWidth - (scrollWidth * slidesQuantity);
            $(".scroller-navigation a").removeClass("active");
            $(".scroller-navigation a:last").addClass("active");
            $(".scroller-slides").animate({ left: scrollPosition }, 500);
        }
    }

    //call of next function
    $(".scroller-next a").click(function () {
        scrollNext();
        return false;
    });
    //call of prev function
    $(".scroller-previos a").click(function () {
        scrollPrev();
        return false;
    });

    //function to auto call next funtion, on time delay
    autoRotate = function () {
        play = setInterval(function () {
            scrollNext();
        }, 5000);
    }
    // call auto function
    autoRotate();

    //stop auto function when mouse over the scroller
    $(".scroller-wrapper").hover(function () {
        clearInterval(play);
    }, function () {
        autoRotate();
    });

    $(".scroller-navigation a").click(function () {
        var goTo = $(this).attr("rel");
        currentslide = parseInt(goTo);
        $(".scroller-navigation a").removeClass("active");
        $(this).addClass("active");
        scrollPosition = scrollWidth - scrollWidth * currentslide;
        $(".scroller-slides").animate({ left: scrollPosition }, 500);
        return false;
    });

});
