function metricReady(url)
{
  ready = false
  $.ajax({
    async: false,
    type: "GET",
        url: url,
        success: function(msg, textStatus){
        ready = true;
      },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
        ready = false;
      }
    });
  return ready;
}

function checkForResult(url)
{
  if(metricReady(url)) {
    // Page responded immediately, we're all set
  }
  else {
    // Page didn't respond, we'll poll
    $("div.waiting").show("fast");
    interval = 3000; // in milliseconds
    metricReadyInterval = window.setInterval(
                       function() {
                         if(metricReady(url)){
                           window.location.reload();
                         }
                         else{
                           // still isn't ready, so keep iterating
                         }
                       },
                       interval);
      //cancel the interval after 30 minutes of polling
    window.setInterval(
                       function() {
                           $("div.waiting").hide("fast");
                           clearInterval(metricReadyInterval);
                       },
        (1000 * 60 * 30));
      
  }
}

  function fileReady(url, temp_id, line_number)
  {
      ready = false;
      $.ajax({
          type: "GET",
          url: url,
          timeout: 20000,
          success: function(msg, textStatus){
              var loading = $("#file-code-loading-"+temp_id); 
              var content = $("#file-code-"+temp_id); 
              content.html(msg);
              content.show();
              loading.hide();
              if(line_number!="") {
                highlightLine(content, line_number);
              }
              ready = true;
          },
          error: function (XMLHttpRequest, textStatus, errorThrown) {
              //not there try again in 6 seconds, this can overload the dynos slowed from 3 to 6 seconds
              setTimeout(function() {  fileReady(url, temp_id, line_number); }, 6000);
              ready = false;
          }
      });
      return ready;
  }
  
  function checkForFile(url, temp_id, line_number)
  {
      //let the whole page load first don't check for the file for 1.5 second after page load
      setTimeout(function() {  fileReady(url, temp_id, line_number); }, 1500);      
  }

  function highlightLine(content, line_number)
  {
      var line_start = content.find(".line-numbers:contains('"+line_number+"')");
      line_start.addClass('highlighted-line');
  }

  function checkForFile(url, temp_id, line_number)
  {
      //let the whole page load first don't check for the file for 1.5 second after page load
      setTimeout(function() {  fileReady(url, temp_id, line_number); }, 1500);      
  }

  function load_revision_diff_count(url)
  {
      $.ajax({
          type: "GET",
          url: url,
          timeout: 25000,
          success: function(msg, textStatus){
              var loading = $("#revision-diff-count-waiting"); 
              var content = $("#revision-diff-count-load"); 
              content.html(msg);
              content.show();
              loading.hide();
          },
          error: function (XMLHttpRequest, textStatus, errorThrown) {
              var loading = $("#revision-diff-count-waiting"); 
              var content = $("#revision-diff-count-load"); 
              content.html("?");
              content.show();
              loading.hide();
          }
      });
  }

  function load_hotspots(url)
  {
      $.ajax({
          type: "GET",
          url: url,
          timeout: 25000,
          success: function(msg, textStatus){
              var loading = $("#hotspots-loader-wait"); 
              var content = $("#hotspots-loader"); 
              content.html(msg);
              content.show();
              loading.hide();
          },
          error: function (XMLHttpRequest, textStatus, errorThrown) {
              var loading = $("#hotspots-loader-wait"); 
              var content = $("#hotspots-loader"); 
              content.html("Loading Hot Spots timed out.");
              content.show();
              loading.hide();
          }
      });
  }
  
  $(document).ready(function() {
      // Add other AJAX goodness in here
  });


