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
    });
}
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;
    });

    $('#file_upload').uploadify({
        'formData': {'{session_name}': '{session_data}'},
        'buttonText': ln_btn_upload,
        'fileTypeDesc': item_fileTypeDesc,
        'fileTypeExts': item_fileTypeExts,
        'swf': '/assets/plugins/uploadify3.2/uploadify.swf',
        'uploader': '{actions_link}',
        'onUploadStart': function(file) {
            var data = {'ag_action': 'add_photo'};
            $('#addForm input, #addForm textarea').each(function() {
                data[$(this).attr('name')] = $(this).val();
            });
            $("#file_upload").uploadify("settings", 'formData', data);
        },
        'onUploadComplete': function(file) {
            refreshPhotos();
        },
        'onUploadSuccess': function(file, data) {
            data = jQuery.parseJSON(data);
            if (data['success'] !== undefined && data['success'] === 'true') {
                $('#file_upload').uploadify('settings', 'scriptData', {'{session_name}': '"' + data['session_data'] + '"'});
            }
        }
    });

    refreshPhotos();
});