﻿// Temporal heat index - Grid control

function GRID() { };

GRID.Init = function() {
    //alert("GRID.Init");

    GRID.HaveLiterals = false;
    GRID.LayerID = "";
};

GRID.DefineLiterals = function(data, status) {
    //alert("GRID.DefineLiterals, status = " + status);

    GRID.CannotUpdateMap = data.CannotUpdateMap;
    GRID.ClickCell = data.ClickCell;
    GRID.Selected = data.Selected;
    GRID.TotalDesc = data.TotalDesc;
    GRID.AllDaysDesc = data.AllDaysDesc;
    GRID.AllTimesDesc = data.AllTimesDesc;
    GRID.MinDesc = data.MinDesc;
    GRID.MaxDesc = data.MaxDesc;
    GRID.AvgDesc = data.AvgDesc;
    GRID.TotDesc = data.TotDesc;
    GRID.StdDevDesc = data.StdDevDesc;
    GRID.StandardDeviationsDesc = data.StandardDeviationsDesc;
    GRID.DOWWarning = data.DOWWarning;
    GRID.Days = data.Days;

    GRID.HaveLiterals = true;
    GRID.CellTotalColor = "fff";  //"#f8f8ff";
    GRID.CellColors = null;
    GRID.EqualColors = new Array("#fff", "#6f6", "#cf6", "#ff6", "#fc6", "#f66");
    GRID.StdDevColors = new Array("#001CC4", "#0059FF", "#6299FF", "#93BCFF", "#C1E0FF", "#E8F3FF", "#FFFF80", "#FFC040", "#FF8000", "#FF4000", "#C00000");

    GRID.SendDataRequest();
};

GRID.Reset = function() {
    //alert("GRID.Reset");

    GRID.SummaryData = null;
    GRID.LayerName = "Items";
    GRID.LayerQuery = "";
    GRID.WhenQuery = "";
    GRID.Style = 1;
    GRID.Minimum = 0;
    GRID.Maximum = 0;
    GRID.Total = 0;
    GRID.Average = 0;
    GRID.StdDev = 0.0;
    GRID.Breaks = new Array();
    GRID.NumBreaks = 10;
    GRID.HaveSubset = false;

    $("#grid-header").hide();
    $("#grid-subset").hide();
    $("#grid-data").html("");
    $("#grid-stats").hide();
};

GRID.GetGridData = function(lyrID) {
    //alert("GRID.GetChartData");
    GRID.LayerID = lyrID;
    GRID.Reset();

    if (GRID.HaveLiterals) {
        GRID.SendDataRequest();
    } else {
        $.ajax(
    	{
    	    type: "GET",
    	    url: "Data.aspx",
    	    data: "page=gridliterals",
    	    dataType: "json",
    	    error: UIHelper.AjaxError,
    	    success: GRID.DefineLiterals
    	});
    }
};
GRID.SendDataRequest = function() {
    var d = new Date();
    $.ajax(
  	    {
  	        type: "GET",
  	        url: "data.aspx",
  	        data: "page=grid&id=" + GRID.LayerID + "&t=" + d.getTime(),
  	        dataType: "json",
  	        error: GRID.AjaxError,
  	        success: GRID.ProcessGridData
  	    });
};
GRID.AjaxError = function(XMLHttpRequest, status) {
    alert("GRID.AjaxError = " + XMLHttpRequest.toString + ", " + status);
};

GRID.ProcessGridData = function(data, status) {
    // Save the data locally
    GRID.Total = data.result;
    GRID.LayerName = data.name;
    GRID.Minimum = data.minimum;
    GRID.Maximum = data.maximum;
    GRID.SummaryData = data.summary;
    GRID.Average = parseFloat(GRID.Total) / 168.0;

    if (UIHelper.IsLinkMap) {
        $("#grid-header").html("");
    } else {
        $("#grid-header").html("<p>" + GRID.ClickCell + "<br/>" + GRID.LayerName + ": " + GRID.TotalDesc + " = " + GRID.Total + "</p>");
    }
    $("#grid-header").show();

    GRID.HaveSubset = false;
    $("#grid-links").hide();

    GRID.BuildGrid();

    // Needed when undoing a drilldown
    GRID.LayerQuery = WHAT.GetLayerQuery(GRID.LayerID);
    //alert("GRID.LayerQuery = " + GRID.LayerQuery);

    // Allow another postback request
    UIHelper.ProcessingRequest = false;
};

