

//  Constructor
// The source url specifies the source of the weatherstation data
// The direction_url specifies the base path of the direction arrow images
function WfMarker(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.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;
}

WfMarker.prototype = new GOverlay();

// Marker functions

// Set the position of the marker on the map
WfMarker.prototype.SetPos = function(marker) {
	if (marker.weatherstation) {
		var div_pos = this.map.fromLatLngToDivPixel(marker.latlng);
		marker.div.style.left = div_pos.x - this.marker_width / 2 + "px";
		marker.div.style.top = div_pos.y - this.marker_height / 2 + "px";
	}
}

// Hides all the markers on the map
WfMarker.prototype.ClearMarkers = function() {
	for (i = 0; i < this.max_marker_count; i++) {
		var x = this.markers[i];
		if (x.weatherstation) {
			x.weatherstation = null;
			x.div.parentNode.removeChild(x.div);
		}
	}
}

// Removes the markers from the map
WfMarker.prototype.RemoveMarkers = function() {
	for (i = 0; i < this.max_marker_count; i++) {
		var x = this.markers[i];
		x.img.parentNode.removeChild(x.img);
		x.spd.parentNode.removeChild(x.spd);
		if (x.div.parentNode) {
			x.div.parentNode.removeChild(x.div);
		}
		x.weatherstation = null;
		x.img.onclick = null;
	}

	this.markers.length = 0;
	this.markers = null;
}

// Called when user clicks on a marker
// Triggers the click event on the weatherstation class
WfMarker.prototype.MarkerClicked = function(marker) {
	GEvent.trigger(this, 'click', marker);
}


// AJAX Methods

// Determines if a weather station request is in progress
WfMarker.prototype.CallInProgress = function(request) {
	switch(request.transport.readyState) {
		case 1: 
		case 2: 
		case 3:
			return true;
			break;
		default:
			return false;
			break;
	}
}

// Called when server replies with a set of markers for the map
WfMarker.prototype.OnLoadMarkers = function(request,json) {
	if (this.loadingControl) {
		this.map.removeControl(this.loadingControl);
	}

	GEvent.trigger(this, "before_marker_load", eval("(" + request.responseText + ")"));
	this.LoadMarkers(request,json);
	GEvent.trigger(this, "after_marker_load", eval("(" + request.responseText + ")"));
}


WfMarker.prototype.LoadMarkers = function(request,json) {
}

// Sets the src of the image and applies alpha image loader to ie
WfMarker.prototype.SetImageSrc = function(img, src) {
	if (document.all && /\.png$/.test(src.toLowerCase())) {
		img.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled=true, src='" + src + "')";
		img.src = 'http://mapper.weatherflow.com/images/windIcons/blank.gif';
	} else {
		img.src = src;
	}
}

// Autoreloads markers after a certain amount of time as passed.
WfMarker.prototype.Autoreload = function() {
	if (this.map) {
		this.RequestMarkers();
		setTimeout(this.AutoreloadHandler, this.autoreload_timeout);
	}
}

// Reload markers at end of map move
WfMarker.prototype.MapMoveEnd = function() {
	this.StartRequestTimeout();
}

// Clears out any request timers and creates a new timeout request
WfMarker.prototype.StartRequestTimeout = function() {

	if (this.reloadTimerID) {
		clearTimeout(this.reloadTimerID);
		this.reloadTimerID = 0;
	}

	this.reloadTimerID = setTimeout(this.RequestMarkersHandler, this.default_timeout);
}

