﻿(function ($) {
    $.fn.simpleRotator = function (options) {

        var defaults = {
            animation: 'fade',
            easing: 'linear',
            autorotate: true,
            timeout: 8000,
            content: '.content',
            menu: '.menu',
            next: '.next',
            previous: '.previous',
            hasMenu: true,
            hasButtons: true
        };

        return this.each(function () {

            if (options) {
                $.extend(defaults, options);
            }

            var thisRotator = $(this), contentItems = thisRotator.find(defaults.content), maxItems = contentItems.find('li').size();

            /*======= Timer ======= */
            var Timer = {
                timeoutID: null,
                init: function () {
                    this.setAutoRotate();
                    $.subscribe('/transition/start', function (index, stopTimer) {
                        stopTimer && Timer.clearAutoTimer();
                    });
                    $.subscribe('/transition/end', function (index) {
                        if (defaults.autorotate) {
                            Timer.setAutoRotate();
                        }
                    });
                },
                setAutoRotate: function () {
                    Timer.timeoutID = setTimeout(function () {
                        $.publish('/transition/start', State.next);
                    }, defaults.timeout);
                },
                clearAutoTimer: function () {
                    if (defaults.autorotate) {
                        clearTimeout(Timer.timeoutID);
                        defaults.autorotate = false;
                    }
                }
            };

            /*======= State ======= */
            var State = {
                current: 0,
                next: 1,
                previous: maxItems - 1,
                inTransition: false,
                updateState: function (index) {
                    this.updateProp('current', index);
                    var next = index < maxItems - 1 ? index + 1 : 0,
                    previous = index === 0 ? maxItems - 1 : index - 1;
                    this.updateProp('next', next);
                    this.updateProp('previous', previous);
                    this.updateProp('inTransition', false);
                },
                updateProp: function (prop, index) {
                    this[prop] = index;
                },
                init: function () {
                    $.subscribe('/transition/start', function () {
                        State.updateProp('inTransition', true);
                    });
                    $.subscribe('/transition/end', function (index) {
                        State.updateState(index);
                    });
                }
            };

            /*======= Menu ======= */
            var Menu = {
                elem: $(defaults.menu),
                init: function () {
                    $.subscribe('/transition/start', function (index) {
                        Menu.updateMenu(index);
                    });
                    this.clickEvent();
                },
                clickEvent: function () {
                    this.elem.delegate('a', 'click', function (event) {
                        event.preventDefault();
                        if (this.className.indexOf('selected') !== -1 && !State.inTransition) {
                            var selectedIndex = elem.find('li').index($(this).parent());
                            $.publish('/transition/start', [indexToShow, true /* Stop Timer */]);
                        }
                    });
                },
                updateMenu: function (index) {
                    this.elem.find('.selected').removeClass('selected').end().find('li').eq(index).addClass('selected');
                }
            };

            /*======= Buttons ======= */
            var Buttons = {
                init: function () {
                    this.clickEvent();
                },
                nextButton: thisRotator.find(defaults.next),
                previousButton: thisRotator.find(defaults.previous),
                clickEvent: function () {
                    $.each([this.nextButton, this.previousButton], function () {
                        $(this).click(function (event) {
                            event.preventDefault();
                            if (!State.inTransition) {
                                var indexToShow = this.className.indexOf('next') !== -1 ? State.next : State.previous;
                                $.publish('/transition/start', [indexToShow, true /* Stop timer */]);
                            }
                        });
                    });
                }
            }

            /*======= Content ======= */
            var Content = {
                init: function () {
                    $.subscribe('/transition/start', function (index) {
                        Content.animations[defaults.animation](index);
                    });
                },
                postTransition: function (currentItem, newItem) {
                    currentItem.removeClass('selected');
                    newItem.addClass('selected');
                },
                animations: {
                    "zoom": function (indexToShow) {
                        var currentItem = contentItems.find('li').eq(State.current), newItem = contentItems.find('li').eq(indexToShow);
                        var currentImg = currentItem.find('img');
                        currentImg.animate({ height: "380px", width: "1260px" }, 1500, '', function () { currentImg.css("width", "630px"); currentImg.css("height", "190px"); });
                        currentItem.animate({
                            "opacity": "0"
                        }, {
                            "duration": 800,
                            "easing": "easeOutExpo"
                        }, function () {
                            $(this).css({
                                "display": "none",
                                "opacity": "1"
                            });
                        });

                        setTimeout(function () {
                            newItem.css({
                                "display": "block",
                                "opacity": "0"
                            }).animate({
                                "opacity": "1"
                            }, 800, 'swing', function () {
                                Content.postTransition(currentItem, newItem);
                                $.publish('/transition/end', indexToShow);
                            });
                        }, 300);
                    }
                }
            }

            defaults.hasMenu && Menu.init();
            defaults.hasButtons && Buttons.init();
            defaults.autorotate && Timer.init();
            State.init();
            Content.init();

        });

    };
})(jQuery);

/*
* jQuery Tiny Pub/Sub - v0.6 - 1/10/2011
* http://benalman.com/
*
* Copyright (c) 2010 "Cowboy" Ben Alman
* Dual licensed under the MIT and GPL licenses.
* http://benalman.com/about/license/
*/
(function ($) { var a = $("<b/>"); $.subscribe = function (b, c) { function d() { return c.apply(this, Array.prototype.slice.call(arguments, 1)) } d.guid = c.guid = c.guid || ($.guid ? $.guid++ : $.event.guid++); a.bind(b, d) }; $.unsubscribe = function () { a.unbind.apply(a, arguments) }; $.publish = function () { a.trigger.apply(a, arguments) } })(jQuery);

