/* globals Panels, $, SocReport, socSortCorrelation */

var Correlations = (function() {
    var loaded = null;

    function urlMaker(version, os) {
        var url = SocReport.base ;
        url += '?product=' + SocReport.product;
        url += '&version=' + version;
        url += '&platform=' + os;
        url += '&signature=' + SocReport.signature;
        return function makeUrl(type) {
            return url + '&correlation_report_type=' + type;
        };
    }

    // Load correlation data for various types.
    // @types CPU, Add-On, Module
    function loadCorrelationTabData(version, os, deferred) {
        var makeUrl = urlMaker(version, os);
        var types = ['core-counts', 'interesting-addons', 'interesting-modules'];

        function loadByType(type) {
            $.getJSON(makeUrl(type), function(data) {
                if(data) {
                    $('#' + type + '_correlation').html('<h3>' + data.reason +
                        '</h3><pre>'+ data.load + '</pre>');
                    socSortCorrelation('#' + type + '_correlation');
                } else {
                    $('#' + type + '_correlation').text('No correlation data found.');
                }
            });
        }

        while (true) {
            var type = types.shift();
            if (!type) {
                // all types done
                deferred.resolve();
                break;
            }
            loadByType(type);
        }
    }


    return {
       activate: function() {
           if (loaded) return;
           var deferred = $.Deferred();
           var $panel = $('#correlations');
           var url = $panel.data('partial-url');
           var qs = location.href.split('?')[1];
           url += '?' + qs;
           var req = $.ajax({
               url: url
           });
           req.done(function(response) {
               $('.loading-placeholder', $panel).hide();
               $('.inner', $panel).html(response);
               var $wrapper = $('.correlations-wrapper', $panel);
               var version = $wrapper.data('correlation_version');
               var os = $wrapper.data('correlation_os');

               $('button.load-version-data').click(function () {
                   var type = $(this).attr('name');
                   var makeUrl = urlMaker(version, os);
                   $.getJSON(makeUrl(type), function(data) {
                       $('#' + type + '-panel').html('<h3>' + data.reason + '</h3><pre>' +
                                                     data.load + '</pre>');
                   });
               });
               if (version && os) {
                   loadCorrelationTabData(version, os, deferred);
               }
           });
           req.fail(function(data, textStatus, errorThrown) {
               $('.loading-placeholder', $panel).hide();
               $('.loading-failed', $panel).show();
               deferred.reject(data, textStatus, errorThrown);
           });
           loaded = true;
           return deferred.promise();
       }
    };
})();

Panels.register('correlations', function() {
    return Correlations.activate();
});

@@ -2322,32 +2322,455 @@
 r(version, os);%0A
+                   var spinner = $('%3Cimg /%3E', %7B%0A                      id: 'loading-spinner',%0A                      src: '/static/img/loading.png',%0A                      width: '16',%0A                      height: '17',%0A                      alt: 'loading spinner'%0A                   %7D);%0A                   var contentPanel = $('#' + type + '-panel');%0A%0A                   contentPanel.empty().append(%5B'Loading ', spinner%5D);%0A%0A
                 
@@ -2841,32 +2841,57 @@
     
-$('#' + type + '-p
+if (data) %7B%0A                         contentP
 anel
-')
 .htm
@@ -2983,16 +2983,18 @@
         
+  
 data.loa
@@ -3000,32 +3000,163 @@
 ad + '%3C/pre%3E');%0A
+                       %7D else %7B%0A                          contentPanel.text('No correlation data found.');%0A                      %7D%0A
                 

Fix Bug 943847, handle cases where data is null and add spinner on click