function FeaturedContentController (viewer) {
    this.viewer = viewer;
    this.isPlaying = false;
    this.intervalId = null;
    thisController = this;
    this.playAction = function () {
        if (thisController.viewer.getCurrentIndex() < (thisController.viewer.getItemCount() - 1)) {
            thisController._changeImplementation('next');
        } else {
            thisController._changeImplementation(1);
        }
    }
}


FeaturedContentController.prototype.change = function (item) {
    this.stop();
    this._changeImplementation(item);
    return false;
}


FeaturedContentController.prototype._changeImplementation = function (item) {
    if (item == 'next') {
        this.viewer.next();
    } else if (item == 'prev') {
        this.viewer.prev();
    } else {
        this.viewer.goto(item - 1);
    }
    this._highlightNumberButton(this.viewer.getCurrentIndex() + 1);
}



FeaturedContentController.prototype._highlightNumberButton = function (number) {
    var numberImageList = jQuery('#buttons img');
    for (var i=0; i < numberImageList.length - 3; i++) {
        numberImageList[i].src = numberImageList[i].src.replace(/_on\.gif$/, '.gif');
    }
    numberImageList[number - 1].src = numberImageList[number - 1].src.replace(/\.gif$/, '_on.gif');
}


FeaturedContentController.prototype._changePlayButton = function (state) {
    var buttonImageList = jQuery('#buttons img');
    var playButtonImage = buttonImageList[buttonImageList.length - 2];
    playButtonImage.src = playButtonImage.src.replace(new RegExp('/[^/]+\.gif$'), '/'+state+'.gif');
}


FeaturedContentController.prototype.togglePlay = function () {
    if (this.isPlaying) {
        this.stop();
    } else {
        this.play();
    }
    return false;
}


FeaturedContentController.prototype.play = function (suppressFirstChange) {
    this.isPlaying = true;
    this._changePlayButton('pause');
    if (! suppressFirstChange) {
        this.playAction();
    }
    this.intervalId = setInterval(this.playAction, 10000);
    return false;
}


FeaturedContentController.prototype.stop = function () {
    clearInterval(this.intervalId);
    this.intervalId = null;
    this._changePlayButton('play');
    this.isPlaying = false;
    return false;
}


