/* For datetime extraction, see uidatetime.js */
function ajaxGetLabels(callback)
{
    ajaxCall({
        type: "GET",
        url: "labels.php",
        dataType: "json",
        success: function (response) {
            callback(response);
            return;
        }
    });
}

function createEventBox(divID) {
    if (!divID) {
        return;
    }

    if ($(".eventInfo,.eventCreate").length)
    {
        closeOpacityBox(".eventInfo,.eventCreate");
        return;
    }

    var dateTime = extractDateTime(divID);

    ajaxGetLabels(function(labelData) {
        var clickedDiv = document.getElementById(divID);
        var eventDiv = document.createElement('div');

        ajaxCall({
            type: "POST",
            url: "event_create.php",
            data: {
                dateTime: dateTime,
                labelData: labelData
            },
            success: function (response) {
                eventDiv.innerHTML = response;
            }
        });
        finishEventBoxStyling(eventDiv, clickedDiv);
    });
}

function editEventBox(eventID) {
    if (!eventID) {
        return;
    }

    closeOpacityBox(".eventInfo");

    ajaxGetLabels(function(labelData) {
        var clickedDiv = document.getElementById(eventID);
        var eventDiv = document.createElement('div');

        ajaxCall({
            type: "POST",
            url: "event_create.php",
            data: {
                eventID: eventID,
                labelData: labelData
            },
            success: function (response) {
                eventDiv.innerHTML = response;
            }
        });
        finishEventBoxStyling(eventDiv, clickedDiv);
    });
}

function submitEvent(e, id)
{
    if (e.startTime.value > e.endTime.value) {
        alert("Error: Event ends before it starts.");
        return;
    }
    $.post('db_requests/eventHandler.php', $(e).serialize(), function(e)
        {closeEventBox();});
}

function finishEventBoxStyling(eventDiv, clickedDiv) {
    eventDiv.setAttribute('class', 'eventBox z-depth-4 eventCreate');
    eventDiv.style.opacity = 0;
    clickedDiv.appendChild(eventDiv);
    eventDiv.style.transition = "opacity 0.1s ease-in-out";
    eventDiv.style.opacity = 1;

    var hourID = parseInt(clickedDiv.id.split('-')[3]);
    var rightOffset = ($('#week').width() - ($(clickedDiv).offset().left + $(clickedDiv).outerWidth()));
    var botOffset = ($('#week').height() - ($(clickedDiv).offset().top));

    if (parseInt(clickedDiv.id.split('-')[1]) === 6) {
        eventDiv.style.right = '10px';
        eventDiv.style.left = "unset";
        
    } else if (rightOffset < $(eventDiv).width()) {
        eventDiv.style.right = 0;
    }

    if (hourID === 23)
    {
        eventDiv.style.bottom = '0px';
        eventDiv.style.top = "unset";
    }
    else if ($(eventDiv).outerHeight() - botOffset > 35)
    {
        eventDiv.style.bottom = 0;
        eventDiv.style.top = "unset";
    }
    else if (botOffset < $(eventDiv).outerHeight()) 
    {
        eventDiv.style.bottom = "15px";
        eventDiv.style.top = "unset";
    }

    if (view == "week")
        eventDiv.style.width = $("#days .day-column").width() * 2 + 1 + "px";
}
