﻿/* Rotating Panel */
jQuery().ready(function () {
    var rotatingPanel = function () {

        var panels = [];

        $('.rotating-panel').each(function (i) {
            panels.push($(this));
        })

        if (panels.length > 0) {

            function StartTimer(position) {
                $(document).everyTime('4s', 'rotating-panel', function () { rotatingPanel.rotate(position); }, 0);
            }

            function StopTimer() {
                $(document).stopTime('rotating-panel');
            }

            $('#slidePager a').click(function (e) {
                $('#slidePager a').removeClass("selected");
                $(this).addClass("selected");

                var newPosition = $(this).attr('href').replace('?position=', '');
                rotatingPanel.rotate(newPosition);

                e.preventDefault();
            });

            panels[0].show();
            StartTimer(1);
        }

        return {
            currentIndex: 0,
            rotate: function (position) {
                StopTimer();
                var oldIndex = this.currentIndex;
                var newIndex = position;
                if (newIndex >= panels.length || position < 0) {
                    newIndex = 0;
                }

                this.currentIndex = newIndex;

                if (oldIndex != this.currentIndex) {
                    $('#slidePager a').removeClass("selected");
                    $('#slidePager a').eq(this.currentIndex).addClass("selected");

                    panels[oldIndex].fadeOut(1000);
                    panels[newIndex].fadeIn(1000, function () { StartTimer(newIndex + 1); });
                }
            }
        }
    } ();
});
