﻿(function ($) {

    $.fn.uoIpsenRotation = function (options) {

        var defaultOptions = {
            rotationSpeed: 8000, navItems: 'rotation-navitems ul', itemTag: 'li', itemsClass: 'uo-item',
            itemsContainerClass: 'rotation-items', currentClass: 'rotation-current', scrollLength: '90', autoStart: true
        };

        options = $.extend(defaultOptions, options);

        var $parent = $(this);
        var myTimer;
        var popupIsActive = false;

        $('.' + options.itemsClass, $parent).each(function (index) {
            var itemId = $(this).attr('id').replace('div_', '');
            var itemImage = $('#div_' + itemId + ' a').attr('rel');
            $('.' + options.navItems, $parent).append('<li id="' + itemId + '"><img class="rotation-img" src="' + itemImage + '" alt="" /></li>');
        });

        $('.' + options.navItems + ' ' + options.itemTag, $parent).first().addClass(options.currentClass);
        $('.' + options.itemsClass, $parent).first().fadeIn(1000);

        if (options.autoStart)
            myTimer = setInterval(__rotateNext, options.rotationSpeed);

        function __rotateNext() {
            var active = $('.' + options.currentClass, $parent).attr('id');
            var next = $('#' + active, $parent).next().attr('id');

            if (!next)
                next = $('.' + options.navItems + ' ' + options.itemTag).first().attr('id');

            if (!popupIsActive) {
                $('.' + options.itemsContainerClass).append($('#div_' + active));
                $('.' + options.itemsContainerClass).append($('#div_' + next));

                $('#div_' + next).fadeIn(400, function () {
                    $('#div_' + active).hide();
                    $('.' + options.navItems + ' ' + options.itemTag, $parent).css({ 'opacity': '' });
                });

                $('.' + options.navItems + ' ' + options.itemTag, $parent).stop(false, false).animate({
                    left: '-' + options.scrollLength + 'px'
                }, 400, 'linear', function () {
                    $('.' + options.navItems + ' ' + options.itemTag, $parent).css({ 'left': '0px' });
                    $('.' + options.navItems, $parent).append($('#' + active, $parent));
                    $('.' + options.navItems + ' ' + options.itemTag, $parent).removeClass(options.currentClass);
                    $('.' + options.navItems + ' ' + options.itemTag, $parent).first().addClass(options.currentClass);
                });
            }
        }

        function __rotateNextNext() {
            var active = $('.' + options.currentClass, $parent).attr('id');
            var next = $('#' + active, $parent).next().attr('id');

            if (!next)
                next = $('.' + options.navItems + ' ' + options.itemTag).first().attr('id');

            if (!popupIsActive) {
                $('.' + options.itemsContainerClass).append($('#div_' + active));
                $('.' + options.itemsContainerClass).append($('#div_' + next));

                $('#div_' + active).fadeOut(800);

                $('.' + options.navItems + ' ' + options.itemTag, $parent).stop(false, false).animate({
                    left: '-' + options.scrollLength + 'px'
                }, 120, 'linear', function () {
                    $('.' + options.navItems + ' ' + options.itemTag, $parent).css({ 'left': '0px' });
                    $('.' + options.navItems, $parent).append($('#' + active, $parent));
                    $('.' + options.navItems + ' ' + options.itemTag, $parent).removeClass(options.currentClass);
                    $('.' + options.navItems + ' ' + options.itemTag, $parent).first().addClass(options.currentClass);
                    $('.' + options.navItems + ' ' + options.itemTag, $parent).first().css({ 'opacity': '0.25' });
                });
            }
        }

        function __rotateBack() {
            var active = $('.' + options.currentClass, $parent).attr('id');
            var next = $('#' + active, $parent).prev().attr('id');

            if (!next)
                next = $('.' + options.navItems + ' ' + options.itemTag).last().attr('id');

            if (!popupIsActive) {
                $('#div_' + next).fadeIn(400, function () {
                    $('#div_' + active).hide();
                    $('.' + options.navItems + ' ' + options.itemTag, $parent).css({ 'opacity': '' });
                });

                $('.' + options.itemsContainerClass).append($('#div_' + next));
                $('.' + options.navItems, $parent).stop(false, false).prepend($('#' + next, $parent));
                $('.' + options.navItems + ' ' + options.itemTag, $parent).stop(false, false).css({ 'left': '-' + options.scrollLength + 'px' });

                $('.' + options.navItems + ' ' + options.itemTag, $parent).stop(false, false).animate({
                    left: '0px'
                }, 400, 'linear', function () {
                    $('.' + options.navItems + ' ' + options.itemTag, $parent).removeClass(options.currentClass);
                    $('.' + options.navItems + ' ' + options.itemTag, $parent).first().addClass(options.currentClass);

                });
            }
        }

        $parent.mouseover(function () {
            clearInterval(myTimer);
        });

        $parent.mouseleave(function () {
            clearInterval(myTimer);
            myTimer = setInterval(__rotateNext, options.rotationSpeed);
        });

        $(".navigate-next").click(function () {
            __rotateNext();
        });

        $(".navigate-back").click(function () {
            __rotateBack();
        });

        $(".popup").click(function () {
            clearInterval(myTimer);
            popupIsActive = true;

            var item = $(this).attr('name');
            var snippet = $('#snippet_' + item).html();
            var $document = $(document);
            var $body = $document.find('body');

            var box = '<div id="lightbox">' + snippet + '<div id="closelightbox"></div></div>';
            $body.append(box);

            $('#closelightbox').click(function () {
                popupIsActive = false;
                clearInterval(myTimer);
                myTimer = setInterval(__rotateNext, options.rotationSpeed);
                $('#lightbox').remove();
            });
        });

        $('.' + options.navItems + ' ' + options.itemTag).click(function () {

            var offset1 = $('.navigate-back').offset().left;

            var offset2 = $(this).offset().left;
            var position = offset2 - offset1 - 27;

            if (position == 55)
                __rotateNext();
            if (position == 111) {
                __rotateNextNext();
                setTimeout(__rotateNext, 140);
            }
        });
    }
})(jQuery);
