
// Dialog constructor
function StatusMessage(html) {

    this.div = new Element('div', { 'class': 'statusmessage' } );
    this.id = this.div.identify();
        
    this.div.insert("<table cellpadding='0' cellspacing='0'><tbody><tr><td>" + html + "</td></tr></tbody></table>");
        
    this.td = this.div.getElementsBySelector("td")[0];    
    this.td.dialog = this;
    this.div.dialog = this;    
    this.td.name = "statusmessagetd";    

    this.td.onclick = function() {
        if (this.dialog) {
            this.dialog.switchoff();
        }
    }
    
    this.fadeTimer = null;
    this.closeTimer = null;
        
    this.open = function() {
        this.div.hide();
        document.body.appendChild(this.div);        
        setTimeout("Effect.Appear('" + this.id + "', { 'duration' : 0.5 } );", 2);
        this.setFadeTimer();
    }
    
    this.close = function() {        
        if (this.div) {            
            document.body.removeChild(this.div);
        }
    }

    this.selfDestructSequence = function() {
        this.cancelSelfDestructSequence();
        this.closeTimer = setTimeout("StatusMessage.close('" + this.id + "')", 1400);
    }
    
    this.cancelSelfDestructSequence = function() {
        if (this.closeTimer) {
            clearTimeout(this.closeTimer);
        }
    }
    
    this.switchoff = function() {
        Effect.SwitchOff(this.id);
        this.selfDestructSequence();
    }    
    
    this.fade = function() {
        //start fade-out, close after 3 seconds. 
        //using setTimeout, because I don't get the scriptaculous callbacks
        Effect.Fade(this.id);
        this.selfDestructSequence();
    }
    
    this.setFadeTimer = function() {        
        this.clearFadeTimer();
        this.fadeTimer = setTimeout("StatusMessage.fade('" + this.id + "', { 'duration' : 2 } )", 3000);        
    }
    
    this.clearFadeTimer = function() {
        if (this.fadeTimer) {
            clearTimeout(this.fadeTimer);
        }
    }
}

// "static" methods
StatusMessage.close = function(id) {
    if ($(id) && $(id).dialog) {
        $(id).dialog.close();
    }
}

StatusMessage.fade = function(id) {
    if ($(id) && $(id).dialog) {
        $(id).dialog.fade();
    }
}