
//  Constructor
// The source url specifies the source of the weatherstation data
// The direction_url specifies the base path of the direction arrow images
function WeatherStation(source_url, direction_url) {

	if (source_url == null)
		source_url = 'json/weatherstation.php';

	if (direction_url == null)
		direction_url = 'images/windIcons/';

	this.source_url = source_url;
	this.img_src = direction_url;

	this.name = "Old Weatherstations";

	this.autoreload_timeout = 60000;
	this.default_timeout = 150; // Timeout in milliseconds to reload markers after map move end
	this.max_marker_count = 300; // The maximum number of makers on the map

	this.marker_width = 40;
	this.marker_height = 40;

	this.MAX_READING_AGE = 90 * 60; // 90 Minutes
}

WeatherStation.prototype = new WfMarker();

// Marker functions

// Called when server replies with a set of markers for the map
WeatherStation.prototype.LoadMarkers = function(request,json) {
	GEvent.trigger(this, 'beforeload', request);

	this.ClearMarkers();

	weatherstations = eval("(" + request.responseText + ")");

	var d = new Date();

	var marker_count = Math.min(weatherstations.length, this.max_marker_count);
	for ( var i = 0; i < marker_count; i++ ) {
		var m = this.markers[i];
		var ws = weatherstations[i];

		m.weatherstation = ws;
		m.latlng = new GLatLng(ws.lat, ws.lon);

		m.spd.title = ws.shortName;
		m.spd.innerHTML = '';

		if (ws.sensorReadings.length) {
			var rd = ws.sensorReadings[0];

			if (d.getTime()/1000 - rd.datetime > this.MAX_READING_AGE) {
				this.SetImageSrc(m.img, 'g_noData.png');
			} else {
				if (rd.direction == null || rd.direction == ' ') rd.direction = 'NULL';
				this.SetImageSrc(m.img, 'g_' + rd.direction.toUpperCase() + '.png');
				if (rd.average >= 0) {
					m.spd.innerHTML = Math.round(rd.average);
				} else if (ws.wfOnly) {
					m.spd.innerHTML = '<b>WF</b>';
				}
			}
		} else {
			if (ws.sensorTypeID == 17) 
				this.SetImageSrc(m.img, 'g_flag.png');
			else
				this.SetImageSrc(m.img, 'g_NULL.png');
		}
		this.SetPos(m);

		this.map_pane.appendChild(m.div);
	}

	this.markerRequest = null;

	if (this.loadingControl)
		this.loadingControl.Hide();

	GEvent.trigger(this, 'afterload');
}

// Sets the src of the image and applies alpha image loader to ie
WeatherStation.prototype.SetImageSrc = function(img, src) {
	if (document.all && /\.png$/.test(src.toLowerCase())) {
		img.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled=true, src='" + this.img_src + src + "')";
		img.src = 'http://mapper.weatherflow.com/images/windIcons/blank.gif';
	} else {
		img.src = this.img_src + src;
	}
}


