function UpdateNotificationIcon(element) {
    $(element).parents("div:first").css("display", "none");

    var childCount = 0;
    var emptyCount = 0;
    var imageLink = '';

    var parentTd = $(element).parents("td:first");
    $(".NotificationMess", parentTd).each(function() {
        childCount++;
        if ($(this).css("display") == "none") {
            emptyCount++;
        }
        else {
            imageLink = $("input", this).attr("value");
        }
    });

    if (childCount == emptyCount)
        $('.AvailableUpdates').each(function() { this.style.display = 'none'; });
    else if (childCount - emptyCount == 1)
        $('.NotifImage').each(function() { this.src = imageLink; })
}

function AcknowledgeNotificationItemsByType(element, type, createdBefore) { 
	$.ajax({
		type: 'Post',
		url: '/Orion/Services/NotificationPanel.asmx/AcknowledgeNotificationItemsByType',
		data: "{'type': '" + type + "', 'createdBefore': '" + createdBefore + "'}",
		contentType: 'application/json; charset=utf-8',
		dataType: 'json',
		success: UpdateNotificationIcon(element),
		error: function(error) {
		}
	});
}

function AcknowledgeNotificationItemById(element, notificationId, createdBefore) {
    $.ajax({
        type: 'Post',
        url: '/Orion/Services/NotificationPanel.asmx/AcknowledgeNotificationItemById',
        data: "{'notificationId': '" + notificationId + "', 'createdBefore': '" + createdBefore + "'}",
        contentType: 'application/json; charset=utf-8',
        dataType: 'json',
        success: UpdateNotificationIcon(element),
        error: function(error) {
        }
    });
}

function AcknowledgeAllNotificationItems(element, createdBefore) {
    $.ajax({
        type: 'Post',
        url: '/Orion/Services/NotificationPanel.asmx/AcknowledgeAllNotificationItems',
        data: "{'createdBefore': '" + createdBefore + "'}",
        contentType: 'application/json; charset=utf-8',
        dataType: 'json',
        success: function() {
            $('.AvailableUpdates').each(function() { this.style.display = 'none'; });
        },
        error: function(error) {
        }
    });
}

function ShowPollerLimitStatus(engineNames) {    
    var html = '<div id="PollerLimitStatus">' +
               '  <div style="padding: 3px 10px;">' +
               '    <div>' +
               '      <div id="warningLimitDetailsList"> '+ 
               '         <p>The following pollers are approaching their polling rate limits:</p>' +
               '         <ul/>' +               
               '         </div>' +
               '      <div id="reachedLimitDetailsList">'+
               '         <p>The following pollers are over their polling rate limits:</p>' +
               '         <ul/>' +               
               '       </div>' +
               '    </div>' +
               '    <p style="margin-bottom:40px;">' +
               '        If the polling rate limit for any poller is reached, polling intervals for that poller will be increased automatically.' +
               '         <span class="LinkArrow">&#0187;</span>' + 
               '        <a class="notificationDialogLink" href="http://knowledgebase.solarwinds.com/kb/questions/3227/" target="_blank">More Details</a>' +
               '    </p>' +
               '    <div style="position:absolute;bottom:10px;right:10px;padding-right:5px;" >' +
               '        <span style="padding-right:5px;" >' +
               SW.Core.Widgets.Button('Add a Poller', { type: 'primary', id: 'pollerStatusAddPoller', href: 'http://www.solarwinds.com/embedded_in_products/productLink.aspx?id=NPM_BuyNewPoller', target: '_blank' }) +
               '        </span>' +
               '        <span>' +
               SW.Core.Widgets.Button('Close', { id: 'maintenanceStatusClose' }) +
               '        </span>' +
               '    </div>' +
               '  </div>' +
               '</div>';
    var dlg = $(html);

    $('#maintenanceStatusClose,#pollerStatusAddPoller', dlg).click(function () {
        dlg.dialog("close");
    });

    var separate = function (engineNames) {
        var items = '';

        if (engineNames.length > 0) {
            $.each(engineNames.split('|'), function () {
                var engine = this.split(';');
                var serverName = engine[0];
                var scaleFactor = parseInt(engine[1]);
                var text = SW.Core.String.Format('<li><b>{0}</b> is at {1}% of its maximum polling rate. </li>', serverName, scaleFactor);
                items += text;
            });
        }
        return items;
    }

    function displayEngineNames(id, engineNames) {
        var items = separate(engineNames);
        if (items.length > 0) {
            $('#'+id+' ul', dlg).empty().append(items);
        }
        else {
            $('#'+id, dlg).hide();
        }
    };

    var warningEngineNames = engineNames.split("||")[0];
    var reachedEngineNames = engineNames.split("||")[1];

    displayEngineNames('warningLimitDetailsList', warningEngineNames);
    displayEngineNames('reachedLimitDetailsList', reachedEngineNames);

    dlg.dialog({
        title: 'Poller Status',
        width: 500,
        height: 'auto',
        modal: true,
        minHeight: 250
    });
}

function ShowMaintenanceStatus(expirations) {
    var html = '<div id="MaintenanceStatus">' +
               '  <div style="padding: 3px 10px;">' +
               '    <div>' +
               '       <p>The maintenance of the following licensed products is about to expire:' +
               '       <ul id="maintenanceDetailsList"/>' +
               '    </div>' +                              
               '    <div style="position:absolute;bottom:10px;right:10px;padding-right:5px;" >' +
               '        <span style="padding-right:5px;" >' +
               SW.Core.Widgets.Button('Renew Now', { type: 'primary', id: 'maintenanceStatusRenew', href: 'http://www.solarwinds.com/support/maintenance.aspx', target: '_blank' }) +
               '        </span>' +
               '        <span>' +
               SW.Core.Widgets.Button('Close', { id: 'maintenanceStatusClose' }) +
               '        </span>' +
               '    </div>' +
               '  </div>' +
               '</div>';
    var dlg = $(html);

    $('#maintenanceStatusRenew,#maintenanceStatusClose', dlg).click(function () {
        dlg.dialog('close');
    });

    var fmt = SW.Core.String.Format;

    var items = '';

    $.each(expirations.split('|'), function () {
        var module = this.split(';');
        var moduleName = module[0];
        var days = parseInt(module[1]);

        var countTxt = days <= 0 ? 'expired' : days == 1 ? '1 day left' : fmt('{0} days left', days);
        items += '<li><b>' + moduleName + '</b> - ' + countTxt + '</li>';
    });
    $('#maintenanceDetailsList', dlg).empty().append(items);

    dlg.dialog({
        title: 'Maintenance Status',
        width: 500,
        height: 250,
        modal: true
    });
}
