var FlashMessages = function(div, rotateElementType, hiddenClass, stopMsPerCharacter, fadeMs)
{
    var that = this;
    this.parentDiv = $('#' + div);
    this.rotateElementType = rotateElementType;
    this.children = this.parentDiv.children(rotateElementType);
    this.hiddenClass = hiddenClass;
    this.timeout = null;
    this.stopMs = stopMsPerCharacter;
    this.fadeMs = fadeMs;

    this.children.fadeOut(0).removeClass(hiddenClass);
    $(this.children[0]).fadeIn(0);

    this.parentDiv.mouseover(function () {that.rotating = false;});
    this.parentDiv.mouseout(function () {that.rotating = true;});

    this.rotating = false;
    this.visIndex = 0;

    this.CountDelay = function(index)
    {
        var length = $(that.children[index]).text().length;
        var delay = length * that.stopMs;
        if(length <= 30) delay += 1000;
        return delay;
    }

    this.Rotate = function()
    {
        this.rotating = true;
        if(that.children.length > 1)
        {
            this.timeout = window.setTimeout(that.Fading, that.CountDelay(0));
        }
    };


    this.Fading = function()
    {
        if(that.rotating && that.children.length)
        {
            $(that.children[that.visIndex]).fadeOut(that.fadeMs, function() {
                if(that.visIndex+1 < that.children.length) {
                    $(that.children[that.visIndex+1]).fadeIn(that.fadeMs);
                    that.visIndex += 1;
                } else {
                    $(that.children[0]).fadeIn(that.fadeMs);
                    that.visIndex = 0;
                }
                that.timeout = window.setTimeout(that.Fading, that.CountDelay(that.visIndex));
            });
        }
        else { that.timeout = window.setTimeout(that.Fading, that.CountDelay(that.visIndex)); }

    }

}

