// JavaScript functions for status.enstratus.com

$(document).ready(function(){
  $.getJSON("status.json", function(json){
    printCurrentInfo(json);
    // Loop through JSON to populate history table
    $.each(json.systemStatus.components, function(i,comp){
      // Print out the Availability after the component name (which is hardcoded)
      printAvailability(i,comp);
      // Print out dates (retrieved from first component)
      if (i == 0){
        printDates(i,comp);
      }
      // Print out a row for each component
      printStatusData(i,comp);
    });
  });
});

function printCurrentInfo(json){
  // javascript doesn't play nicely with dates.  
  var da = new Date();
  db = da.toGMTString() 	// Convert to a String in "predictable formt"
  dc = db.split(" ") 	// Split the string on spaces
  if ( eval( dc[3] ) < 1970 ) dc[3] = eval( dc[3] ) +100 	// Correct any date purporting to be before 1970
  db = dc[2] + " " + dc[1] + ", " + dc[3] 	// Ignore day of week and combine date, month and year
  $("h2#today").html(db + "").after(json.systemStatus.message);
}

function printAvailability(i,comp){
  var availInfo = "<span>" + comp.availability + " Availability<sup>1</sup></span>";
  $("th#" + comp.id + "Head").append(availInfo); // component id corresponds to a th id in the html
}

function printDates(i,comp){
  $.each(comp.history, function(j,hist){
    $("tr#datesHead").append("<th>" + hist.month + "/" + hist.date + "</th>")
    // should be 7 when nav is implemented, per George
    if ( j == 13 ) return false;
  });
}

function printStatusData(i,comp){
  // build td for each component, and append to tr whose id matches the component id
  // ELIMINATED MOST DOM INSERTIONS TO SPEED THINGS UP ALOT
  $.each(comp.history, function(j,hist){
    var type  = hist.type;
    // add table cells with images and addt info, where applicable
    var tdHtml = "<td>" +
                  "<span class='info " + type + "'>" +
                    "<img src='images/" + type + ".png' width='20px' height='20px' alt='" + type + "' title='" + type + "'/>";
    // display more information for status other than "normal"
    if (type != "normal"){
      tdHtml += "<span class='moreInfo'><span class='insideWrap'>"; // wrappers for pop-up and style purposes
      // loop through incidents
      $.each(hist.incidents, function(k,incid){   // per incident within a history
        var incidStatus = incid.resolved ? "Resolved" : "Unresolved";
        // Build html for each incident
        tdHtml += "<img src='images/" + incid.type +
                              ".png' width='20px' height='20px' alt='" +
                              incid.type + "' title='" + incid.type + "'/>" + // incident image                                      //display image
                  "<h3>" +
                      "<span class='incidType'>"
                        + incid.type +                                        // incident type
                      "</span>" +

                      " - <span class='note'>"
                        + hist.month + "/" + hist.date + ", " + incid.hour + ":" + incid.minute +                                       // incident type
                      "</span>" +
                  "</h3>" +
                  "<h4>"
                      + incid.impacted + " - " + incid.id +             // components impacted and id (timestamp)
                      "&nbsp; [" + incidStatus + "]<br/>" +             // resolved/unresolved
                  "</h4>" +
                  "<span class='details'";         // open wrapper for updates
        $.each(incid.updates, function(k,upd){     // per update WITHIN an incident
           tdHtml +=  "<p>" +
                        "<strong>" + upd.subject + "</strong> - " +   // update subject
                        "<i>" + upd.timestamp + "</i><br/>" +         // update timestamp
                          upd.message +                               // update message
                      "</p>";
        });
        tdHtml += "</span>";       // close wrapper for updates
      });
      tdHtml += "</span></span>";  // end wrappers for pop-up and style purposes
    }
    tdHtml += "</span></td>";
    $("tr#" + comp.id + "Data").append(tdHtml);
    // display info box when hover over icon
    $("table#datesAndStatusTable span.info:not(.normal)").hover(
      function () { $(this).contents("span.moreInfo").css("display","block"); },
      function () { $(this).contents("span.moreInfo").css("display","none"); }
    );
    // remove dotted border from last paragraph
    $("span.details:last").css("border","none");
    // should be 7 when nav is implemented, per George
    if ( j == 13 ) return false;
  });
}



















