jQuery(function($){
    /** Filter panel logic */
    /// Run once
    $('div.filters-toggle').click( function() {
        if ( $.browser.msie == false )
            $('div.filters').slideToggle('fast');
        else
            $('div.filters').toggle('fast');
    });

    // Clicking the reset link sets the form back to default state
    $('div.filters a.reset').click( function() {
        $(this).parent('form')[0].reset();
    }).siblings('input').attr('disabled',false);

    // in any multiple select, the 'Any' option is mutually exclusive to all other options
    $('div.filters option').click( function() {
        if ('' == $(this).val()) { $(this).siblings().removeAttr('selected'); }
        else { $(this).siblings('option:first').removeAttr('selected'); }
    });
    
    //  filter form handler
    $('div.filters form').submit( function( event ) {
        event.preventDefault();
        
        var data = $(this).serializeArray();
        var action = $(this).attr('action');
        var filters = [];
        $(data).each(function(i,item){
            
            if ( item.value == '' ) return;
            
            if ( item.name.indexOf('[]') != -1 ) {
                item.name = item.name.substring(0,item.name.lastIndexOf('[]'))
            }
            
            if ( filters[item.name] )
                filters[item.name] += ','+item.value;
            else
                filters[item.name] = item.value;
        });
        
        for ( var i in filters ) {
            action += '/'+i+':'+filters[i];
        }
        
        location.href = action;
        
        return false;
    });
});

