/*
 * CMC Portal JavaScript Library v1.0
 *
 * Created by Henry Tavarez
 * Date: 2009-09-08 (Tue, Sep 8th, 2009)
 */


$(document).ready(function() {

    $("#cmcStudentMsgTray").each(function() {

        /******************************************************************************
        All variables that can be configured appear in this section
        ******************************************************************************/
        var msgTray = {
            traySpeed: 500, // tray animation speed in milliseconds        
            noAlertLabel: "You currently have no alerts on record.", // message that displays when there are no alerts
            noHoldLabel: "You currently have no holds on record.", // message that displays when there are no holds
            noApptLabel: "You currently have no appointments scheduled.", // message that displays when there are no appointments
            closeLabel: "Close", // label that appears for the close link... it's usually replaced in the CSS with an icon but the label is needed for accessibility
            viewAllAlertsLabel: "View all alerts", // label that appears for the view all link in the alerts section
            viewAllHoldsLabel: "View all holds", // label that appears for the view all link in the holds section
            viewAllApptsLabel: "View all appointments", // label that appears for the view all link in the appointments section
            noServiceLabel: "Your messages are currently unavailable at this time.", // message that displays when the service is unavailable
            noMessagesLabel: "You do not have any new messages at this time.", // message that displays when the service returns zero messages

            /******************************************************************************
            Do not change any code after this line 
            ******************************************************************************/
            trayOpen: false,
            trayXML: lvBaseURL + "/PortalAsyncServices/StudentAsyncService.asmx/GetStudentMessageList", //URL for the service that returns the XML of the message center tray
            holdURL: lvBaseURL + "/secure/student/Prof/msg_Home.aspx?tab=1", // URL to the page that normally displays holds
            alertURL: lvBaseURL + "/secure/student/Prof/msg_Home.aspx?tab=1", // URL to the page that normally displays alerts
            apptURL: lvBaseURL + "/secure/student/Prof/msg_Home.aspx?tab=2", // URL to the page that normally displays appointments
            alertHeader: $("#cmcTrayAlerts").html(),
            holdHeader: $("#cmcTrayHolds").html(),
            apptHeader: $("#cmcTrayAppts").html()
        };


        // HTML thats being appended to the page specifically for the tray. These are the sections that the messages will be appended to
        var trayHTML = "<div id='cmcMsgHolds'><h2>" + msgTray.holdHeader.substring(msgTray.holdHeader.indexOf("</") + 5, msgTray.holdHeader.lastIndexOf("</")) + "</h2><ol></ol><a href='#' class='cmcCloseMsgTray'>" + msgTray.closeLabel + "</a><a href='" + msgTray.holdURL + "' class='cmcViewAll'>" + msgTray.viewAllHoldsLabel + "</a></div>";
        trayHTML += "<div id='cmcMsgAlerts'><h2>" + msgTray.alertHeader.substring(msgTray.alertHeader.indexOf("</") + 5, msgTray.alertHeader.lastIndexOf("</")) + "</h2><ol></ol><a href='#' class='cmcCloseMsgTray'>" + msgTray.closeLabel + "</a><a href='" + msgTray.alertURL + "' class='cmcViewAll'>" + msgTray.viewAllAlertsLabel + "</a></div>";
        trayHTML += "<div id='cmcMsgAppts'><h2>" + msgTray.apptHeader.substring(msgTray.apptHeader.indexOf("</") + 5, msgTray.apptHeader.lastIndexOf("</")) + "</h2><ol></ol><a href='#' class='cmcCloseMsgTray'>" + msgTray.closeLabel + "</a><a href='" + msgTray.apptURL + "' class='cmcViewAll'>" + msgTray.viewAllApptsLabel + "</a></div>";
        $(this).append(trayHTML);

        // Click Event Listener for close link
        $(this).find("a.cmcCloseMsgTray").click(function() {
            msgTray.trayOpen = false;
            $(this).parent().slideUp(msgTray.traySpeed);
            $("#cmcStudentMsgTray ul a").removeClass("cmcTrayTabActive");
            return false;
        });

        // Tray Navigation Event Listener. Clicking each link will open the corresponding tray section
        $(this).find("#cmcTrayHolds a").click(function() {
            loadTrayMeassages("Hold", msgTray);
            return false;
        });
        $(this).find("#cmcTrayAlerts a").click(function() {
            loadTrayMeassages("Alert", msgTray);
            return false;
        });
        $(this).find("#cmcTrayAppts a").click(function() {
            loadTrayMeassages("Appointment", msgTray);
            return false;
        });

    });
});

function loadTrayMeassages(lvMsgType, trayObj) {
    switch (lvMsgType) {
        case "Alert":
            var msgSection = "#cmcMsgAlerts";
            var navSection = "#cmcTrayAlerts";
            var msgURL = trayObj.alertURL;
            var noMsgLabel = trayObj.noAlertLabel;
            var noDate = false;
            break;
        case "Hold":
            var msgSection = "#cmcMsgHolds";
            var navSection = "#cmcTrayHolds";
            var msgURL = trayObj.holdURL;
            var noMsgLabel = trayObj.noHoldLabel;
            var noDate = true;
            break;
        case "Appointment":
            var msgSection = "#cmcMsgAppts";
            var navSection = "#cmcTrayAppts";
            var msgURL = trayObj.apptURL;
            var noMsgLabel = trayObj.noApptLabel;
            var noDate = false;
            break;
    }
    
    // Ajax to get messages. On success, function parses XML, gets count of each message type and creates HTML for tray
    $.ajax({
        type: "POST",
        url: trayObj.trayXML,
        cache: false,
        data: { msgType: lvMsgType }, //sortDirection: "Descending"
        success: function(data) {
            var msgCount = 0;
            $(msgSection + " ol").html("");

            $("StudentMessage", data).each(function() {
                msgCount++;
                var msgDate = "";

                if (!noDate) {
                    msgDate = "<span>" + $(this).find("DisplayDate").text() + "</span> ";
                }

                var msgSubject = $(this).find("Subject").text();
                if (msgSubject.length > 128) msgSubject = msgSubject.substring(0, 128) + "...";

                $(msgSection + " ol").append("<li>" + msgDate + "<a href='" + msgURL + "'>" + msgSubject + "</a></li>");
            });
            
            if (msgCount == 0) {
                $(msgSection + " ol").html("<li>" + noMsgLabel + "</li>");
                if (lvMsgType == "Hold") $("#cmcMsgHolds .cmcViewAll").hide();
            } else {
                if (lvMsgType == "Hold") $("#cmcMsgHolds .cmcViewAll").show();
            }

            showTray(navSection, msgSection, trayObj);
        },
        error: function(XMLHttpRequest, textStatus, errorThrown) {
            $(msgSection + " ol").html("<li>" + trayObj.noServiceLabel + "</li>");
            showTray(navSection, msgSection, trayObj);
        }

    });
}

function showTray(navSection, msgSection, trayObj) {
    $("#cmcStudentMsgTray div").hide();
    $("#cmcStudentMsgTray ul a").removeClass("cmcTrayTabActive");
    if (trayObj.trayOpen) {
        $(msgSection).show();
    } else {
        trayObj.trayOpen = true;
        $(msgSection).slideDown(trayObj.traySpeed);
    }
    $(navSection + " a").addClass("cmcTrayTabActive");
}
