if (!GoldstoneGranite) {
	var GoldstoneGranite = {};
}

/**
 * Constructor for SlideShow
 *
 * @constructor
 * @param domQuery {string} DOM query for the container around SlideShow
 * @param htmlTag {string} The HTML tag of the items in the SlideShow, i.e., img
 * @param options {object} Interval and initial delay options
 */
GoldstoneGranite.SlideShow = function(domQuery, htmlTag, options) {
    this._domQuery = domQuery;
    this._htmlTag = htmlTag;

    // Default values; these attributes are not required in options array
    this._interval = 5000;
    this._initialDelay = 0;

    if (typeof options != "undefined") {
        if (typeof options.interval != "undefined") {
            this._interval = options.interval;
        }
        if (typeof options.initialDelay != "undefined") {
            this._initialDelay = options.initialDelay;
        }
    }
};

GoldstoneGranite.SlideShow.prototype = {
    /**
     * Starts the SlideShow
     */
    start: function() {
        var domQuery = this._domQuery;
        var htmlTag = this._htmlTag;
        var interval = this._interval;
        var delayWrapper = function() {
            var slide = function() {
                var active = $(domQuery + " " + htmlTag + ".active");

                if (active.length == 0) {
                    active = $(domQuery + " " + htmlTag + ":last");
                }

                var next;
                if (active.next().length) {
                    next = active.next();
                } else {
                    next = $(domQuery + " " + htmlTag + ":first");
                }

                active.addClass("last-active");

                next.css({opacity: 0.0})
                    .addClass("active")
                    .animate({opacity: 1.0}, 1000, function() {
                        active.removeClass("active last-active");
                    });
                setTimeout(slide, interval);
            }
            setTimeout(slide, interval);
        }
        setTimeout(delayWrapper, this._initialDelay);
    }
};

$(document).ready(function() {
    var feature1 = new GoldstoneGranite.SlideShow(
        "#rotate-box", "img", {interval: 3000}
    );
    feature1.start();
});