﻿(function ($) {
    $.fn.SetupPictureRotator = function (pluginSettings) {

        var defaultSettings = {
            delaySpeed: 5000
        };
        var $settings = $.extend(defaultSettings, pluginSettings);

        return this.each(function () {

            var $canvas = $(this).find('.picturesWrapper');
            var $stage = $(this).find('.pictures')
            var $items = $(this).find('li');
            var _totalPages = $items.size();
            var rolling;

            // clonning pictures for loop effect
            var $firstItem = $items.filter(':first');
            var $lastItem = $items.filter(':last');
            $firstItem.before($lastItem.clone().addClass('cloned'));
            $lastItem.after($firstItem.clone().addClass('cloned'));
            $items = $(this).find('li');

            var pictureWidth = $firstItem.width();
            var _currentIndex = 1;

            // setup stage width
            $stage.width($items.size() * pictureWidth)

            // reseting scroll position
            $canvas.scrollLeft(pictureWidth);

            // scrolling animation
            function GoToPhoto(index) {

                // - deslocar a foto
                var direction = index < _currentIndex ? -1 : 1,
                    n = Math.abs(_currentIndex - index),
                    left = pictureWidth * direction * n;

                $canvas.filter(':not(:animated)').animate({
                    scrollLeft: '+=' + left
                }, 500, function () {

                    if (index == 0) {
                        $canvas.scrollLeft(pictureWidth * _totalPages);
                        index = _totalPages;
                    } else if (index > _totalPages) {
                        $canvas.scrollLeft(pictureWidth);
                        index = 1;
                    }

                    _currentIndex = index;

                });

            };

            // manual navigation
            $(this).find('.prev').click(function () {
                GoToPhoto(_currentIndex - 1);
                return false;
            });
            $(this).find('.next').click(function () {
                GoToPhoto(_currentIndex + 1);
                return false;
            });

            // auto navigation
            if (_totalPages > 1) {

                var autoScrolling = true;
                rolling = setInterval(function () {
                    if (autoScrolling) {
                        GoToPhoto(_currentIndex + 1);
                    }
                }, $settings.delaySpeed);

                $canvas.mouseover(function () {
                    autoScrolling = false;
                }).mouseout(function () {
                    autoScrolling = true;
                });
            }

        });
    };
})(jQuery);

$(document).ready(function () {
    $('.pictureRotator').SetupPictureRotator({
        delaySpeed:5000
    });
});
