



var rotatingPanelHome = function () {

    this.rotatingActionsHome = {};

    var panelsHome = [];
    var rotatingHome = false;

    return {
        initHome: function () {
		
			
            // Get rotating panels and store them in a private array
            $('div#audiencePanel div.rotating-feature').each(function (i) {
				panelsHome.push($(this));
            })

			
			
            // If this results in more than one panel begin rotating and setup pager
            if (panelsHome.length > 1) {
                rotatingPanelHome.rotatingActionsHome = $('span#action1, ul#actions a');

                // Add click handlers to the actions container and allow events to bubble
                rotatingPanelHome.rotatingActionsHome.mouseenter(function (e) {
                    var actionHome = $(this); 
                    
                    actionHome.addClass('hasFocus')
                    .oneTime(50, function () {
                        if (actionHome.hasClass('hasFocus')) {
                            var id = $(this).attr('id');
                            rotatingPanelHome.rotatingActionsHome.removeClass('current');
                            actionHome.addClass('current');
                            rotatingPanelHome.stateHome = "stopped";
                            rotatingPanelHome.rotateHome(null, id.replace('action', ''), true); // Call public rotate panel method when event activated
                        }
                    });

                }).mouseleave(function () {rotatingPanelHome.state = "started"; $(this).removeClass('hasFocus'); });
            }
        },
        currentIndex: 0,
        stateHome: "started", /* "started" or "stopped". Bit of a hack to prevent the timer being started on callback of the hide animations */
        // Function to allow all panels to be shown and timers stopped
        showAllPanelsHome: function () {
            this.stateHome = "stopped";
            for (var ii = 0, jj = panelsHome.length; ii < jj; ii = ii + 1) {
                panelsHome[ii].show(0);
            }
        },
        // Resets all the panels to start rotating again
        resetHome: function () {
            for (var ii = 1, jj = panelsHome.length; ii < jj; ii = ii + 1) {
                panelsHome[ii].hide(0);
            }
            this.stateHome = "started";
            this.rotateHome(null, 1, true);
        },
        rotateHome: function (direction, index, noFade) {

            if (!rotatingHome) {
                rotatingHome = true;
                var oldIndex = this.currentIndex; // Store of the old index for refernce later

                // If moving back decrement else if forward increment anything else does nothing
                if (direction && direction === 'prev') {
                    this.currentIndex--;

                    if (this.currentIndex < 0) {
                        this.currentIndex = (panelsHome.length - 1);
                    }
                }
                else if (direction && direction === 'next') {
                    this.currentIndex++;

                    if (this.currentIndex > (panelsHome.length - 1)) {
                        this.currentIndex = 0;
                    }
                }
                else if (index > 0) {
                    this.currentIndex = (index - 1);
                }

                if (noFade) {
                    panelsHome[oldIndex].hide(0,
                    function () {
                        rotatingPanelHome.rotatingActionsHome.removeClass('current');
                        rotatingPanelHome.rotatingActionsHome.filter('[id="action' + (rotatingPanelHome.currentIndex + 1) + '"]').addClass('current');
                    }); // Fade old panel out
                    panelsHome[rotatingPanelHome.currentIndex].show(0, function () { rotatingHome = false; }); // Fade new panel in and on completion begin timer again
                }
                else {
                    panelsHome[oldIndex].fadeOut(500,
                    function () {
                        rotatingPanelHome.rotatingActionsHome.removeClass('current');
                        rotatingPanelHome.rotatingActionsHome.filter('[id="action' + (rotatingPanelHome.currentIndex + 1) + '"]').addClass('current');
                        panelsHome[rotatingPanelHome.currentIndex].fadeIn(500, function () { rotating = false; }); // Fade new panel in and on completion begin timer again
                    }); // Fade old panel out
                }
            }
        }
    }
} ();


$(document).ready(function () { rotatingPanelHome.initHome(); });


