$(function() {
    $('#add_to_gallery').click(function() {
        window.location = '/gallery/add';
    });
    $('#login').click(function() {
        window.location = '/user/login';
    });
    $('input#show_create').click(function() {
        window.location = '/gallery/show_create';
    });
    $('#my_galleries').click(function() {
        window.location = '/gallery/my';
    });
    $('#edit_gall').click(function() {
        var id = $(this).attr('rel');
        window.location = '/gallery/edit/' + id;
    });
    $('#edit_show').click(function() {
        var id = $(this).attr('rel');
        window.location = '/show/edit/' + id;
    });
    function check_exp() {
        var txt = $('#show_items').html().trim();
        if (txt == '') {
            $('#galleries').hide();
        }
    }

    $('input.check_elem').change(function() {
        var add = ($(this).attr("checked") == undefined) ? false : true;
        var id = $(this).attr('id').split('_')[1];
        var label_id = 'add_' + id;
        if (add) {
            $.ajax({
                url: '/gallery/add_to_show/' + id,
                dataType: 'json',
                error: function() {
                },
                success: function(data) {
                    if (data.error == false) {
                        $('#show_items').append(data.html)
                        $('#galleries').fadeIn(750);
                        $("label[for='" + label_id + "']").html('Удалить из выставки');
                    }
                }
            })
        } else {
            if (confirm('Вы действительно хотите убрать экспонат из выставки?')) {
                $.ajax({
                    url: '/gallery/remove_from_show/' + id,
                    dataType: 'json',
                    success: function(data) {
                        $('#exp_' + id).remove();
                        $('input#add_' + id).removeAttr('checked');
                        $("label[for='" + label_id + "']").html('Добавить к выставке');
                        check_exp();
                    }
                })
            } else {
                $(this).attr("checked", "checked");
            }
        }
    });

    $('#remove_from_show').live('click', function() {
        var id = $(this).attr('rel');
        var label_id = 'add_' + id;
        if (confirm('Вы действительно хотите удалить элемент?')) {
            $.ajax({
                url: '/gallery/remove_from_show/' + id,
                dataType: 'json',
                success: function(data) {
                    $('#exp_' + id).remove();
                    $('input#add_' + id).removeAttr('checked');
                    $("label[for='" + label_id + "']").html('Добавить к выставке');
                    check_exp();
                }
            })
        }
    });

    $('a#news_delete').click(function() {
        return confirm('Вы действительно хотите удалить новость?');
    });

    $('input#create').click(function() {
        window.location = '/gallery/create';
    });
    $('input#add').click(function() {
        $.ajax({
            url: '/gallery/my_shows',
            dataType: 'html',
            success: function(data) {
                $('div#shows').html(data);
            }
        })
    });
    $('#select_show').live('change', function() {
        var val = $(this).val();
        if (val > 0) {
            window.location = '/show/edit/' + val;
        } else {
            alert('Выберите выставку');
        }
    })

})
