﻿
//Get the Events for the Month
function getMonthEvents(proxy, ModuleId, MonthId) {

    //Show the Animation
    jQuery("#CalendarAJAX").css("visibility", "visible");


    //Set service parameters
    var parametersAsJSONObject = { "ModuleID": ModuleId,
                                   "MonthID": (MonthId + 1)
                                 };


    //Set the success callback function
    var successCallback = function(data) {


        displayEventsOnCalendar(data.d)
        
        //hide and show the appropriate controls
        jQuery("#CalendarAJAX").css("visibility", "hidden");
    };

    //Call the service
    ServiceCall(proxy + "GetMonthEvents", parametersAsJSONObject, successCallback);


}

//Display the events on the calendar
function displayEventsOnCalendar(events) {

    //Initialize Calendar
    jQuery.jMonthCalendar.ReplaceEventCollection(events);

}

//Get the Event Detail
function getEventDetail(proxy, ID, overlay) {

    if (overlay.isOpened) {
        overlay.close();
    }

    //open the overlay
    overlay.load();

    //Set service parameters
    var parametersAsJSONObject = { "EventID": ID };

    //Set the success callback function
    var successCallback = function(data) {

        displayEventDetail(data.d);

        //hide and show the appropriate controls

    };

    //Call the service
    ServiceCall(proxy + "GetEventDetail", parametersAsJSONObject, successCallback);

}

//Display the Event Detail
function displayEventDetail(data) {

    var EventDetail = data;

    jQuery("#EventDetailTitle").html(EventDetail.Title);

    if (EventDetail.DateStart == EventDetail.DateEnd) {
        jQuery("#EventDetailDates").html(formatJSONDate(EventDetail.DateStart, "mmmm dd, yyyy"));
    } else {
        jQuery("#EventDetailDates").html(formatJSONDate(EventDetail.DateStart, "mmmm dd, yyyy") + " - " + formatJSONDate(EventDetail.DateEnd, "mmmm dd, yyyy"));
    }

    if ((EventDetail.TimeStart != null) && (EventDetail.TimeEnd != null)) {
        jQuery("#EventDetailTimes").html(convertMilitaryToStandard(EventDetail.TimeStart) + " - " + convertMilitaryToStandard(EventDetail.TimeEnd));
    }
    
    jQuery("#EventDetailDescription").html(EventDetail.Description);
    
}

//Show the More Events Tooltip
function showToolTip(proxy, callerID, eventArray, overlayAPI) {

    //Get Position of Elements
    var showMoreElement = jQuery("#" + callerID);

    var position = showMoreElement.offset();
    
    var showMoreElementHeight = showMoreElement.height();
    var showMoreElementWidth = showMoreElement.width();

    var toolTipWidth = jQuery("#ToolTip").width();
    var toolTipHeight = jQuery("#ToolTip").height();

    var toolTipLeft = (position.left + showMoreElementWidth) - 5;
    var toolTipTop = ((position.top + showMoreElementHeight) - toolTipHeight);

    //Setup the ToolTip
    jQuery("#ToolTip").css("left", toolTipLeft);
    jQuery("#ToolTip").css("top", toolTipTop);
    jQuery("#ToolTip").css("display", "block");

    var imageSrc = "/Resources/Shared/images/jQueryPlugins/jQueryTools/ToolTip/Close.png";
    var imageSrcHover = "/Resources/Shared/images/jQueryPlugins/jQueryTools/ToolTip/CloseHover.png";

    jQuery("#CloseToolTip").mouseover(function() {
        jQuery(this).attr("src", imageSrcHover);
    }).mouseout(function() {
        jQuery(this).attr("src", imageSrc);
    });

    //StringBuffer is custom function
    var dayEventsHTML = new StringBuffer();

    //Get the Month
    var month = jQuery(".MonthName").html().replace(/[0-9]/g, "");

    //Get the Day
    var monthDay = jQuery(showMoreElement).attr("id").replace(/\D/g, "") - 1;
    
    //Display the Month/Day in the ToolTip
    dayEventsHTML.append("<div class='dayTitle'>" + month + " " + monthDay + "</div>");
    
    dayEventsHTML.append("<ul>");
    
    //Show the Events in the ToolTip
    for (var index = 0, length = (eventArray.length); index < length; index++) {

        dayEventsHTML.append("<li><a class='moreEventsEventLink' eventID='" + eventArray[index].ID  + "'>" + eventArray[index].Title + "</a></li>");

    }

    dayEventsHTML.append("</ul>");
    
    //Set the ToolTip HTML
    jQuery("#ToolTipContent").html(dayEventsHTML.toString());

    //Set up the click event for a link in the More Events ToolTip
    jQuery(".moreEventsEventLink").click(function() {
        getEventDetail(proxy, jQuery(this).attr("eventID"), overlayAPI);
    });
}

//Close the ToolTip
function closeToolTip() {

    //Hide the ToolTip
    jQuery("#ToolTip").css("display", "none");

    //Clear out the ToolTip HTML
    jQuery("#ToolTipContent").html("");
    
}
