﻿
var rotatingFeatureCount;
var rotatingFeatureDelay = '8s'; // Default value - overriden by server-side value based on the Delay property in RotatingFeature.ascx
var rotatingFeatureMaxRotations = 20;  // Default value - overriden by server-side value based on the MaxRotations property in RotatingFeature.ascx

$(function() {
    rotatingFeatureCount = $('div#RotatingFeatureContainer div.Feature').length;
    if (rotatingFeatureCount > 1) {
        // Wire up automatic rotation
        $("div#RotatingFeatureContainer").everyTime(
            rotatingFeatureDelay,
            "RotatingFeatureTimer",
            function() {
                currentItem = parseInt($($('div#RotatingFeatureController a.On')[0]).text());
                // Hide the current item
                featureHide($('div#RotatingFeatureController a')[currentItem - 1]);
                // Show the next item
                currentItem += 1;
                if (currentItem > rotatingFeatureCount) currentItem = 1;
                featureShow($('div#RotatingFeatureController a')[currentItem - 1]);
            },
            rotatingFeatureMaxRotations
        );
        // Add navigation links
        for (var i = 1; i < rotatingFeatureCount + 1; i++) {
            $('div#RotatingFeatureController').append(String.format('<a href="#{0}">{0}</a>', i));
        }
        // Highlight the first item
        featureShow($('div#RotatingFeatureController a')[0]);
        $('div#RotatingFeatureController').fadeIn('slow');
        // Wire up hover on navigation links
        $('div#RotatingFeatureController a').hover(function() {
            if ($(this).hasClass('On') == false) {
                $("div#RotatingFeatureContainer").stopTime("RotatingFeatureTimer"); // stop rotation
                featureHide($('div#RotatingFeatureController a.On')[0]);
                featureShow(this);
            }
        });

        // Stop automatic rotation on hover (ICMA asked for this to be disabled)
        //            $('div#RotatingFeatureContainer').hover(function() {
        //                $("div#RotatingFeatureContainer").stopTime("RotatingFeatureTimer");
        //            });
        
    }
});

featureShow = function(linkElement) {
    $('div#RotatingFeatureController a').removeClass('On');
    featureId = parseInt($(linkElement).text());
    $(linkElement).addClass('On');
    $($('div#RotatingFeatureContainer div.Feature')[featureId - 1]).fadeIn('slow');
};

featureHide = function(linkElement) {
    featureId = parseInt($(linkElement).text());
    $($('div#RotatingFeatureContainer div.Feature')[featureId - 1]).fadeOut('slow');
};


