// Предпросмотр картинки при загрузке
$(document).ready(function () {

    var filesCounter = 0;

    //publicity photos update validate checking
    $('#update-publicity-form').on('afterValidateAttribute', function (evt, attribute, messages) {

        if (attribute.name == 'photos[]') {
            if (messages.length !== 0) {
                $('.temp-photos').remove();
                return false;
            } else {
                filesCounter++;
                if (filesCounter > 1) {
                    return false;
                }

                $('#advert-photo').on('change', function () {
                    handleFileSelect(event);
                });
            }
        }
    });

    //avatar update validate checking
    $('#update-user-form').on('afterValidateAttribute', function (evt, attribute, messages) {

        if (attribute.name == 'photoName') {
            if (messages.length !== 0) {
                $('.temp-photos').remove();
                return false;
            } else {
                filesCounter++;
                if (filesCounter > 1) {
                    return false;
                }
                $('#user-photoname').on('change', function () {
                    handleFileSelect(event);
                });
            }
        }
    });
});


function handleFileSelect(event) {
    var files = event.target.
w at top
$(document).ready(function () {

    var $searchOpen = $('#search-open');
    var $searchClose = $('#search-close');
    var $searchMenu = $('#search-menu');

    $searchOpen.on('click', function () {
        $searchMenu.fadeIn("fast", "swing", function () {
            $searchOpen.css("cursor", "no-drop");
            $searchClose.css("cursor", "pointer");
        });

    });
    $searchClose.on('click', function () {
        $searchMenu.fadeOut("fast", "swing", function () {
            $searchClose.css("cursor", "no-drop");
            $searchOpen.css("cursor", "pointer");
        });
    });
});


$(document).ready(function () {
    // clicking on start editind button when you want to edit your avatar thumb
    $('#edit-thumbnail').on('click', function () {
        $('#start_thumb').trigger('click');
    });
    // reloading page after croping picture. Default this plugin uses ajax. We don't need ajax
    $('#crop_thumb').on('click', function () {
        window.location.reload()
    });


});
files;

    $('.temp-photos').fadeOut('fast', function () {
        $(this).remove();
        // $.Jcrop('#thumb').destroy();
        // $('#thumb').empty();
    });

    for (var i = 0, file; file = files[i]; i++) {
        if (file.type === 'image/jpeg' || file.type === 'image/png') {

            var reader = new FileReader();
            reader.readAsDataURL(files[i]);
            reader.onload = function (e) {
                
                $('#temp-photo').after('<img src="' + e.target.result + '" class=\'temp-photos thumbnail\'>');
                $('#thumb').attr('src', e.target.result);
                // $('.jcrop-holder').find('img').attr('src', e.target.result);
            };
        }
    }
}


// Open/closing search windo