/*

Life as Me: Flash Message jQuery Plugin
Copyright (c) 2008 Alice Dawn Bevan-McGregor. All Rights Reserved.

Permission is granted to use and modify this file as long as this original header remains intact.

For additional information on the site design used by Life as Me, please see the following website:

    http://www.lifeasme.com/corporate/site-design/

*/

jQuery.Flash = function(element){
    this.element = $(element);
    this.timeout = undefined;

    this.element.hide()
        .click(function(){ jQuery.flash.hide(); })
        .hover(function(){ jQuery.flash.onOver(); }, function(){ jQuery.flash.onLeave(); })

    this.element.removeClass('expired');

    if ( this.element.is(":visible") ) {
        if ( this.timeout ) {
            clearTimeout(this.timeout);
            this.timeout = undefined;
        }

        this.element.fadeOut(1000, function(){ jQuery.flash.hide(); });
        return;
    }

    this.element.fadeIn(1000);

    if ( ! this.element.hasClass('error') )
        this.timeout = window.setTimeout(function(){ jQuery.flash.onTimeout() }, 15000);
};

jQuery.Flash.version = 1.1;

jQuery.Flash.prototype.onOver = function() {
    this.element.addClass('over');
}

jQuery.Flash.prototype.onLeave = function() {
    this.element.removeClass('over');

    if ( this.element.hasClass('expired') ) this.hide();
}

jQuery.Flash.prototype.onTimeout = function() {
    this.element.addClass('expired');
    if ( ! this.element.hasClass('over') ) this.hide();
}

jQuery.Flash.prototype.hide = function() {
    if ( this.timeout ) {
        clearTimeout(this.timeout);
        this.timeout = undefined;
    }

    this.element.fadeOut(1000).removeClass('expired').removeClass('over');
}

$(function(){ jQuery.flash = new jQuery.Flash('#flash'); });