var featureLinkCount = 0;
var sliderExpandedIndex = -1;
var currentRotatingIndex = -1;
var rotatingTimeoutMS = 4000;

function setFeatureLinkCount(count) {
    featureLinkCount = count;

    for (var i = 0; i < featureLinkCount; i++) {
        attachSliderClick(i);
    }

    rotateFeatureLinks();
}

function attachSliderClick(index) {
    jQuery("#slide-tab" + index).click(function() {
        toggleSlider(index);
    });
}

function toggleSlider(index) {
    // No current expanded slider - set the expanded slider index, toggle message visibility
    if (sliderExpandedIndex == -1) {
        sliderExpandedIndex = index;

        if (currentRotatingIndex != index) {
            jQuery("#slide-tab" + currentRotatingIndex).toggleClass("slide-tab-active");
            jQuery("#slide-tab" + index).toggleClass("slide-tab-active");
        }

        jQuery("#message" + currentRotatingIndex).toggle();
        jQuery("#pane" + index).slideToggle("slow");

        currentRotatingIndex = index;
        return false;
    }

    // The specified index was the same as current expanded slider index - retract slider, toggle message visibility
    if (sliderExpandedIndex == index) {
        jQuery("#pane" + index).slideToggle("slow", function() {
            sliderExpandedIndex = -1;
            jQuery("#message" + currentRotatingIndex).toggle();            
        });
    }
    else {
        // The specified index was different from the currently expanded slider index - change pane and slider selection
        jQuery("#slide-tab" + sliderExpandedIndex).toggleClass("slide-tab-active");
        jQuery("#pane" + sliderExpandedIndex).hide();

        jQuery("#slide-tab" + index).toggleClass("slide-tab-active");
        jQuery("#pane" + index).show();

        sliderExpandedIndex = index;
        currentRotatingIndex = index;
    }

    return false;
}

function rotateFeatureLinks() {
    // Only perform rotation if the slider is not expanded
    if (sliderExpandedIndex == -1) {
        // If a current rotating index exists then toggle rotation
        if (currentRotatingIndex > -1) {
            jQuery("#slide-tab" + currentRotatingIndex).toggleClass("slide-tab-active");
            jQuery("#message" + currentRotatingIndex).toggle();
        }

        var newRotatingIndex = 0;

        if (currentRotatingIndex < featureLinkCount - 1) {
            newRotatingIndex = currentRotatingIndex + 1;
        }

        jQuery("#slide-tab" + newRotatingIndex).toggleClass("slide-tab-active");
        jQuery("#message" + newRotatingIndex).toggle();

        currentRotatingIndex = newRotatingIndex;
    }

    // Wait a number of milliseconds before recursively calling the same function
    setTimeout(function() {
        rotateFeatureLinks();
    }, rotatingTimeoutMS);
}

