﻿// TODO: set this to false when in production mode to cancel the errors being displayed
var notificationsDebugMode = true;

function getNotificationsCount(firstTime) {
    try {
        if (isUserLoggedIn()) {

            $.ajax({
                url: "/proxy.aspx",
                data: "command=notificationsGetCount&u=" + getUName(),
                success: function (data) {
                    setNotificationsCount(data, firstTime);
                }
            });
        }
    } catch (e) {
        appendError("getNotificationsCount : " + e.message);
    }
};


function getUserScore() {
    try {
        if (isUserLoggedIn()) {
            $.ajax({
                url: "/proxy.aspx",
                data: "command=scoreGetUserScore&u=" + getUName(),
                success: function (data) {
                    setUserScore(data);
                }
            });
        }
    } catch (e) {
        appendError("getUserScore : " + e.message);
    }
};

function getNotificationContent() {
    try {
        if (isUserLoggedIn()) {
            $.ajax({
                url: "/proxy.aspx",
                data: "command=notificationsGetContent&u=" + getUName(),
                success: function (data) {
                    var fragmentContent = data + " &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; " +
                                                     " <p><a href='/notifications.aspx' id='btnNotifications' class='arrow-link'>See all Updates</a></p> ";
                    $("#pnlFragmentContent").html(fragmentContent);
                    AssignNotificationEvents();
                }
            });
        }
    } catch (e) {
        appendError("getNotificationContent : " + e.message);
    }
}


function hideNotificationsPanel() {
    tb_remove();
}


function setNotificationsCount(noOfNotifications, firstTime) {
    if (isNaN(noOfNotifications)) {
        $("#btnNotificationsCount").html("<span>&nbsp;</span>");
    }
    else {
        $("#btnNotificationsCount").html("<span>" + noOfNotifications + "</span> NEW MESSAGES");
        if (firstTime == 1) {
            showNotificationsIfJustLoggedIn(noOfNotifications);
        }
    }
}

function setUserScore(userScore) {
    $(".notification-user-points").html("<span>" + userScore + "</span> points");
}

function markAsReadAndHideThePanel() {
    try {
        if (isUserLoggedIn()) {
            $.ajax({
                url: "/proxy.aspx",
                data: "command=notificationsMarkAsReadAndGetNotificationsCount&u=" + getUName(),
                success: function (data) {
                    setNotificationsCount(data);
                    hideNotificationsPanel();
                }
            });
        }
    } catch (e) {
        appendError("markAsReadAndHideThePanel : " + e.message);
    }
}


function appendError(errMsg) {
    if (notificationsDebugMode) {
        $("#pnlNotificationErrorMessages").show();
        $("#pnlNotificationErrorMessages").append(errMsg + "<br />");
    }
}

function clearErrorsAndHideTheDiv() {
    $("#pnlNotificationErrorMessages").html("");
    $("#pnlNotificationErrorMessages").hide();
}


// EVENT assignation
function AssignNotificationEvents() {
    $("#btnNotificationsCount").unbind("click").click(function () {
        showNotifications();
    });


    $("#btnMarkAsRead").unbind("click").click(function () {
        markAsReadAndHideThePanel();
    });


    $("#btnClose").unbind("click").click(function () {
        hideNotificationsPanel();
    });

    $("#btnNotificationsCount").attr("href", "javascript:void(0);");
}

function setNotificationsVisibilityBasedOnLoginState() {
    if (!isUserLoggedIn()) {
        $("#user-notifications-container").hide();
    }
}

function showNotificationsIfJustLoggedIn(numberOfNotifications) {
    var notificationVisible = getQuerystringValue("notification");
    if (typeof (notificationVisible) != undefined && notificationVisible == 1 && numberOfNotifications != "0") {
        showNotifications();
    }
}

function showNotifications() {
    // add the loading text while the AJAX call is being processed                
    $("#pnlFragmentContent").html("<div style='text-align:center;vertical-align:middle'>loading ...</div>");

    clearErrorsAndHideTheDiv();
    // make the panel visible   
    tb_show("", "#TB_inline?inlineId=pnlNotifications", null);

    // update the count on click
    getNotificationsCount(0);
    // get the user score
    getUserScore();
    // make de AJAX call that will get the notifications from the server
    getNotificationContent();
    // assign the events for the close link
    AssignNotificationEvents();
}        
