﻿/************************************************************************  
                                VARS
**************************************************************************/
var _CalendarEventPage = "SecondIINoneCalendarEvent.aspx?in=y";
var _isAuthenticated = false;
var _isOfficer = false;
var _currentUserId = 0;
var _selectedOrganizationId = 0;

/************************************************************************ 
                        Page Element Enums
**************************************************************************/
var CalendarElements =
{
    DivNewCalendarEvent: 'divNewCalendarEvent',
    DivChapters: 'divChapters',
    DdlChapters: 'ddlChapters'
}

/************************************************************************ 
                        Functions  
**************************************************************************/
$(document).ready(function () {

    showWaitDialog(InformationMessages.CalendarEventRetrieval);
    loadSecondIINoneMCCalendar();
});

function loadSecondIINoneMCCalendar() {
    getOrganizationsUsingService();
    getSecondIINoneCalendarEventsUsingService();
}

function getOrganizationsUsingService() {
    SecondIINoneUIService.GetOrganizations(onGetOrganizationsComplete, onSecondIINoneCalendarEventsError);
}

function onGetOrganizationsComplete(organizations) {
    var element = $get(CalendarElements.DdlChapters);

//    showContent($get(CalendarElements.DivChapters));

    populateSecondIINoneOrganizationsWithSelectedOrganizationId(element, organizations, _selectedOrganizationId);
}

function newEvent() {
    window.location = _CalendarEventPage;
}

function getSecondIINoneCalendarEventsBySelectedOrganization() {
    showWaitDialog(InformationMessages.CalendarEventRetrieval);

    _selectedOrganizationId = getSelectedValue($get(CalendarElements.DdlChapters));

    getSecondIINoneCalendarEventsUsingService();
}

function getSecondIINoneCalendarEventsUsingService() {

    CalendarUIService.GetCalendarEvents(_selectedOrganizationId, onGetSecondIINoneCalendarEventsComplete, onSecondIINoneCalendarEventsError);
}

function onGetSecondIINoneCalendarEventsComplete(secondIInoneCalendarEvents) {
    var calendarEventList = "{ events: []}";
    
    if (secondIInoneCalendarEvents.length > 0) {
        var eventsString = "{ events: [";

        _isAuthenticated = secondIInoneCalendarEvents[0].IsAuthenticated;
        _isOfficer = secondIInoneCalendarEvents[0].IsOfficer;
        _currentUserId = secondIInoneCalendarEvents[0].CurrentUserId;

        if (_isOfficer) {
            showContent($get(CalendarElements.DivNewCalendarEvent));
        }
        else {
            hideContent($get(CalendarElements.DivNewCalendarEvent));
        }

        for (var secondIInoneCalendarEventRowCount = 0; secondIInoneCalendarEventRowCount < secondIInoneCalendarEvents.length; secondIInoneCalendarEventRowCount++) {
            var startDate = new Date(secondIInoneCalendarEvents[secondIInoneCalendarEventRowCount].Startdate);
            var startDateDay = startDate.getDate();
            var startDateMonth = startDate.getMonth();
            var startDateYear = startDate.getFullYear();
            var startDateHour = startDate.getHours();
            var startDateMinute = startDate.getMinutes();
            var startDateSecond = startDate.getSeconds();

            var endDate = new Date(secondIInoneCalendarEvents[secondIInoneCalendarEventRowCount].Enddate);
            var endDateDay = endDate.getDate();
            var endDateMonth = endDate.getMonth();
            var endDateYear = endDate.getFullYear();
            var endDateHour = endDate.getHours();
            var endDateMinute = endDate.getMinutes();
            var endDateSecond = endDate.getSeconds();

            if (secondIInoneCalendarEventRowCount < secondIInoneCalendarEvents.length - 1) {
                eventsString += "{title: '" + secondIInoneCalendarEvents[secondIInoneCalendarEventRowCount].Name + "',start: new Date('" + startDateYear + "', '" + startDateMonth + "', '" + startDateDay + "', '" + startDateHour + "', '" + startDateMinute + "', '" + startDateSecond + "'), end: new Date('" + endDateYear + "', '" + endDateMonth + "', '" + endDateDay + "', '" + endDateHour + "', '" + endDateMinute + "', '" + endDateSecond + "'),allDay: false,url: 'SecondIINoneCalendarEvent.aspx?id=" + secondIInoneCalendarEvents[secondIInoneCalendarEventRowCount].Id + "'},";
            }
            else {
                eventsString += "{title: '" + secondIInoneCalendarEvents[secondIInoneCalendarEventRowCount].Name + "',start: new Date('" + startDateYear + "', '" + startDateMonth + "', '" + startDateDay + "', '" + startDateHour + "', '" + startDateMinute + "', '" + startDateSecond + "'), end: new Date('" + endDateYear + "', '" + endDateMonth + "', '" + endDateDay + "', '" + endDateHour + "', '" + endDateMinute + "', '" + endDateSecond + "'),allDay: false,url: 'SecondIINoneCalendarEvent.aspx?id=" + secondIInoneCalendarEvents[secondIInoneCalendarEventRowCount].Id + "'}";
            }
        }

        eventsString += "]}";

        calendarEventList = eval(eventsString);
    }

    $('#calendar').empty();

    $('#calendar').fullCalendar({ header: {
        left: 'prev,next today',
        center: 'title',
        right: 'month,agendaWeek,agendaDay'
    },
        editable: false,
        events: calendarEventList
    });

    hideWaitDialog();

    performPostLoadOperations();
}

function onSecondIINoneCalendarEventsError(result) {
    var problems = new Array();
    problems[problems.length] = result.get_message();
    setErrorInformation($get(LayoutElements.DivError), problems);

    hideWaitDialog();
}