GRID.BuildGrid = function() {
    var rx, cx, cols, val = 0, colTot = 0;
    var d, s, hour, style;
    var pre = "0";
    var cell = "cell";
    var hourTotal = "hour-total";
    var cellBottom = "cell-bottom";
    var rowTot = [0, 0, 0, 0, 0, 0, 0];

    switch (GRID.Style) {
        case 0:
            GRID.CalcEqualInterval();
            break;

        case 1:
            GRID.CalcStdDev();
            break;
    }

    d = "<table cellpadding='0' cellspacing='0'>"
      + "<tr>"
      + "<td class='dow'>&nbsp;</td>";
    for (cx = 0; cx < 7; cx++) {
        rowTot[cx] = 0;
        d += "<td class='dow'>" + GRID.Days[cx] + "</td>";
    }
    d += "<td class='dow'>" + GRID.TotDesc + "</td>";
    d += "</tr>";

    for (rx = 0; rx < 24; rx++) {
        if (rx > 9) pre = "";
        hour = pre + rx;
        //if (rx == 23) cell = "cell-bottom";
        s = "<tr><td class='hour'>" + hour + "</td>";
        cols = GRID.SummaryData[rx].col;
        colTot = 0;
        for (cx = 0; cx < 7; cx++) {
            val = cols[cx].val;
            colTot += val;
            rowTot[cx] += val;
            style = "background-color:" + GRID.GetColor(val) + ";";
            if (val < 1 || UIHelper.IsLinkMap) {
                onclick = "";
                cell = "cell";
            } else {
                cell = "cell-data";
                style += "cursor:pointer;";
                onclick = "javascript:GRID.CellClick(" + rx + "," + cx + "," + val + ");";
            }
            s += "<td onclick='" + onclick + "' class='" + cell + "' style='" + style + "'>" + val + "</td>";
        }
        // Add total cell
        style = "background-color:" + GRID.CellTotalColor + ";";
        if (colTot < 1 || UIHelper.IsLinkMap) {
            onclick = "";
            hourTotal = "hour-total";
        } else {
            hourTotal = "hour-total-data";
            style += "cursor:pointer;";
            onclick = "javascript:GRID.CellClick(" + rx + ",7," + colTot + ");";
        }
        s += "<td onclick='" + onclick + "' class='" + hourTotal + "' style='" + style + "'>" + colTot + "</td>";

        s += "</tr>";
        d += s;
    }
    s = "<tr><td class='hour'>" + GRID.TotDesc + "</td>";
    colTot = 0;
    for (cx = 0; cx < 7; cx++) {
        colTot += rowTot[cx];
        style = "background-color:" + GRID.CellTotalColor + ";";
        if (rowTot[cx] < 1 || UIHelper.IsLinkMap) {
            onclick = "";
            cellBottom = "cell-bottom";
        } else {
        cellBottom = "cell-bottom-data";
            style += "cursor:pointer;";
            onclick = "javascript:GRID.CellClick(24," + cx + "," + rowTot[cx] + ");";
        }
        s += "<td onclick='" + onclick + "' class='" + cellBottom + "' style='" + style + "'>" + rowTot[cx] + "</td>";
    }
    style = "background-color:" + GRID.CellTotalColor + ";cursor:pointer;";
    s += "<td onclick='javascript:GRID.UpdateMapForAllGrids();' class='cell-bottom-data' style='" + style + "'>" + colTot + "</td>";
    s += "</tr></table>";
    d += s;

    if (colTot != GRID.Total) UIHelper.ShowStatusMessage(GRID.DOWWarning + " " + (GRID.Total - colTot), "warning");

    $("#grid-data").html(d);
    $("#grid-stats").show();
};
GRID.GetColor = function(val) {
    //if (val == 0) return GRID.CellColors[0];
    var chkVal = val;

    switch (GRID.Style) {
/*        case 0:
            chkVal = val;
            break;
*/        case 1:
            chkVal = (parseFloat(val) - GRID.Average) / GRID.StdDev;
            break;
    }

    var bgIdx = GRID.NumBreaks;
    for (var idx = 0; idx < GRID.NumBreaks; idx++) {
        if (GRID.Breaks[idx] >= chkVal) {
            bgIdx = idx;
            break;
        }
    }

    return GRID.CellColors[bgIdx];
};

GRID.CellClick = function(row, col, count) {
    //alert("Row = " + row + ", Col = " + col);

    var dates = "";
    var idx, hour = "", time = "", dow = "", dayDesc = "", timeDesc = "";

    if (col < 7) {
        for (idx = 0; idx < col; idx++) dow += "0";
        dow += "1";
        for (idx = col + 1; idx < 7; idx++) dow += "0";
        dayDesc = GRID.Days[col];
    } else {
    dayDesc = GRID.AllDaysDesc;
    }

    if (row < 24) {
        if (row < 10) hour = "0";
        hour += row;
        time = hour + "00" + hour + "59";
        timeDesc = hour;
    } else {
    timeDesc = GRID.AllTimesDesc;
    }

    if (WHEN.IsValid) dates = WHEN.DateSelection;

    GRID.WhenQuery = dates + "|" + time + "|Sliced|" + dow;
    //alert("GRID.WhenQuery = " + GRID.WhenQuery);

    if (GRID.LayerQuery == "") {
        UIHelper.ShowStatusMessage(GRID.CannotUpdateMap, "warning");
    } else {
        UIHelper.UpdateGridDrilldownMap();
        $("#report-tab a").unbind("click");
        $("#report-tab a").bind("click", function(e) { UIHelper.SetupReportTab(); });
        $("#grid-subset").html("<p>" + GRID.Selected + ": " + dayDesc + "-" + timeDesc + ", " + GRID.TotalDesc + " = " + count + "</p>");
        $("#grid-subset").show();
        $("#grid-links").show();
        GRID.HaveSubset = true;
    }
};

