﻿var pageAlbum = 0;

var idPhotoAlbumevent;
function showContentAlbumDialogNormal(idAlbum, el) {
    el.parent().children().css({ 'background-color': 'Black' });
    el.css({ 'background-color': 'Red' });
    idPhotoAlbumevent = idAlbum;
}

function loadPageAlbumPreview(nPage, element) {
    $.post('/Gallery/getRangeAlbumUser', { page: nPage }, function(data) { $('#previewAlbumArea').html(data); element.parent().children().css({ 'color': 'White', 'text-decoration': 'none' }); ; element.css({ 'color': 'red', 'text-decoration': 'underline' }); }, 'html');
}

function deleteAlbumPortletOpenDialog(idAlbum, el) {
    var idAlbumDelete = idAlbum;
    var elementPortlet = el;
    $.post('/Gallery/removeAlbumDialogContent', { idAlbum: idAlbum }, function(data) {
        $('#deleteAlbumDialogContent').html(data);
        $('#deleteAlbumDialog').dialog({
            bgiframe: true,
            autoOpen: true,
            modal: true,
            width: 400,
            buttons: {
                //$.cookie('selectedTab').substr(1)
                'Continua': function() { $.post('/Gallery/removeAlbum', { idAlbum: idAlbumDelete }, function(data) { elementPortlet.parent().parent().remove(); }, 'html'); $(this).dialog('destroy'); },
                'Annulla': function() { $(this).dialog('destroy'); }
            },
            close: function(event, ui) { }
        });
    }, 'html');
}

function openDialogModifyAlbumPhoto(idAlbum, elementDivDialog, elementText) {
    $.post('/Gallery/getDialogEditAlbum', { idAlbum: idAlbum }, function(data) {
        elementDivDialog.html(data);
        $('.dialogEditAlbum').dialog({
            bgiframe: true,
            autoOpen: true,
            modal: true,
            buttons: {
                'Salva': function() {
                    elementText.html($('.dialogEditAlbum-nameAlbum').val());
                    $.post('/Gallery/editAlbum', { idAlbum: idAlbum, nameAlbum: $('.dialogEditAlbum-nameAlbum').val() }, function(result) { $('.dialogEditAlbum').remove(); }, 'json');
                    $(this).dialog('destroy');
                },
                'Annulla': function() { $('.dialogEditAlbum').remove(); $(this).dialog('destroy'); }
            },
            close: function(event, ui) { $('.dialogEditAlbum').remove(); }
        });
    }, 'html');
}

function editNamePhoto(idPhoto, element, elementDialog, classNamePhoto) {
    elementDialog.dialog({
        bgiframe: true,
        autoOpen: true,
        modal: true,
        width: 200,
        buttons: {
            //$.cookie('selectedTab').substr(1)
            'Salva': function() {
                $.post('/Gallery/updateNamePhoto', { idPhoto: idPhoto, namePhoto: elementDialog.find('.eidtNamePhoto').val() }, function(data) {
                    classNamePhoto.html(elementDialog.find('.eidtNamePhoto').val());
                }, 'html'); $(this).dialog('destroy');
            },
            'Annulla': function() { $(this).dialog('destroy'); }
        },
        close: function(event, ui) { }
    });
}

function deleteAlbumPhoto(idAlbum, el) {
    $.post('/Gallery/removeAlbum', { idAlbum: idAlbum }, function(data) { el.parent().parent().remove(); }, 'html');
}

function openDialogPermAlbum(idAlbum) {
    $.post('/Gallery/dialogPermAlbum', { idAlbum: idAlbum }, function(data) {
        $('#dialogPermAlbum-extern').html(data);
        $('#dialogPermAlbum').dialog({
            bgiframe: true,
            autoOpen: true,
            modal: true,
            buttons: {
                'Salva': function() { saveChangePermAlbum($(this), idAlbum); },
                'annulla': function() { $('#dialogPermAlbum').remove(); $(this).dialog('destroy'); }
            }
        });
    }, 'html');
}

