/*

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/

Changelog:

    1.0     Initial release.
    1.1     Updated to use setTimeout vs. the elem.animate() hack.
    
Upcoming features:

    - Multiple message support.  This requires creating a new flash div for each message and stacking them.

Sample CSS formatting:

    #flash { position: fixed; top: 0px; left: 0px; width: 100%; z-index: 1000; background-color: #569; color: white; background-image: url('/static/img/btn.png'); background-position: bottom; border-bottom: 1px solid #555; }
    #flash, #flash * { cursor: pointer; }
    #flash .yui-b { padding: 5px 0; }
    #flash .yui-b>* { font-size: 128%; }
    #flash label { display: block; text-align: right; font-weight: bold; text-transform: capitalize; }
    #flash label:after { content: ':'; }

    #flash.subtle { background-color: #444; color: white; }
    #flash.subtle:hover { background-color: #222; }
    #flash.warning { background-color: #ff0; color: black; }
    #flash.warning:hover { background-color: #ff8; }
    #flash.success { background-color: #595; }
    #flash.success:hover { background-color: #7b7; }
    #flash.failure, #flash.error { background-color: #800; }
    #flash.failure:hover, #flash.error:hover { background-color: #a00; }
    #flash.subtle, #flash.success, #flash.failure, #flash.error { text-shadow: black 2px 2px 2px; }

*/


jQuery.Flash = function(element){
    this.element = $(element);
    this.timeout = 300;
    
    
    this.element.append('<h2></h2>');
    this.element.append('<p></p>');
    
    this.element.hide()
        .click(function(){ jQuery.flash.hide(); })
        .hover(function(){ jQuery.flash.onOver(); }, function(){ jQuery.flash.onLeave(); });
};

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.show = function() {
    this.element.fadeIn();//(300);
    if(this.element.attr('class') == "carregando")
	    this.timeout = window.setTimeout(function(){ jQuery.flash.sistema_lento() }, 20000);//se passar de 20 segundos apresenta mensagem de lentidão
	else
    	if(this.element.attr('class') != "lentidao")
    		this.timeout = window.setTimeout(function(){ jQuery.flash.onTimeout() }, 3500);
}

jQuery.Flash.prototype.hide = function() {
    if ( this.timeout ) {
        clearTimeout(this.timeout);
        this.timeout = undefined;
    }
    this.element.removeClass('expired').removeClass('over').fadeOut();//slideUp(300)
}

jQuery.Flash.prototype.message = function(klass, label, message) {
    this.element.removeClass('expired');
    
    if ( this.element.is(":visible") ) {
        if ( this.timeout ) {
            clearTimeout(this.timeout);
            this.timeout = undefined;
        }
        this.element.hide()
        jQuery.flash.message(klass, label, message);
    }else
    {
    	this.element.attr('class', klass);
	    this.element.find('p').html(message);
	    this.element.find('h2').html(label);
	    this.show();
   
   	}
}

jQuery.Flash.prototype.erro= function(label, message) { this.message('erro', 'Operação não realizada!', 'O sistema não pôde completar a operação.'); }
jQuery.Flash.prototype.excecao= function(label, message) { this.message('excecao', 'Oops!', 'O sistema não estã conseguindo processar seu pedido, a Vector receberá um relatório sobre esse problema.'); }
jQuery.Flash.prototype.carregando= function(label, message) { this.message('carregando', 'Aguarde', 'Realizando operação.'); }
jQuery.Flash.prototype.sistema_lento= function(label, message) { this.message('lentidao', 'Processando', 'O sistema está demorando para responder, espere por mais alguns segundos. Caso o sistema não responda entre em contato com a Vector.'); }
jQuery.Flash.prototype.message_estatica = function(classe, label, message) { 
			this.message(classe, label, message);
			clearTimeout(this.timeout);
            this.timeout = undefined;}

$(function(){ jQuery.flash = new jQuery.Flash('#flash'); });


