$(function() {
    $('#search-facets').delegate('li.facet', 'click', function(e) {
        var $this = $(this);
        if ($this.hasClass('active')) {
            var $tgt = $(e.target);
            if ($tgt.is('a')) {
                $tgt.closest('.facet').find('.selected').removeClass('selected');
                $tgt.closest('li').addClass('selected');
                return;
            }
            $this.removeClass('active');
        } else {
            $this.closest('ul').find('.active').removeClass('active');
            $this.addClass('active');
        }
    });

    if ($('body').hasClass('pjax') && $.support.pjax) {
        initSearchPjax('#pjax-results');
    }
});


function initSearchPjax(container) {
    var $container = $(container);

    function pjaxOpen(url) {
        var urlBase = location.pathname + location.search;
        if (!!url && url != '#' && url != urlBase) {
            $.pjax({'url': url, 'container': container});
        }
    }

    function hijackLink() {
        pjaxOpen($(this).attr('href'));
    }

    function loading() {
        var $this = $(this),
            $wrapper = $this.closest('.results'),
            msg = gettext('Updating results&hellip;'),
            cls = 'updating';
        $wrapper.addClass('loading');

        // The loading indicator is absolutely positioned atop the
        // search results, so we do this to ensure a max-margin of sorts.
        if ($this.outerHeight() > 300) {
            cls += ' tall';
        }

        // Insert the loading indicator.
        $('<div>', {'class': cls, 'html': msg}).insertBefore($this);
    }

    function finished() {
        var $this = $(this),
            $wrapper = $this.closest('.results');

        // Initialize install buttons and compatibility checking.
        $.when($this.find('.install:not(.triggered)').installButton()).done(function() {
            $this.find('.install').addClass('triggered');
            initListingCompat();
        });

        // Remove the loading indicator.
        $wrapper.removeClass('loading').find('.updating').remove();

        // Scroll up.
        $('html').animate({scrollTop: 0}, 200);
    }

    function turnPages(e) {
        if (fieldFocused(e)) {
            return;
        }
        if (e.which == $.ui.keyCode.LEFT || e.which == $.ui.keyCode.RIGHT) {
            e.preventDefault();
            var sel;
            if (e.which == $.ui.keyCode.LEFT) {
                sel = '.paginator .prev:not(.disabled)';
            } else {
                sel = '.paginator .next:not(.disabled)';
            }
            pjaxOpen($container.find(sel).attr('href'));
        }
    }

    $('.pjax-trigger a').live('click', _pd(hijackLink));
    $container.bind('start.pjax', loading).bind('end.pjax', finished);
    $(document).keyup(_.throttle(turnPages, 300));
}

@@ -923,42 +923,469 @@
 ax(%7B
-'
+%0A                
 url
-'
 : url,
- 'container': container
+%0A                container: container,%0A                beforeSend: function(xhr) %7B%0A                    this.trigger('end.pjax', %5Bxhr, url, container%5D);%0A                    xhr.setRequestHeader('X-PJAX', 'true');%0A                    loading();%0A                %7D,%0A                complete: function(xhr) %7B%0A                    this.trigger('end.pjax', %5Bxhr, url, container%5D);%0A                    finished();%0A                %7D%0A            
 %7D);%0A
@@ -1517,37 +1517,8 @@
 var 
-$this = $(this),%0A            
 $wra
@@ -1517,36 +1517,41 @@
 var $wrapper = $
-this
+container
 .closest('.resul
@@ -1703,24 +1703,23 @@
 loading 
-indicato
+throbbe
 r is abs
@@ -1833,20 +1833,25 @@
    if ($
-this
+container
 .outerHe
@@ -1931,32 +1931,31 @@
 the loading 
-indicato
+throbbe
 r.%0A        $
@@ -2007,20 +2007,25 @@
 Before($
-this
+container
 );%0A    %7D
@@ -2068,37 +2068,8 @@
 var 
-$this = $(this),%0A            
 $wra
@@ -2076,20 +2076,25 @@
 pper = $
-this
+container
 .closest
@@ -2190,20 +2190,25 @@
 $.when($
-this
+container
 .find('.
@@ -2281,20 +2281,25 @@
        $
-this
+container
 .find('.
@@ -3115,79 +3115,8 @@
 ));%0A
-    $container.bind('start.pjax', loading).bind('end.pjax', finished);%0A
     

fix ajax results loading bug