﻿/// <reference path="../jQuery/jquery-1.3.2.js" />

EventCalendarList = function(eventList, display) {
    this.init(eventList, display);
}

$.extend(EventCalendarList.prototype,
{
    // object variables
    m_eventList: null, // JSON EventList
    m_accountID: GetCurrentUserAccountID(),
    m_bOwnerView: true,

    init: function(eventList, display) {
        if (this.m_accountID != GetTargetUserAccountID()) {
            this.m_accountID = GetTargetUserAccountID();
            this.m_bOwnerView = false;
        }
        // Init value
        this.m_eventList = eventList.result;
        if (display == "full") this.renderEventFull();
        if (display == "compact") this.renderEventCompact();
    },
    renderEventFull: function() {
        var todayEventOL = "<ol>";
        var nextWeekEventOL = "<ol>";
        var comingEventOL = "<ol>";
        var todayEventCount = 0;
        var nextWeekEventCount = 0;
        var comingEventCount = 0;
        var noEventHtml = "<li><center>no event at this time</center></li>";

        if (this.m_eventList != null) {
            for (i = 0; i < this.m_eventList.length; i++) {
                if (this.m_eventList[i].EventListType == "Today") {
                    todayEventOL += this.renderEventFullOL(this.m_eventList[i], i % 2);
                    todayEventCount++;
                }
                if (this.m_eventList[i].EventListType == "NextWeek") {
                    nextWeekEventOL += this.renderEventFullOL(this.m_eventList[i], i % 2);
                    nextWeekEventCount++;
                }
                if (this.m_eventList[i].EventListType == "Coming") {
                    comingEventOL += this.renderEventFullOL(this.m_eventList[i], i % 2);
                    comingEventCount++;
                }
            }
        }

        if (todayEventCount == 0) todayEventOL += noEventHtml;
        if (nextWeekEventCount == 0) nextWeekEventOL += noEventHtml;
        if (comingEventCount == 0) comingEventOL += noEventHtml;

        todayEventOL += "</ol>";
        nextWeekEventOL += "</ol>";
        comingEventOL += "</ol>";

        $("#todayEvent").html(todayEventOL);
        $("#nextWeekEvent").html(nextWeekEventOL);
        $("#comingEvent").html(comingEventOL);
    },
    renderEventFullOL: function(eventList, alt) {
        var html = "<li" + (alt ? " class=\"boxgrey\">" : ">");
        html += "<em>" + formatDate(new Date(eventList.StartDate), "NNN d, yyyy") + "</em>";

        if (eventList.EventID == -1) // Friend Birthday.
        {
            html += "<span class=\"icon_" + eventList.EventIcon.toLowerCase() + "\" >" + eventList.AccountDisplayName + (eventList.AccountDisplayName == "" ? "" : "'s ") + eventList.Title + "</span>";
            if (eventList.AccountID != GetCurrentUserAccountID()) html += "<input type=\"button\" class=\"btn_sendawish\" value=\"Send a Wish!\" onclick=\"window.location='" + GetURL("Messages/Default.aspx?AccountID=" + eventList.AccountID) + "'\" />";
        } else {
            if (eventList.AccountID != GetCurrentUserAccountID()) {
                // Invitation calendar
                html += "<a href=\"javascript:void(0);\" class=\"icon_" + eventList.EventIcon.toLowerCase() + "\" onclick=\"window.location='ManageParty.aspx?EventCalendarAction=4&EventCalendarID=" + eventList.EventID + "&AccountID=" + eventList.AccountID + "';\" >" + eventList.Title + "</a>";
            } else {
                // My calendar
                html += "<a href=\"javascript:void(0);\" class=\"icon_" + eventList.EventIcon.toLowerCase() + "\" onclick=\"window.location='ManageParty.aspx?EventCalendarAction=4&EventCalendarID=" + eventList.EventID + "&AccountID=" + eventList.AccountID + "';\" >" + eventList.Title + "</a>";
                if (this.m_bOwnerView && eventList.CanEdit) html += "<input type=\"button\" class=\"btn_btn_edit\" value=\"Edit\" onclick=\"window.location='ManageParty.aspx?EventCalendarAction=2&EventCalendarID=" + eventList.EventID + "';\"/>";
            }
        }

        html += (eventList.IsPublic) ? "" : "<i class=\"icon_lock\">Lock</i>";
        html += "</li>";
        return html;
    },
    renderEventCompact: function() {
        var compactEvent = "";
        var i = 0; var j = 0; var total = 4; var lastRecord = false;
        if (this.m_eventList != null) {
            do {
                if (j == (total - 1) || j == this.m_eventList.length - 1) lastRecord = true;
                if (this.m_eventList[i].EventListType == "Today") { j++; compactEvent += this.renderEventCompactUL(this.m_eventList[i], lastRecord); }
                if (this.m_eventList[i].EventListType == "NextWeek") { j++; compactEvent += this.renderEventCompactUL(this.m_eventList[i], lastRecord); }
                if (this.m_eventList[i].EventListType == "Coming") { j++; compactEvent += this.renderEventCompactUL(this.m_eventList[i], lastRecord); }
                if (j >= 4) break;
                i++;
            } while (i < this.m_eventList.length);
        }
        $("#compactEvent").append(compactEvent);
    },
    renderEventCompactUL: function(eventList, lastRecord) {
        var html = "";

        if (lastRecord)
            html = "<li class=\"icon_" + eventList.EventIcon.toLowerCase() + " noborderbottom\"><strong>" + eventList.AccountDisplayName + (eventList.AccountDisplayName == "" ? "" : "'s ") + "</strong> " + (eventList.AccountDisplayName == "" ? "" : eventList.EventIconName + " - ") + eventList.Title + ", " + formatDate(new Date(eventList.StartDate), "NNN d, yyyy");
        else
            html = "<li class=\"icon_" + eventList.EventIcon.toLowerCase() + "\"><strong>" + eventList.AccountDisplayName + (eventList.AccountDisplayName == "" ? "" : "'s ") + "</strong> " + (eventList.AccountDisplayName == "" ? "" : eventList.EventIconName + " - ") + eventList.Title + ", " + formatDate(new Date(eventList.StartDate), "NNN d, yyyy");

        if (eventList.EventID == -1) // Friend Birthday.
            if (eventList.AccountID != GetCurrentUserAccountID()) html += " <a class=\"icon_gif02\" href=\"javascript:void(0);\" onclick=\"window.location='" + GetURL("Messages/Default.aspx?AccountID=" + eventList.AccountID) + "'\" >send a wish</a></li>";
        else
            html += "</li>";

        return html;
    },
    toString: function() {
        return 'EventCalendarList';
    }
});
