﻿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) {
        }
    });
}