jQuery.Flash = function(element) {
    this.element = $(element);
    this.timeout = undefined;

    var content = $('<span class="title"></span><span class="description"></span>');

    this.element.hide().append(content);
};

jQuery.Flash.prototype.show = function() {
    this.element.fadeIn();
    
    if ( ! this.element.hasClass('error') )
        this.timeout = window.setTimeout(function() { jQuery.flash.hide() }, 3000);
}

jQuery.Flash.prototype.hide = function() {
    if ( this.timeout ) {
        clearTimeout(this.timeout);
        this.timeout = undefined;
    }
    
    this.element.fadeOut();
}

jQuery.Flash.prototype.message = function(klass, title, description) {
    if ( this.element.is(":visible") ) {
        if ( this.timeout ) {
            clearTimeout(this.timeout);
            this.timeout = undefined;
        }

        this.element.fadeOut(function() { jQuery.flash.message(klass, title, description); });
        return;
    }
    
    this.element.attr('class', klass);
    this.element.find('.title').text(title);
    this.element.find('.description').text(description);
    
    this.show();
}

jQuery.Flash.prototype.error = function(label, description) { this.message('flash error', label, description); }
jQuery.Flash.prototype.warning = function(label, description) { this.message('flash warning', label, description); }
jQuery.Flash.prototype.success = function(label, description) { this.message('flash success', label, description); }

$(function(){ jQuery.flash = new jQuery.Flash('#flash'); });
