// Some class constants
//RadarTileLayer.prototype.copyrights = new GCopyrightCollection("Weatherflow, Inc");
//RadarTileLayer.prototype.minRes = 1;
//RadarTileLayer.prototype.maxRes = 17;

// Creates and instance of the radar tile layer
function RadarTileLayer(map) {
	
	this.minRes = 1;
	this.maxRes = 17;
	this.map = map;
	this.overlay = new GTileLayerOverlay(this);
	this.opacity = 0.75;
	this.baseURL = "http://cs4.weatherflow.com/cgi-bin/radar.pl?";
	this.requestTime = new Date() / 1000;
}

RadarTileLayer.prototype = new GTileLayer(new GCopyrightCollection("Weatherflow, Inc"), 1, 17);

// Creates a Radar Map Type out of the radar map layer
RadarTileLayer.prototype.GetRadarMapType = function() {
	var mapOpts = new Object();
	mapOpts.maxResolution = this.maxRes;
	mapOpts.minResolution = this.minRes;
	
	return new GMapType(new Array(G_NORMAL_MAP.getTileLayers()), new GMercatorProjection(19), "Radar_Test", mapOpts);
}

// Gets the tile URL
RadarTileLayer.prototype.getTileUrl = function(tile, zoom, unbounded) {
	var projection = this.map.getCurrentMapType().getProjection();

	var topLeftPX = new GPoint(tile.x * 256, (tile.y) * 256);
	var bottomRightPX = new GPoint((tile.x+1) * 256, (tile.y+1) * 256);

	var topLeftLL = projection.fromPixelToLatLng(topLeftPX, zoom, unbounded);
	var bottomRightLL = projection.fromPixelToLatLng(bottomRightPX, zoom, unbounded);

	// TODO: Needs to pass the projection to the radar tile generator
	var url = this.baseURL;
	url += "topleft=" + topLeftLL.toUrlValue();
	url += "&bottomright=" + bottomRightLL.toUrlValue();
	url += "&time=" + this.GetRequestTime();

	return url;
}

// Gets the current request time of the layer (Unix Epoch)
RadarTileLayer.prototype.GetRequestTime = function() {
	// Floor the time to the 5 minute mark
	var time = Math.floor(this.requestTime / 300) * 300;
	return time;
}

// Sets the current request time of the layer (Unix Epoch)
RadarTileLayer.prototype.SetRequestTime = function(requestTime) {
	this.requestTime = requestTime;
	//this.map.setMapType(this.map.getCurrentMapType());
	//GLog.write('Step');
	this.overlay.redraw();
}


RadarTileLayer.prototype.isPng = function() {
	return false;
}

RadarTileLayer.prototype.getOpacity = function() {
	return this.opacity;
}


