﻿function MessageBoxClass (id)
{
    this.id = "#" + id ;

    this.ShowMessage = function (source, text)
    {
        var node = $(this.id + " h4[id='" + source + "']") ;
        if (node.length == 0)
        {
            node = $("<h4 id='" + source + "'>" + text + "</h4>") ;
            var special = $(this.id + " [special='true']").first () ;
            if (special.length > 0)
                 node.insertBefore (special) ;
            else $(this.id + " div.i").append (node) ;
            node.hide () ;
        }
        else
            node.html (text) ;
        node.stop (true, true).slideDown ('fast') ;
        $(this.id).stop (true, true).slideDown ('fast') ;
    }

    this.HideMessage = function (source)
    {
        var node = $(this.id + " h4[id='" + source + "']")
        this.RemoveNode (node) ;
    }

    this.ShowFieldMessage = function (field, text, index)
    {
        if (typeof (index) == 'undefined') index = 65536 ;
        $(this.id + " [special='true']").stop (true, true).slideDown ('fast') ;
        var node = $(this.id + " li[id='" + field + "']").first () ;
        if (node.length == 0)
        {
            node = $("<li id='" + field + "' index='" + index + "'>" + text + "</li>") ;
            var nodes = $(this.id + " li") ;
            var l = 0 ;
            var r = nodes.length - 1 ;
            
            while (r > l)
            {
                var m = (l + r) / 2;
                var mi = parseInt (nodes.eq (m).attr("index")) ;
                if (mi < index)
                     l = m + 1 ;
                else r = m ;
            }

            if (nodes.length == 0)
                $(this.id + " ul").append (node) ;
            else
            {
                var ri = parseInt (nodes.eq (r).attr ("index")) ;
                if (ri < index)
                    $(this.id + " ul").append (node) ;
                else node.insertBefore (nodes.eq (r)) ;
            }
            node.hide () ;
        }
        else
        {
            node.html (text) ;
            node.attr ("index", index) ;
        }
        node.attr ('dontRemove', true) ;
        node.stop (true, true).slideDown ('fast') ;
        $(this.id).stop (true, true).slideDown ('fast') ;
    }
    
    this.HideFieldMessage = function (field)
    {
        var node = $(this.id + " li[id='" + field + "']") ;
        this.RemoveNode (node) ;
    }
    
    this.RemoveNode = function (node)
    {
        var _this = this ;
        node.removeAttr ("dontRemove") ;
        node.stop (true, true).delay ('fast').slideUp (300, function () { if (node.attr ("dontRemove")) return ; node.remove () ; _this.CheckVisibility () ; }) ;
    }
    
    this.CheckVisibility = function ()
    {
        var specialVisible = $(this.id + " li").length > 0 ;
        var boxVisible     = $(this.id + " h4[special!='true']:visible").length > 0 || specialVisible ;
        
        if (!specialVisible)
            $(this.id + " [special='true']").stop (true, true).slideUp ('fast') ;
        if (!boxVisible)
            $(this.id).stop (true, true).slideUp ('fast') ;
    }
    
    this.Clear = function ()
    {
        $(this.id + " li").remove () ;
        $(this.id + " h4[special!='true']").remove () ;
        this.CheckVisibility () ;
    }
}