GRID.CalcEqualInterval = function() {
    GRID.NumBreaks = 5;
    GRID.CellColors = GRID.EqualColors;

    var diff = (parseFloat(GRID.Maximum) - parseFloat(GRID.Minimum) + 1.0) / parseFloat(GRID.NumBreaks);
    var brkVal = parseFloat(GRID.Minimum);

    for (rx = 0; rx < GRID.NumBreaks; rx++) {
        brkVal += diff;
        GRID.Breaks[rx] = brkVal;
    }

    alert("Breaks = " + GRID.NumBreaks + ", diff = " + diff + ", GRID.Breaks[0] = " + GRID.Breaks[0] + ", GRID.Minimum = " + GRID.Minimum + ", GRID.Maximum = " + GRID.Maximum);
};

GRID.CalcStdDev = function() {
    var rx, cx, cols, val, sqrAvg, sqrTot = 0.0, stdDev;

    // Calc the total value of the sqrt of all cells
    for (rx = 0; rx < 24; rx++) {
        cols = GRID.SummaryData[rx].col;
        for (cx = 0; cx < 7; cx++) {
            val = parseFloat(cols[cx].val) - GRID.Average;
            sqrTot += val * val;
        }
    }
    // The std dev is the sqrt of the average of the total sqrts
    sqrAvg = sqrTot / 168.0;
    GRID.StdDev = Math.sqrt(sqrAvg);

    //alert("avg = " + GRID.Average + ", sqrt avg = " + sqrAvg + ", std dev = " + GRID.StdDev);

    GRID.NumBreaks = 10;
    GRID.CellColors = GRID.StdDevColors;
    GRID.Breaks[0] = -3.0;
    GRID.Breaks[1] = -2.0;
    GRID.Breaks[2] = -1.5;
    GRID.Breaks[3] = -1.0;
    GRID.Breaks[4] = -0.5;
    GRID.Breaks[5] = 0.5;
    GRID.Breaks[6] = 1.0;
    GRID.Breaks[7] = 1.5;
    GRID.Breaks[8] = 2.0;
    GRID.Breaks[9] = 3.0;

    var stats = "<p>"
              + GRID.MinDesc + "&nbsp;=&nbsp;" + GRID.Minimum + ", "
              + GRID.MaxDesc + "&nbsp;=&nbsp;" + GRID.Maximum + ", "
              + GRID.AvgDesc + "&nbsp;=&nbsp;" + GRID.Average.toFixed(2) + ", "
              + GRID.StdDevDesc + "&nbsp;=&nbsp;" + GRID.StdDev.toFixed(2) 
              + "</p>";
    var leg = "<table cellpadding='0' cellspacing='5'><tr>";
    for (cx = 0; cx <= GRID.NumBreaks; cx++) {
        leg += "<td style='background-color:" + GRID.CellColors[cx] + ";'>&nbsp;</td>";
    }
    leg += "</tr><tr>";
    for (cx = 0; cx < GRID.NumBreaks; cx++) {
        switch (cx) {
            case 0:
                leg += "<td>&lt;&nbsp;" + GRID.Breaks[cx].toFixed(1) + "</td>";
                break;
            case 1: case 2: case 3: case 4:
                leg += "<td>" + GRID.Breaks[cx].toFixed(1) + "&nbsp;to<br/>" + GRID.Breaks[cx - 1].toFixed(1) + "</td>";
                break;
            case 5:
                leg += "<td>" + GRID.Breaks[cx - 1].toFixed(1) + "&nbsp;to<br/>+" + GRID.Breaks[cx].toFixed(1) + "</td>";
                break;
            case 6: case 7: case 8: case 9:
                leg += "<td>+" + GRID.Breaks[cx - 1].toFixed(1) + "&nbsp;to<br/>+" + GRID.Breaks[cx].toFixed(1) + "</td>";
                break;
        }
    }
    leg += "<td>&gt;&nbsp;+" + GRID.Breaks[GRID.NumBreaks - 1].toFixed(1) + "</td>";
    leg += "</tr></table>";

    $("#grid-stats").html(stats + leg + "<p>" + GRID.StandardDeviationsDesc + "</p>");
};

GRID.UpdateMapForAllGrids = function() {
    //alert("GRID.UpdateMapForAllGrids");
    if (GRID.LayerQuery == "") return;

    GRID.WhenQuery = UIHelper.WhenQuery;
    UIHelper.UpdateGridDrilldownMap();
    $("#report-tab a").unbind("click");
    $("#report-tab a").bind("click", function(e) { UIHelper.SetupReportTab(); });

    $("#grid-subset").hide();
    $("#grid-links").hide();
};

