$(document).foundation();
var clockRunning = false;
var autoload = true;
var reloadPeriod = 5000;
var clockRunningLabel, clockStoppedLabel;
var clockSpeed = "1.0";
$(document).ready(function() {
	clockRunningLabel = $("#clockRunningLabel");
	clockStoppedLabel = $("#clockStoppedLabel");
});
function setRunningLabel(running) {
	if (running) {
		clockRunningLabel.innerHtml('Running at 1:'+clockSpeed).show();
		clockStoppedLabel.hide();
	} else {
		clockRunningLabel.hide();
		clockStoppedLabel.innerHtml(' Stopped').show();
	}
}
function getClockStatus(){
	var xh = new XMLHttpRequest();
	xh.onreadystatechange = function() {
		if (xh.readyState == 4) {
			if (xh.status == 200) {
				var result = JSON.parse(xh.responseText);
				/*
				heap.add(res.heap);
				temp.add(res.analog);
				digi.add(res.gpio);
				if (autoload) setTimeout(getClockStatus, reloadPeriod);
				*/
				clockRunning = result.clock_active;
				clockSpeed = result.clock_speed;
			} else running = false;
		}
	};
	xh.open("GET", "/clock/status", true);
	xh.send(null);
};
function stopClock() {
	var xh = new XMLHttpRequest();
	xh.onreadystatechange = function() {
		if (xh.readyState == 4) {
			if (xh.status == 200) {
				clockRunning = false;
				setRunningLabel(clockRunning);
			}
		}
	};
	xh.open("GET", "/clock/stop", true);
	xh.send(null);
};
function startClock(){
	var xh = new XMLHttpRequest();
	xh.onreadystatechange = function() {
		if (xh.readyState == 4) {
			if (xh.status == 200) {
				clockRunning = true;
				setRunningLabel(clockRunning);
			}
		}
	};
	xh.open("GET", "/clock/start", true);
	xh.send(null);
};