/**
* FeatureRotator object encapsulates all the logic and variables needed to rotate a collection of
* features in a target container.
*
* @author   Andy Shellam
* @date     22/11/2010
* @requires jQuery 1.4+
*/
function FeatureRotator(features, delay) {
    if (
        features == null ||
        features.constructor == null ||
        features.constructor.toString().indexOf("Array") == -1) {
        throw "options parameter must be an array";
    }

    for (var i = 0; i < features.length; i++) {
        if (
            features[i] == null ||
            features[i].constructor == null ||
            features[i].constructor.toString().indexOf("Array") == -1) {
            throw "Index " + i + " within the options parameters must be an array"
        }
    }

    if (delay == null) {
        delay = 5000;
    }

    this.currentIndex = 0;
    this.delay = delay;
    this.features = features;
    this.numberOfFeatures = features.length;

    /**
    * Changes the currently displayed feature to the feature at the given position
    *
    * @param position Number of the feature in the array to change to (1-based)
    */
    this.changeTo = function(position) {
        if (position > this.numberOfFeatures || position < 1) {
            return;
        }

        position--; // convert to zero-indexed
        if (position == 0) {
            position = this.numberOfFeatures - 1;
        }
        else {
            position--; // onWindowInterval will increment the index
        }

        this.currentIndex = this.onWindowInterval(this.features, position);
    }

    /**
    * Changes the current displayed feature to the feature at the given position and pauses
    * the rotation for a given interval
    *
    * @param position Number of the feature in the array to change to (1-based)
    * @param interval Length of time to pause the rotation
    */
    this.changeToAndPause = function(position, interval) {
        if (position > this.numberOfSnippets || position < 1) {
            return;
        }

        this.changeTo(position);
        this.pause(interval);
    }

    /**
    * Method called from window.setInterval to rotate the feature to the next feature
    */
    this.onWindowInterval = function(features, currentIndex) {
        currentIndex++;

        if (currentIndex > features.length - 1) {
            currentIndex = 0;
        }

        var currentFeature = features[currentIndex];
        var title = currentFeature[0];
        var text = currentFeature[1];
        var image = currentFeature[2];
        var skin = currentFeature[3];

        var targetHtml = image;
        targetHtml += '<div id="feature_panel_text" class="' + skin + '">';
        targetHtml += '<h2>' + title + '</h2>';
        targetHtml += '<p>' + text + '</p>';
        targetHtml += '</div>';

        $('#feature_panel_content').html(targetHtml);

        // Have to remove the sIFR-active class so sIFR can be re-processed
        $('html').removeClass('sIFR-active');

        // Re-configure the sIFR text
        sIFR.replace(blisstext, { selector: '#feature_panel_text h2', ratios: [6, 0.92, 8, 0.94, 10, 1, 15, 1.05, 20, 1.1, 26, 1.12, 32, 1.13, 36, 1.14, 41, 1.15, 42, 1.16, 46, 1.15, 62, 1.16, 82, 1.17, 83, 1.18, 93, 1.17, 94, 1.18, 95, 1.17, 97, 1.18, 98, 1.17, 102, 1.18, 103, 1.17, 1.18], css: [
            '.sIFR-root {color:#ffffff; font-size:28px;text-decoration:none; }'
            , 'a { text-decoration:none;color: #ffffff; }'
            , 'a:link { text-decoration:none;color: #ffffff; }'
            , 'a:visited { text-decoration:none;color: #ffffff; }'
            , 'a:hover { text-decoration:none;color: #ffffff; }'
            ], wmode: 'transparent'
        });

        /* Get the correct class and update the link colours */
        var colourClass = $('#feature_panel_content #feature_panel_text').attr('class');
        $('#feature_panel_links a').removeClass();
        $('#feature_panel_links a').addClass(colourClass);

        // Set the link to selected
        $('#feature_panel_links div').removeClass();
        $('#feature_panel_links div').eq(currentIndex).addClass('selected');

        return currentIndex;
    }

    /**
    * Pauses the rotation for an interval (given in milliseconds)
    *
    * @param interval Length of time to pause the rotation
    */
    this.pause = function(interval) {
        if (this.intervalId == null) {
            return;
        }

        window.clearInterval(this.intervalId);

        var rotator = this;
        window.setTimeout(function() {
            rotator.start();
        }, interval - rotator.delay);
    }

    /**
    * Starts the features rotating
    */
    this.start = function() {
        var rotator = this;

        this.intervalId = window.setInterval(function() {
            rotator.currentIndex = rotator.onWindowInterval(rotator.features, rotator.currentIndex);
        }, this.delay);
    }

    /**
    * Stops the features rotating
    */
    this.stop = function() {
        if (this.intervalId != null) {
            window.clearInterval(this.intervalId);
        }
    }
}

