$(document).ready(function(){
    $('#gallery').parent().css({
        'cursor': 'pointer',
        'padding': '5px;',
        'display': 'inline',
        'float': 'right',
        'margin-top': '-3px'
    });
    $('#gallery').click(function(){
        if ($('input[name=isgallery]').attr('value') == '0') {
            $('input[name=isgallery]').attr('value', '1')
        }
        else {
            $('input[name=isgallery]').attr('value', '0')
        }
        $('#filterform').attr('action',$('#filterform').attr('act'));
        $('#filterform').submit();
    }
    );
    function cb_click(obj){
        if ($(obj).prev().is('[checked="checked"]')) {
            $(obj).attr('checked', 'checked');
            cb_sel[$(obj).attr('id')] =  [$(obj).attr('a_'), $(obj).attr('t_'), $(obj).attr('y_')];
        }
        else {
            $(obj).attr('checked', '');
            delete cb_sel[$(obj).attr('id')];
        }
        $.cookie('cb_sel', JSON.stringify(cb_sel), {path: "/"});
        if (JSON.stringify(cb_sel) == '{}') {
            $('#clear_selected').add('#show_selected').css('display','none');
        }
        else {
            $('#clear_selected').add('#show_selected').css('display','inline');
        }
    }
    $('a.ac').click(function(){
        $(this).attr('checked', $(this).is('[checked="checked"]') ? '' : 'checked' );
        cb = $('#c'+$(this).attr('id'));
        cb_click(cb);
    });
    $('input:checkbox.ac').click(function(){
        $(this).prev().attr('checked', $(this).prev().is('[checked="checked"]') ? '' : 'checked' );
        cb_click(this);
    });
    $('#clear_selected').click(function(){
        for (i in cb_sel) {
            $('#' + i).prev().attr('checked', '');
            cb_click($('#' + i));
        }
        $.cookie('cb_sel', JSON.stringify(cb_sel), {path: "/"});
        $('#clear_selected').add('#show_selected').css('display','none');
    });
    $('.list').css({
        'display': 'inline-block',
        'text-align': 'center',
        'padding': '10px',
        'margin': '3px',
        'background-color': 'gray',
        'cursor': 'pointer',
        'width': '14px'
    });
    $('.list-selected').css({
        'background-color': 'black'
    });
    //обработка чекбоксов для вывода
    cb_sel = {};
    if ( ! $.cookie('cb_sel')) {
        $.cookie('cb_sel', JSON.stringify(cb_sel), {path: "/"});
    }
    else {
        cb_sel = JSON.parse($.cookie('cb_sel'));
        for(var i in cb_sel) {
            $('#' + i).prev().attr('checked', 'checked');
            cb_click($('#' + i));
        }
    }
});
