$(document).ready( function () {
    var filter_max_price = remember_max_filter_price;
    if ( $('.cat-filter-title').html() ) {
        filter = document.getElementById('cat-filter');
        filter.style.marginTop = '10px';
        filter.style.marginBottom = '20px';
        filter.style.marginLeft = '10px';
        
        form   = document.createElement('form');
        form.setAttribute('onsubmit','Javascript: return check_filters()');
        
        last_group = "vnarg";
        
        for (var i in search_tags) {
            if (search_group[i].indexOf('hidden')==-1) {
                if (search_group[i] != last_group) {
                    if (typeof(subheading)!='undefined') form.appendChild(subheading);
                    last_group = search_group[i];
                    subheading = document.createElement('div');
                    subheading.innerHTML = '<h3>'+search_group[i]+'</h3>';
                }

                child = document.createElement('h5');
                
                click = document.createElement('input');
                click.setAttribute('type','checkbox');
                click.setAttribute('id',search_class[i]);
                click.onclick=function(){
                    $('#heading_'+$(this).attr('id')).attr('checked',$(this).attr('checked'));
                    check_filters();
                }

                if (show_tag_quantities) {
                    var count = 0;
                    $('.tag_sweep').each( function () {
                        if ($(this).attr('tag_'+search_class[i])) count++;
                    });

                    description = document.createElement('span');
                    description.innerHTML = search_tags[i].replace('percent','%')+'<span>('+count+')</span>';
                }
                else {
                    description = document.createElement('span');
                    description.innerHTML = search_tags[i].replace('percent','%');                
                }

                child.appendChild(click);
                child.appendChild(description);

                subheading.appendChild(child);
            }
        }
        if (typeof(subheading)!='undefined') form.appendChild(subheading);
        filter.appendChild(form);

        if (show_price_filter) {
            priceform = document.createElement('form');
            priceform.setAttribute('onsubmit','Javascript: return check_price_filter()');
            priceform.style.marginTop = '20px';

            priceinfo = document.createElement('h5');
            priceinfo.innerHTML = "Price Under &pound;";
            priceform.appendChild(priceinfo);
        
            priceselect = document.createElement('select');
            priceselect.style.float = 'right';
            priceselect.style.width = '80px';
            priceselect.setAttribute('id','filter_price');
            priceinfo.appendChild(priceselect);
        
            for (var p in price_filter) {
                child = document.createElement('option');
                child.setAttribute('name',p);
                child.innerHTML = price_filter[p];
                priceselect.appendChild(child);
            }

            child = document.createElement('option');
            child.setAttribute('name',0);
            child.setAttribute('selected',1);
            child.innerHTML = 'Any';
            priceselect.appendChild(child);

            filter.appendChild(priceform);
            $('#filter_price').change( function () {check_price_filter(); });
        }
        
        document.body.style.cursor = 'auto';
    }
    
    check_price_filter = function () {
        filter_max_price = $('#filter_price').val();
        if (filter_max_price == 'Any') filter_max_price = remember_max_filter_price;
        check_filters();
    }
    
    check_filters = function () {
        var default_filter_shown = true;

        for (var i in search_tags) {
            search_active[ search_class[i] ] = $('#'+search_class[i]).attr('checked');
            if ( search_active[ search_class[i] ] ) default_filter_shown = false;
        }

        $('.tag_sweep').each( function () {
            var shown = default_filter_shown;
            var hide = true;
            for (var i in search_tags) {
                if ( search_active[ search_class[i] ] ){
                    if ( $(this).attr('tag_'+search_class[i]) == 1) shown = true;
                    if ( $(this).attr('tag_'+search_class[i]) != 1) hide = false;
                }
            }

            if (shown && hide) {
                if ( parseFloat($(this).attr('tag_price')) <= parseFloat(filter_max_price) ) {
                    $(this).css('display','block');
                }
                else {
                    $(this).css('display','none');
                }
            }
            else {
                $(this).css('display','none');
            }
            
        });

        if (typeof(filter_update)=='function') filter_update( $(this) );
        $('#filter_show_all').css('display','none');
        return false;
    }
});