function saveChangePermAlbum(element, idAlbum) {
    jQuery.ajaxSettings.traditional = true;

    var wallModify = new Array();
    $.each($("#albumArea-perm-album-modify .clickCheckBox"), function(i, v) {
        wallModify.push($(v).is(":checked"));
    });
    var wallRead = new Array();
    $.each($("#albumArea-perm-album-read .clickCheckBox"), function(i, v) {
        wallRead.push($(v).is(":checked"));
    });
    var wallWrite = new Array();
    $.each($("#albumArea-perm-album-write .clickCheckBox"), function(i, v) {
        wallWrite.push($(v).is(":checked"));
    });
    var admins = new Array();
    $.each($('.usernameSelectDialog'), function(i, v) {
        admins.push($(v).text());
    });
    $.post('/Gallery/saveChangePermAlbum', { idAlbum: idAlbum, 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(data) { $('#dialogPermAlbum').remove(); $(this).dialog('destroy'); }, 'json');
}

function evidenziaFoto(elementHilight, elementDelete, elementDiv) {
    elementDelete.show();
    elementHilight.show();
    //elementDiv.css({ 'background-color': 'Yellow' });
}

function nonEvidenziaFoto(elementHilight, elementDelete, elementDiv) {
    elementDelete.hide();
    elementHilight.hide();
    //elementDiv.css({ 'background-color': 'Black' });
}

var idPhotoLogoEvent;
function selectPhotoNormal(idPhoto, el) {
    selezionaAlbumLogo = 1;
    el.parent().children().css({ 'background-color': 'Black' });
    el.css({ 'background-color': 'Red' });
    idPhotoLogoEvent = idPhoto;
}

function deletePhoto(idPhoto, el) {
    $.post('/Gallery/removePhoto', { idPhoto: idPhoto }, function(data) { el.parent().parent().remove(); }, 'html');
}

function showContentAlbumDialog(id, idPlace) {
    $.post('/Gallery/getContentAlbumDialogSelect', { idAlbum: id, page: pageAlbum, idPlace: idPlace }, function(h) { $("#previewAlbum").html(h); }, 'html');
}

function showContentAlbum(data) {
    $.post($(data).attr('title'), { idAlbum: data.attr('id'), page: pageAlbum }, function(h) { $("#previewAlbum").html(h); }, 'html');
}

function showDefaultPermissionAlbumPhoto(elementTarget) {
    $.post('/Gallery/getDefaultPermAlbum', {}, function(data) { elementTarget.html(data); elementTarget.show('slide', {}, 1000); }, 'html');
}

function createAlbum(dialog) {
    jQuery.ajaxSettings.traditional = true;

    var wallModify = new Array();
    $.each($("#albumArea-perm-album-modify .clickCheckBox"), function(i, v) {
        wallModify.push($(v).is(":checked"));
    });
    var wallRead = new Array();
    $.each($("#albumArea-perm-album-read .clickCheckBox"), function(i, v) {
        wallRead.push($(v).is(":checked"));
    });
    var wallWrite = new Array();
    $.each($("#albumArea-perm-album-write .clickCheckBox"), function(i, v) {
        wallWrite.push($(v).is(":checked"));
    });
    var admins = new Array();
    $.each($('.usernameSelectDialog'), function(i, v) {
        admins.push($(v).text());
    });
    if (wallModify == null || wallModify.length == 0) {
        $.post('/Gallery/newAlbumDefaultPerm', { nameAlbum: dialog.find('input').val(), makeWall: $('.dialogNewALbum-makeWall').is(':checked') }, function(data) {
            $('#previewAlbumArea').prepend(data);
            $('#newAlbumDialog').remove();
            dialog.dialog('destroy');
        }, 'html');
    } else {
        jQuery.ajaxSettings.traditional = true;
        $.post('/Gallery/newAlbum', { nameAlbum: dialog.find('input').val(), 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], makeWall: $('.dialogNewALbum-makeWall').is(':checked') }, function(data) {
            $('#previewAlbumArea').prepend(data);
            $('#newAlbumDialog').remove();
            dialog.dialog('destroy');
        }, 'html');
    }
}

function makeNewAlbum() {
    tempListFriend = new Array();
    $.post('/Gallery/getDialogNewAlbum', {}, function(data) {
        $('#dialogNewAlbum-extern').html(data);
        $('#newAlbumDialog').dialog({
            bgiframe: true,
            autoOpen: true,
            modal: true,
            autoOpen: true,
            width: 400,
            buttons: {
                'Crea un Album': function() {
                    createAlbum($(this));
                },
                'Annulla': function() {
                    $('#newAlbumDialog').remove();
                    $(this).dialog('destroy');
                }
            }
        });
    }, 'html');
    $('#selecetdUserToAdmin').html("");
}

function changePhoto(data, idAlbum, elOld, elTarget, direction) {
    if (api != null) {
        api.destroy();
    }
    elOld.html('<img style="margin:80px 0px 80px 240px" src="../../Content/ajax-loader.gif" />');
    $.post('/Gallery/getPhoto', { idPhoto: data, idAlbum: idAlbum }, function(data) {
        if (direction == "right") {
            elOld.parent().html(data);
            /*elOld.parent().hide('slide', { direction: 'left' }, 700, function() {

            });
            setTimeout(function() { elTarget.parent().html(data).show('slide', { direction: 'right' }, 1000); elOld.parent().remove(); }, 400);*/
        }
        if (direction == "left") {
            elTarget.parent().html(data)
            /*elOld.hide('slide', { direction: 'right' }, 700, function() {
            //elOld.parent().remove();
            });
            setTimeout(function() { elTarget.parent().html(data); elTarget.show('slide', { direction: 'left' }, 1000); elOld.parent().remove();}, 1250);*/
        }
    }, 'html');
}
    
    
