<!DOCTYPE html>
<html>
<head>
<title>DIY Radiation</title>
<style>
body {
    background-image: url("static/chalk.jpg");
}
h2 {
    font: bold 24px arial, sans-serif;
}
h4 {
    font: bold 16px arial, sans-serif;
}
.mainContainer {
    width: 750px;
    text-align: center;
    margin: auto;
}
.datetime {
    font: bold 22px arial, sans-serif;
    padding: 20px;
}
.rowContainer {
    display: table;
    width: 100%;
}
.currentDataCell {
    width: 50%;
    font: bold 20px arial, sans-serif;
    text-align: center;
    display: table-cell;
    vertical-align: middle;
}
.dataItems {
    padding: 2px;
    text-align: left;
    line-height: 130%;
    display: inline-block;
    vertical-align: top;
}
.chartContainer {
    padding: 2px;
}
img.chart {
    width:100%;
}
.notes {
    font: 17px arial, sans-serif;
    text-align: left;
    padding: 10px;
}
span.chartNav {
    margin: auto;
}
ul.chartNav {
    list-style-type: none;
    margin: 10px;
    padding: 0;
    overflow: hidden;
    background-color: #bbb;
    text-align: center;
}
li.chartNav {
    display: inline-block;
    font: bold 18px arial, sans-serif;
    color: black;
}
text.chartNav:hover {
    background-color: #333;
    cursor: pointer;
    color: white;
}
text.chartNav {
    display: inline-block;
    padding: 8px 12px;
}
</style>
</head>

<body onload="main()">

<div class="mainContainer">

<h2>DIY Radiation Monitor</h2>
<h4>Albany, Oregon</h4>

<div class="datetime">
<text id="date"></text>
&nbsp;&nbsp;
<text id="time"></text>
</div>

<div class="rowContainer">
<div class="currentDataCell">
<ins>Current Reading</ins><br><br>
<div class="dataItems">
Counts per minute:<br>
Counts per second:<br>
uSv per hour:
</div>
<div class="dataItems">
<text id="cpm"></text><br>
<text id="cps"></text><br>
<text id="uSvPerHr"></text>
</div>
</div>

<div class="currentDataCell">
<ins>Radiation Monitor</ins><br><br>
<div class="dataItems">
Device:<br>
Mode:
</div>
<div class="dataItems">
<text id="status"></text><br>
<text id="mode"></text>
</div>
</div>
</div>

<span class="chartNav">
<ul class="chartNav">
<li class="chartNav">Select charts:</li>
<li class="chartNav"><text class="chartNav" onclick="show_charts(1)">24 hours</text></li>
<li class="chartNav"><text class="chartNav" onclick="show_charts(2)">4 weeks</text></li>
<li class="chartNav"><text class="chartNav" onclick="show_charts(3)">12 months</text></li>
</ul>
</span>
<br>

<div class="chartContainer">
<img class="chart" id="cpmChart">
</div>

<div class="chartContainer">
<img class="chart" id="uSvChart">
</div>

<div class="notes">
<b>NOTES:</b>
<ul>
<li>DIY Radiation Monitor project plans and software available at
<a href="https://github.com/fractalxaos/radmon" target="_new"><i>Github.com</i></a>.</li>
<li>Project sponsored by <a href="http://www.eugenemakerspace.com" TARGET="_NEW">
<i>Eugene Maker Space</i></a>, Eugene, Oregon.</li>
<li>For more information about the effects of radiation on the human body see
<a href="https://xkcd.com/radiation/" target="_blank">
Radiation Dose Chart</a> by Randall Monroe.</li>
</ul>
</div>
</div>
<br>

<script>

/* Globals */
var radmonDataUrl = "dynamic/radmonData.js";
var httpRequest = new XMLHttpRequest();
var graphPeriod;

function main() {
    getRadmonData();
    show_charts(1);
    setInterval(getRadmonData, 10000);
}

function getRadmonData() {
    httpRequest.onreadystatechange = function() {
        if (httpRequest.readyState == 4 && httpRequest.status == 200) {
            var dataArray = JSON.parse(httpRequest.responseText);
            displayData(dataArray[0]);
        }
    };
    httpRequest.open("POST", radmonDataUrl, true);
    httpRequest.send();
}

function show_charts(n) {
    graphPeriod = n;
    getRadmonGraphs();   
    setInterval(getRadmonGraphs, 60000);
}

function getRadmonGraphs() {
    var d = new Date;
    switch(graphPeriod) {
        case 1:
            document.getElementById("cpmChart").src =
                "dynamic/radGraph1.png?ver=" + d.getTime();
            document.getElementById("uSvChart").src = 
                "dynamic/radGraph2.png?ver=" + d.getTime();
            break;
        case 2:
            document.getElementById("cpmChart").src = 
                "dynamic/radGraph3.png?ver=" + d.getTime();
            document.getElementById("uSvChart").src = 
                "dynamic/radGraph4.png?ver=" + d.getTime();
            break;
       case 3:
            document.getElementById("cpmChart").src = 
                "dynamic/radGraph5.png?ver=" + d.getTime();
            document.getElementById("uSvChart").src = 
                "dynamic/radGraph6.png?ver=" + d.getTime();
            break;
    }
}

function displayData(dataItem) {
    var timeStamp, date, time, hourminute, status;

    timeStamp = dataItem.date;
    date = timeStamp.split(" ")[0];
    time = timeStamp.split(" ")[1];
    hourminute = time.split(":")[0] + ":" + time.split(":")[1];
    localDateObj = new Date();
    localTimeZone = localDateObj.getTimezoneOffset() / 60;
    
    document.getElementById("date").innerHTML = date;    
    document.getElementById("time").innerHTML =  
        hourminute + "  <small>(GMT+" + localTimeZone + ")</small>";    
    document.getElementById("cpm").innerHTML =  dataItem.CPM;    
    document.getElementById("cps").innerHTML =  dataItem.CPS;    
    document.getElementById("uSvPerHr").innerHTML =  dataItem.uSvPerHr;    
    document.getElementById("mode").innerHTML =  dataItem.Mode;    

    status = dataItem.status;
    document.getElementById("status").innerHTML = status;    
    if (status == "online") {
       document.getElementById("status").style.color = "green";
    } else {
       document.getElementById("status").style.color = "red";
    }
}

</script>

</body>
</html>