// Requests the a set of markers for the map
WfMarker.prototype.RequestMarkers = function() {
	if (this.loadingControl) {
		this.map.addControl(this.loadingControl);
	}

	// Abort old marker request
	if (this.markerRequest && this.CallInProgress(this.markerRequest)) {
		this.markerRequest.transport.abort();
		this.markerRequest = null;
	}

	this.reloadTimerID = 0;

	var bounds = this.map.getBounds();
	var northEast = bounds.getNorthEast();
	var southWest = bounds.getSouthWest();

	center = this.map.getCenter();

	var url = this.source_url;
	var pars = 'minLat=' + southWest.lat();
	pars += '&minLon=' + southWest.lng();
	pars += '&maxLat=' + northEast.lat();
	pars += '&maxLon=' + northEast.lng();
	pars += '&zoomLevel=' + this.map.getZoom();

	this.markerRequest = new Ajax.Request(
		url,
		{
			method: 'get',
			parameters: pars,
			onComplete: this.LoadMarkersHandler
		});
}


// GMap overrides

// Called right after adding the overlay to the map
// Binds to moveend 
WfMarker.prototype.initialize = function(map) {
	this.map = map;
	this.map_pane = map.getPane(G_MAP_MARKER_MOUSE_TARGET_PANE);

	// Create the event handlers
	this.RequestMarkersHandler = GEvent.callback(this, this.RequestMarkers);
	this.LoadMarkersHandler = GEvent.callback(this, this.OnLoadMarkers);
	this.AutoreloadHandler = GEvent.callback(this, this.Autoreload);

	this.loadingControl = null;
	try{
		this.loadingControl = new Loading();
	} catch(err) {
	}

	// Create the markers for the map
	this.markers = new Array(); // The array of markers on the map
	for (var i = 0; i < this.max_marker_count; i++) {
		var m = {
		div: document.createElement("div"),
		img: document.createElement("img"),
		spd: document.createElement("div")
		}

		m.div.style.position = "absolute";
		m.div.style.width = this.marker_width + "px";
		m.div.style.height = this.marker_height + "px";
		m.div.style.visibility = "visible";
		if (document.all) {
			m.div.onselectstart = function() { return false; }
			m.div.ondragstart = function() { return false; }
		} else {
			m.div.style.MozUserSelect = 'none';
		}

		m.img.style.position = "absolute";
		m.img.style.top = "0px";
		m.img.style.left = "0px";
		m.img.onclick = GEvent.callbackArgs(this, this.MarkerClicked, m);
		if (document.all) {
			m.img.style.cursor = "hand";
			m.img.onselectstart = function() { return false; }
			m.img.ondragstart = function() { return false; }
		} else {
			m.img.style.cursor = "pointer";
			m.img.style.MozUserSelect = 'none';
		}

		m.spd.style.position = "relative";
		m.spd.style.top = "30%";
		m.spd.style.left = "0px";
		m.spd.style.width = this.marker_width + "px";
		m.spd.style.height = this.marker_height + "px";
		m.spd.style.textAlign = "center";
		m.spd.style.visibility = "visible";
		m.spd.onclick = GEvent.callbackArgs(this, this.MarkerClicked, m);
		if (document.all) {
			m.spd.style.cursor = "hand";
			m.spd.onselectstart = function() { return false; }
			m.spd.ondragstart = function() { return false; }
		} else {
			m.spd.style.cursor = "pointer";
			m.spd.style.MozUserSelect = 'none';
		}

		m.div.appendChild(m.img);
		m.div.appendChild(m.spd);

		this.markers.push(m);
	}

	// Listen for the move end event in order to reload the markers on the map
	this.moveendBind = GEvent.bind(this.map, "moveend", this, this.MapMoveEnd);

	// Load the inital set of markers
	this.Autoreload();
}

// Called when overlay is removed from map
// Removes all weatherstation makers from the map
WfMarker.prototype.remove = function() {
	this.RemoveMarkers();
	this.map = null;
	GEvent.removeListener(this.moveendBind);
}

// Creates a copy of the WfMarker
WfMarker.prototype.copy = function() {
	return new WfMarker(this.source_url);
}

// Set the position of the markers on the map
WfMarker.prototype.redraw = function(force) {
	if (!force) return;

	for (var i = 0; i < this.markers.length; i++) {
		if (this.markers[i].weatherstation)
			this.SetPos(this.markers[i]);
	}
}

