﻿$(document).ready(function () {
    displayNotification();
});
function createNotificationMessage(results) {
    var message = '';
    var ids = '';
    var notifications = null;
    if (results != null) {
        notifications = results.d;
        var cookieValues;
        var cookieValue = getCookieValueByName(GetCookieName());        
        if (cookieValue != null && cookieValue != '') {
            cookieValues = cookieValue.split(' ');
        }
        else {
            cookieValues = '';
        }        
        for (var notification in notifications) {
            if ($.inArray(notifications[notification].ID.toString(), cookieValues) == -1) {
                message += notifications[notification].Message;
            }
            if (!notifications[notification].AlwaysDisplay) {
                ids += notifications[notification].ID + ' ';
            }
        }
        if (message != '') {
            $("#vhdaWebNotificationArea").bar({
                time: 84000000,
                message: message
            });
            ids = $.trim(ids);
            $(".jbar-cross").click(function (e) { persistViewedIDs(ids); });
        }
        else {
            persistViewedIDs(ids);
        }
    }    
}
function displayNotification() {
//  Local
//  var request = "http://localhost:9876/WebNotification.svc/Notifications";

//  Development
//  var request = "https://d-appsvhdacom/Applications/WebNotifications/WebNotification.svc/Notifications";

//  Test
//  var request = "https://t-appsvhdacom.vhda.web/Applications/WebNotifications/WebNotification.svc/Notifications";

//  Productions
    var request = "https://apps.vhda.com/Applications/WebNotifications/WebNotification.svc/Notifications";

    $.ajax({
        dataType: "jsonp",
        data: { "$filter": "substringof('" + $("#vhdaWebNotificationEnvironment").val() + "', Environment) eq true",
                "$select": "ID,Message,AlwaysDisplay",
                "$format":"json",
                "$callback": "createNotificationMessage"
            },
        contentType: "application/json; charset=utf-8",
        url: request,
        jsonpCallback: "createNotificationMessage"
    });
}
function persistViewedIDs(ids) {
    setCookie(GetCookieName(), ids, 365);
}
function GetCookieName() {
    return "VhdaWebNotifications-" + $("#vhdaWebNotificationEnvironment").val().toUpperCase();
}
function getCookieValueByName(check_name) {
    var a_all_cookies = document.cookie.split(';');
    var a_temp_cookie = '';
    var cookie_name = '';
    var cookie_value = '';
    var b_cookie_found = false;

    for (i = 0; i < a_all_cookies.length; i++) {
        a_temp_cookie = a_all_cookies[i].split('=');

        cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');

        if (cookie_name == check_name) {
            b_cookie_found = true;
            if (a_temp_cookie.length > 1) {
                cookie_value = unescape(a_temp_cookie[1].replace(/^\s+|\s+$/g, ''));
            }            
            return cookie_value;
            break;
        }
        a_temp_cookie = null;
        cookie_name = '';
    }
    if (!b_cookie_found) {
        return null;
    }
}
function setCookie(name, value, days) {
    var expires = "";
    if (days) {
        var date = new Date();
        date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
        expires = "; expires=" + date.toGMTString();
    }
    document.cookie = name + "=" + value + expires + "; path=/"; 
}
