jQuery(function($) {
    var filterParameters = ''

    // Change event on the selectors
    function bindProjectFilterEvents() {
        $('.jNiceWrapper').each(function(){
            var selectedValue = $(this).find('.jNiceSelectText').text();
            var defaultValue = $(this).find('.jNice-list ul li a[index="0"]').text();
            if(selectedValue != defaultValue) {
                // Crop string if it exceeds the limit of the jNice box
                var selectedContent = $(this).find('.jNiceSelectText').text();
                if(selectedContent.length > 26) {
                    selectedContent = selectedContent.substring(0,26)+'...';
                    $(this).find('.jNiceSelectText').text(selectedContent);
                }
                $(this).find('.jNiceSelectText').addClass('jNiceSelected');
            }
        });

        Cufon.replace('.project .txt h3', {hover: 'true'});
        Cufon.now();

        $('form#filter .loader').css('display', 'none');


        $('form#filter #market, form#filter #continent, form#filter #period, form#filter #activity, form#filter #country, form#filter #projects').change(function(){
            applyFilter();
        });

        $('#clearFilter').click(function() {
            $('#tx_searchprojects_pi1_clearFilter').attr('value', '1');
            applyFilter();
            return false;
        });

        $('#perpageform').jNice();
        $('#perpageform .jNiceWrapper').css('z-index','0');

        $('#perpageform select').change(function(){
            applyFilter();
        });

        $('#project-list div.pager a').click(function(){
            var urlParts = $(this).attr('href').split('/');
            var url = urlParts[urlParts.length - 1];
            var page = parseInt(url.substring(1)) - 1;
            $('input#pointer').attr('value',page);
            applyFilter();
            return false;
        });
    }
    
    // Reload the filter
    function applyFilter() {
        filterParameters = $('form#filter').serialize()+'&'+$('form#perpageform').serialize();
        $('#loading').fadeIn(300);
        $('form#filter .loader').fadeIn(300);
        $('#project-list').empty();

        $.ajax({
            url: 'index.php',
            data: filterParameters+'&eID=tx_searchprojects_applyFilter',
            success: function(data) {
                $('form#filter .loader').fadeOut(300);
                $('div#filters').parent().html(data);
                $('#filter').jNice();
                reloadResults();
            }
        });
    }

    // Reload the results
    function reloadResults() {
        $.ajax({
            url: 'index.php',
            data: filterParameters+'&eID=tx_searchresults_reloadResults',
            success: function(data) {
                $('#loading').fadeOut(300);
                $('div#project-list').parent().html(data);
                bindProjectFilterEvents();
                $('.project', $('div#project-list')).hide().showdelay();
            }
        });
    }

    // Bind the events
    bindProjectFilterEvents();
});
