// source --> https://rotlichtadresse.de/wp-content/plugins/ultimate-member-new-custom/assets/js/script.js?ver=5.0.4 
jQuery(document).ready(function ($) {
    $('.eael-gallery-grid-item').click(function (e) {
        e.stopPropagation();
        window.open($(this).find('.gallery-item-buttons a').attr('href'), '_blank');
    });
    $('.eael-gallery-grid-item .gallery-item-buttons a').click(function (e) {
        e.stopPropagation();
    });


    $(".delete-user").click(function (e) {
        e.preventDefault();
        if (confirm('Möchtest Du dieses Profil wirklich löschen? Dies ist nicht mehr rückgängig zu machen.')) {
            window.location.href = $(this).attr("href");
        }
    });

    $(".push-profile").click(function (e) {
        e.preventDefault();
        var button = $(this);
        button.attr("disabled", "disabled");
        button.fadeTo(500, 0.3);
        $.post(
            customVar.ajaxUrl, {
                action: "umnw_push_agency_profile",
                user: button.data("id"),
            },
            function (response) {
                alert(response);
                button.fadeTo(500, 1);
            }
        );
    });


    $(".models-profile-block .tabs a:first-of-type, .models-profile-block .content > div:first-of-type").addClass("active");
    $(".models-profile-block .tabs a").click(function (e) {
        e.preventDefault();

        if ($(this).hasClass("active"))
            return;

        $(".models-profile-block .tabs a.active").removeClass("active");
        $(this).addClass("active");

        $(".models-profile-block .content > div.active").removeClass("active");
        $(".models-profile-block .content > div.tab-" + $(this).attr("data-id")).addClass("active");

    });


    $('#push_own_profile').click(function (e) {
        e.preventDefault();
        var button = $(this);
        button.attr('disabled', 'disabled');
        button.fadeTo(500, 0.3);
        $.post(
            customVar.ajaxUrl, {
                action: 'umnw_push_own_profile',
                user: button.data('id'),
            },
            function (response) {
                if (response != '1') {
                    alert('Ein Fehler ist aufgetreten. Bitte versuche es erneut.');
                    button.removeAttr('disabled');
                    button.fadeTo(500, 1);
                } else {
                    alert('Dein Profil ist nun auf der ersten Position!');
                    button.remove();
                }
            }
        );
    });

    $('.agent-profile .agent-hide').remove();

    $('#custom-tipsy').tipsy({
        gravity: 'w',
        opacity: 1,
        offset: 3,
        title: function () {
            return $('#custom-tipsy .inner-text').html();
        }
    });


    
    $(".um-profile.um-editing .um-form form").attr("enctype","multipart/form-data");
    
    $(".um-profile.um-editing .um-form form .filetouploadvideo").change(function(event){
            var file;


        if ((file = this.files[0])) {
            console.log(this.files[0])
            if(this.files[0].type != "video/mp4"){
                alert("Es ist keine gültige Datei.");
            }
            var size = ((this.files[0].size)/1024)/1024;
            if(size > 100){
                alert("Datei ist zu groß. Bitte lade ein Video hoch, das kleiner oder gleich 100 MB ist.");
            }
        }
    });
    
    $(".gallery-upload-photo .delete_file").click(function(event){

        if (confirm("Bist Du Dir sicher? Dein Foto wird vom Server gelöscht. Dies ist nicht mehr rückgängig zu machen.")) {
            var button = $(this);

            button.parent().addClass('loading');
            
            $.post(
                customVar.ajaxUrl, {
                    action: "umnw_delete_picture",
                    user: button.data("user"),
                    number: button.data("id"),
                },
                function (response) {
                    if (response == '0') {
                        alert('Some error occured. Try again later');
                    } else {
                        button.parent().removeClass('foto-uploaded loading');
                        button.parent().children('div.img').css('background-image', '');
                        button.parent().children('.filetouploadimage').val('');
                    }
                }
            );
        }

    });
    
    $(".delete_video_file").click(function(event){

        $(this).parent().remove();

    });

    
    
    $(".gallery-upload-photo .filetouploadimage").change(function(event){

        var button = $(this);
        if (button.val() != '' && button[0].files.length > 0) {
            var file = button[0].files[0];

            button.parent().addClass('loading');

            var fd = new FormData();
            fd.append('fileToUpload' + button.data('id'),file);
            fd.append('action','umnw_upload_picture');
            fd.append('user',button.data('user'));
            fd.append('id',button.data('id'));

            $.ajax({
                url: customVar.ajaxUrl,
                type: 'post',
                data: fd,
                contentType: false,
                processData: false,
                success: function(response){
                    button.parent().removeClass('loading');
                    button.val('');
                    if (response == '0') {
                        alert('Some error occured. Try again later');
                    } else if(response.search('http') < 0){
                        alert(response);
                    } else {
                        button.parent().addClass('foto-uploaded');
                        button.parent().children('div.img').css('background-image', 'url('+ response +')');
                    }
                },
            });
        }

    });

});