﻿function removeEventDialog(idEvent, element) {
    var idEventSaved = idEvent;
    var elementSaved = element;
    $.post('/Calendar/removeEventDialogContent', { idEvent: idEvent }, function(data) {
        $('#deleteEventDialogContent').html(data);
        $('#deleteEventDialog').dialog({
            bgiframe: true,
            autoOpen: true,
            modal: true,
            width: 400,
            buttons: {
                'Solo l\'evento': function() { $.post('/Calendar/removeEvent', { idEvent: idEventSaved, wallDelete: false, albumDelete: false }, function(data) { elementSaved.parent().parent().parent().remove(); }, 'html'); $(this).dialog('destroy'); },
                'evento+Bacheca': function() { $.post('/Calendar/removeEvent', { idEvent: idEventSaved, wallDelete: true, albumDelete: false }, function(data) { elementSaved.parent().parent().parent().remove(); }, 'html'); $(this).dialog('destroy'); },
                'tutto': function() { $.post('/Calendar/removeEvent', { idEvent: idEventSaved, wallDelete: true, albumDelete: true }, function(data) { elementSaved.parent().parent().parent().remove(); }, 'html'); $(this).dialog('destroy'); },
                'Annulla': function() { $(this).dialog('destroy'); }
            },
            close: function(event, ui) { }
        });
    }, 'html');
}

function changeEventPerm(idEvent, dialog) {
    var wallModify = new Array()
    $.each($("#adminEvent-perm-events-modify .clickCheckBox"), function(i, v) {
        wallModify.push($(v).is(":checked"));
    });
    var wallRead = new Array();
    $.each($("#adminEvent-perm-events-read .clickCheckBox"), function(i, v) {
        wallRead.push($(v).is(":checked"));
    });
    var wallWrite = new Array();
    $.each($("#adminEvent-perm-events-write .clickCheckBox"), function(i, v) {
        wallWrite.push($(v).is(":checked"));
    });
    var admins = new Array();
    $.each($('#adminEvent-show-adminEvents .usernameSelectDialog'), function(i, v) {
        admins.push($(v).text());
    });
    jQuery.ajaxSettings.traditional = true;
    $.post('/Calendar/updatePermissionEvent', { idEvent: idEvent, admins: admins, aW: wallWrite[0], fW: wallWrite[1], nW: wallWrite[2], aR: wallRead[0], fR: wallRead[1], nR: wallRead[2], aM: wallModify[0], fM: wallModify[1], nM: wallModify[2] }, function() {
        dialog.dialog('destroy'); $(".dialogEventPermission").remove();
    }, 'json');
}

function permissionEventDialog(idEvent, element) {
    $.post('/Calendar/loadAdminEventPanel', { idEvent: idEvent }, function(data) {
        $(".divDialogEventPermission").html(data);
        $(".dialogEventPermission").dialog({
            bgiframe: true,
            autoOpen: true,
            height: 400,
            width: 350,
            modal: true,
            buttons: {
                'Salva': function() {
                    changeEventPerm(idEvent, $(this));
                },
                'annulla': function() {
                    $(this).dialog('destroy');
                    $(".dialogEventPermission").remove();
                }
            },
            close: function() {
                $(this).dialog('destroy');
                $(".dialogEventPermission").remove();
            }
        });
    }, 'html');
}

function modifyEventDialog(idEvent, element) {
    var elementToModify = element;
    $.post('/Calendar/getEventForModify', { idEvent: idEvent }, function(data) {
        $('#eventModifyDialog-body').html(data);
        $('#eventModifyDialog').dialog({
            bgiframe: true,
            autoOpen: true,
            modal: true,
            width: 380,
            buttons: {
                'Applica modifiche': function() {
                    var contacts = new Array();
                    $.each($('.modifyEvent-listContactEventPanel li'), function(i, v) {
                        var contactLi = "";
                        $.each($(v).children('span'), function(j, k) {
                            contactLi += $(k).text() + ";";
                        });
                        contacts.push(contactLi);
                    });
                    var admins = new Array();
                    $.each($('.modifyEvent-postEvent-show-adminEvent .usernameSelectDialog'), function(i, v) {
                        admins.push($(v).text())
                    });
                    jQuery.ajaxSettings.traditional = true;
                    $.post('/Calendar/modifyEvent', { idEvent: idEvent, dateStart: $('#modifyEvent-dateEventStart').val(), dateEnd: $('#modifyEvent-dateEventStart').val(), hourStart: $('#modifyEvent-selectHourStartEvent option:selected').val(), minutesStart: $('#modifyEvent-selectMinutesStartEvent option:selected').val(), hourFinish: $('#modifyEvent-selectHourFinishEvent option:selected').val(), minuteFinish: $('#modifyEvent-selectMinutesFinishEvent option:selected').val(), title: $('#modifyEvent-titleEvent').val(), nameLocal: $('#modifyEvent-aliasLocation').val(), cityName: $('#modifyEvent-cityEvent').val(), adress: $('#modifyEvent-adressCity').val(), number: $('#modifyEvent-numberAdress').val(), idPhotoLogo: $('#modifyEvent-idPhotoLogoEvent div').text(), idPhotoPoster: $('#modifyEvent-idPhotoPosterEvent div').text(), contacts: contacts, adminsEvent: admins, description: $('#modifyEvent-TextareaEvent').val() }, function(data) {
                        elementToModify.html(data);
                    }, 'text');
                    $(this).dialog('destroy');
                },
                'Annulla': function() { $(this).dialog('destroy'); }
            },
            close: function(event, ui) { }
        });
    }, 'html');
}
