﻿/// <reference name="MicrosoftAjax.debug.js" />
/// <reference name="MicrosoftAjaxTimer.debug.js" />
/// <reference name="MicrosoftAjaxWebForms.debug.js" />

Type.registerNamespace("NCI");
NCI.MessagePanelType = { Error: 1, Warning: 2, Info: 3, Success: 4 };
var MessagePanel = null;

NCI.MessagePanel = function(element) {
    NCI.MessagePanel.initializeBase(this, [element]);
}

NCI.MessagePanel.prototype = {
    initialize: function() {
        NCI.MessagePanel.callBaseMethod(this, "initialize");
        MessagePanel = this;
    },

    dispose: function() {
        NCI.MessagePanel.callBaseMethod(this, 'dispose');
    },

    add_message: function(identifier, message, messageType) {
        if (!identifier || !message || !messageType)
            return;
        this.remove_message(identifier, messageType);
        var appropriateMessagePanel = this._get_containing_panel(messageType);
        if (appropriateMessagePanel) {
            $('<div>' + message + '</div>').attr('messageId', identifier).appendTo(appropriateMessagePanel);
            appropriateMessagePanel.css('display', 'block');
        }
    },
    add_message_link: function(identifier, message, messageType, name) {
        if (!identifier || !message || !messageType)
            return;
        this.remove_message(identifier, messageType);
        var appropriateMessagePanel = this._get_containing_panel(messageType);
        if (appropriateMessagePanel) {
            var txt = '<div><a href="#' + name + '">' + message + '</a></div>';
            $(txt).attr('messageId', identifier).appendTo(appropriateMessagePanel);
            appropriateMessagePanel.css('display', 'block');
        }
    },
    remove_message: function(identifier, messageType) {
        if (!identifier || !messageType)
            return;

        var appropriateMessagePanel = this._get_containing_panel(messageType);
        if (appropriateMessagePanel) {
            $(appropriateMessagePanel).find('[messageId=' + identifier + ']').remove();

            var hasVisibleItems = false;
            $(appropriateMessagePanel).find('*').each(function() {
                if (!hasVisibleItems && this.nodeName != 'BR' && this.style['display'] != 'none') {
                    hasVisibleItems = true;
                }
            });

            if (!hasVisibleItems) {
                $(appropriateMessagePanel).css('display', 'none');
            }
        }
    },

    _get_containing_panel: function(messageType) {
        var appropriateMessagePanel = null;

        switch (messageType) {
            case NCI.MessagePanelType.Error:
                appropriateMessagePanel = $('.error');
                break;
            case NCI.MessagePanelType.Warning:
                appropriateMessagePanel = $('.warning');
                break;
            case NCI.MessagePanelType.Success:
                appropriateMessagePanel = $('.success');
                break;
            case NCI.MessagePanelType.Info:
                appropriateMessagePanel = $('.info');
                break;
        }

        return appropriateMessagePanel;
    }
}

NCI.MessagePanel.registerClass("NCI.MessagePanel", Sys.UI.Control);


if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();