﻿function shareVideo(idContent, username) {
    $('.videoEmbed').hide();
    $('#dialogShowGroupSelectDestination').dialog({
        bgiframe: true,
        autoOpen: true,
        modal: true,
        resizable: false,
        width: 450,
        height: 0,
        title: 'Condividi il mesaggio',
        buttons: {
            'Annulla': function() {
                $(this).dialog('close');
            },
            'Mie discussioni': function() {
                shareVideoMy(idContent, username);
                $(this).dialog('destroy');
            },
            'Discussioni degli Amici': function() {
                selectFriendForShare(idContent);
                $(this).dialog('destroy');
            }
        },
        close: function() {
            $('.videoEmbed').show();
            $(this).dialog('destroy');
        }
    });

}

function selectFriendForShare(idContent) {
    $.post('/Friend/getAllFriends', { username: $.cookie('usernameActive') }, function(data) {
        tempListFriend = new Array();
        $('#dialogShowFriendArea').html(data);
        $('#friendDialog-SelectAll-checkBox').parent().remove();
        $('#dialogShowFriend').dialog({
            bgiframe: true,
            autoOpen: true,
            height: 500,
            width: 550,
            modal: true,
            resizable: false,
            buttons: {
                'Conferma': function() {
                    var stringArray = new Array();
                    var usernameFriend = "";
                    $.each(tempListFriend, function(i, v) {
                        usernameFriend = v.username;
                    });
                    shareVideoMy(idContent, usernameFriend);
                    $(this).dialog('close');
                },
                'Annulla': function() { $(this).dialog('close'); }
            },
            close: function() {
                selectedFriend = null;
                $(this).dialog('destroy');
            }
        });
    }, 'html');
}

function shareVideoMy(idContent, username) {
    $.post('/Wall/getGroupContentDialog', { idGroup: -1, username: username }, function(data) {
        $('.videoEmbed').hide();
        $('#dialogShowGroup' + idContent).html(data);
        $('#dialogShowGroup' + idContent).dialog({
            position: ['0', '0'],
            bgiframe: true,
            autoOpen: true,
            modal: true,
            width: 850,
            resizable: false,
            title: 'Seleziona la discussione di destinazione',
            buttons: {
                conferma: function() {
                    $.post('/Wall/copyContent', { idContent: idContent, idWall: idWallSelected }, function(data) { }, 'json');
                    $('#dialogShowGroup' + idContent).append("<div class='resultShare' title='Condivisione messaggio' style='display:none'>Messaggio copiato con successo.</div>")
                    var dialogToDestroy = $(this);
                    $('#dialogShowGroup' + idContent).find('.resultShare').dialog({
                        bgiframe: true,
                        autoOpen: true,
                        modal: true,
                        width: 450,
                        resizable: false,
                        buttons: {
                            'Vai alla discussione': function() {
                                openTabWallIndex(idWallSelected, nameWallSelected);
                                $(this).dialog('destroy');
                                dialogToDestroy.dialog('destroy');
                                $('#dialogShowGroup' + idContent + ' div').remove();
                            },
                            'Condividi Ancora': function() {
                                $(this).dialog('destroy');
                            },
                            'Esci': function() {
                                $(this).dialog('destroy');
                                $('.videoEmbed').show();
                                dialogToDestroy.dialog('destroy');
                                $('#dialogShowGroup' + idContent + ' div').remove();
                            }
                        },
                        close: function() {
                            $(this).dialog('destroy');
                        }
                    })

                },
                annulla: function() {
                    $('.videoEmbed').show();
                    $(this).dialog('destroy');
                    $('#dialogShowGroup' + idContent + ' div').remove();
                }
            },
            close: function() {
                $('.videoEmbed').show();
                $(this).dialog('destroy');
                $('#dialogShowGroup' + idContent + ' div').remove();
            }
        });
    }, 'html');
}
