jQuery(function(){
    
    $('#subnav input:checkbox').checkbox({
        empty: '/imports/css/checkbox/empty.png'
    });
    $('#subnav .disabled input:checkbox').attr("disabled", true); 

    // Bind click for filter
    $( '#subnav input:checkbox' ).bind( 'click', function() {
        var filterName = $( '#subnav form' ).attr( 'name' );

        // Get right subdir
        var subdir = '';
        switch( filterName ) {
            case 'filter':
                subdir = 'bains';
                break;
            case 'japaneseBathFilter':
                subdir = 'bains_japonais';
                break;
            default:
                subdir = 'bains';
        }

        // On filter changes bring page back to 1
        $( '#currentPage' ).attr( 'value', 1 );

        if( $( this ).attr( 'class' ) == 'tous' ) {
            // Uncheck all other.
            $( this.parentNode ).siblings( 'li' ).children( 'input' ).attr( 'checked', false );
        } else {
            // Uncheck the first one.
            $( this.parentNode ).siblings( 'li' ).children( 'input.tous' ).attr( 'checked', false );
        }

        // Get serialized values. Hack to get the good value of the item we just selected.
        var str = '';

        // Start hack
        $( this ).attr( 'checked', !$( this ).attr( 'checked' ) );
        // End hack

        // Verify if this input is the last one of the group, if yes we reselect the "tous" one.
        if( $( this.parentNode.parentNode ).children( 'li' ).children( 'input:checked' ).length === 0 ) {
            $( this.parentNode.parentNode ).children( 'li' ).children( 'input.tous' ).attr( 'checked', true );
        }

        var isFirst = true;


        $.map( $( '[name=' + filterName + "]" ).serializeArray(), function( arg ) {
            if( isFirst === false ) {
                str += ',';
            }

            str += $( arg ).attr( 'name' ) + ':' + $( arg ).attr( 'value' );
            isFirst = false;
        } );

        // Start hack
        $( this ).attr( 'checked', !$( this ).attr( 'checked' ) );
        // End hack

        // Fetch  the page with the new list.
        $.get( 'liste.php', { 'filter' : str }, function( arg ) {
            $( '#list-wrapper' ).html( arg );
        } );
    });

    // Bind click event on pager for bath page.
    $( '#toppager .pager a' ).live( 'click', function() {
        var filterName = $( '#subnav form' ).attr( 'name' );

        // Get right subdir
        var subdir = '';
        switch( filterName ) {
            case 'filter':
                subdir = 'bains';
                break;
            case 'japaneseBathFilter':
                subdir = 'bains_japonais';
                break;
            default:
                subdir = 'bains';
        }


        // Fetch  the page with the new list.
        var page = $( this ).html();

        // Manage + and - page element.
        if( page === '+' ) {
            page = parseInt( $( '#currentPage' ).attr( 'value' ), 10 ) + 1;
        }
        if( page === '-' ) {
            page = parseInt( $( '#currentPage' ).attr( 'value' ), 10 ) - 1;
        }
        $( '#currentPage' ).attr( 'value', page );

        $.get( 'liste.php', { 'page': page , filterName : '' }, function( arg ) {
            $( '#list-wrapper' ).html( arg );
        } );

        return false;
    } );

    $( '#displayAll' ).live( 'click', function() {
        $( '#subnav input:checkbox' ).attr( 'checked', false );
        $( '#subnav .tous' ).attr( 'checked', true );
        $( '#subnav input:first' ).click();
    } );
});
