function setItemAction() {
    $('.btn-edit').click(function () {
        $.post('{actions_link}', {ag_action: 'get_photo', id_photo: $(this).attr('href').replace('#', '')}, function (data) {
            if (data) {
                var m_dialog = $('#alpha_g_dialog');
                m_dialog.find('.modal-body').html(data);
                m_dialog.modal('show');
            }
        });
        return false;
    });
    $('.btn-del').click(function () {
        var el = $(this);
        if (confirm(ln_del + el.parent().find('.fancybox').html().replace(/<i.*>/, '') + ' ?')) {
            var data = {
                id: el.attr('href').replace('#', ''),
                hash: $('input[name=hash]').val(),
                photogallery: $('input[name=photogallery]').val(),
                ag_action: 'del_photo'
            };
            $.post('{actions_link}', data, function (data) {
                var obj = jQuery.parseJSON(data);
                if (obj.success == 1) {
                    refreshPhotos();
                }
            });
        }
        return false;
    });
    $("a.fancybox").fancybox({
        'speedIn': 600,
        'speedOut': 200,
        'overlayShow': true
    });
    // Sortable
    $('div.photolist').sortable({
        update: function () {
            var data = 'ag_action=sort&hash=' + $('input[name=hash]').val() + '&' + $(this).sortable('serialize');
            $.post('{actions_link}', data);
        }
    });
    $('div.photolist').disableSelection();
}

function refreshPhotos() {
    $.post('{actions_link}', {ag_action: 'get_photos'}, function (data) {
        $('.photolist').html(data);
        setItemAction();
    });
}

$(function () {
    setItemAction();
    $('#alpha_g_dialog .btn-primary').click(function () {
        var data = {'ag_action': 'edit_photo'};
        $('#editForm input, #editForm textarea').each(function () {
            data[$(this).attr('name')] = $(this).val();
        });
        $.post('{actions_link}', data, function (data) {
            $('#alpha_g_dialog').modal('hide');
        });
        return false;
    });

    refreshPhotos();
});

