var wind_directions = new Array("N", "NNE", "NE", "ENE", "E", "ESE", "SE", "SSE", "S", "SSW", "SW", "WSW", "W", "WNW", "NW", "NNW");

function get_el(id) {
	return document.getElementById(id);
}

function set_indicators() {
	var len = data.length;
	var el;
	for(var i = 0; i < len; i++) {
		el = get_el(data[i].name + "-indicator");

		// Limit readings to full-scale
		if(data[i].value > data[i].max) {
			data[i].value = data[i].max
		}
		else if(data[i].value < data[i].min) {
			data[i].value = data[i].min
		}

		switch(data[i].name) {
			case "winddirection":
				/*
				 *	Wind direction is quantised into 16 possible directions.
				 *	Rather than use a lookup table for ranges, we calculate the
				 *	array index based on the angle.
				 *	We use this both as a text label and also to request the appropriate image.
				 */
				 var degrees_per_segment = 360 / 16;
				 var angle_boundary_offset = degrees_per_segment / 2;
				 // FFS, "1" + 1 = "11".  String concatenation is not addition so it should have its own operator!  Something that PHP actually got right.  Javascript fail.
				 var direction_index = Math.floor(((parseFloat(data[i].value) + angle_boundary_offset) % 360) / degrees_per_segment, 0);
				 var new_img = document.createElement('img');
				 new_img.src = "/weatherstation/graphics/wind-" + wind_directions[direction_index] + ".jpg";
				 new_img.width = 100;
				 new_img.height = 101;
				 el.appendChild(new_img);
				 el = get_el("winddirection-label");
				 new_p = document.createElement('p');
				 new_text = document.createTextNode(wind_directions[direction_index]);
				 new_p.appendChild(new_text);
				 new_p.className = "reading";
				 el.appendChild(new_p);
				break;
			case "temperature":
				/*
				 *	Note the background is a "full-scale" thermometer and we paint over it with white
				 *	from the top down.
				 *	Otherwise we'd have to calculate both height and top attributes.
				 */
				var offset_value = data[i].max - data[i].value;
				el.style.height = Math.round(offset_value * data[i].gain, 0) + data[i].offset + "px";
				break;
			case "windspeed":
				el.style.left = Math.round(data[i].value * data[i].gain, 0) + data[i].offset + "px";
				break;
			case "windspeedmax":
				el.style.left = Math.round(data[i].value * data[i].gain, 0) + data[i].offset + "px";
				break;
			default:
				el.style.top = Math.round(data[i].value * data[i].gain, 0) + data[i].offset + "px";
				break;
		}
	}
}
