/** ===========================================================================================================
 * single-function definition of custom DNP map-type, currently called "Vector" in map
 *
 */
function DnpMapClean() {

	tilepath = "/_assets/img/dnptiles/";
	tilepathzoom = tilepath + "w_zoom";
	tilepathblank = tilepath + "white_256x256.png";
	
	// init copyright
	copyrights = new GCopyrightCollection('Map data');
	copyrights.addCopyright(
		new GCopyright('dnp',	// index
		new GLatLngBounds(new GLatLng(-90,-180), new GLatLng(90,180)),	// bounds
		0,	// minZoom
		'©2006-09 Delta National Park' // copyrightText
	));

	// init tileset
	var tileset = new GTileLayer(copyrights, 9, 14);  // (copyrightCollection, minZoom, maxZoom)
	tileset.getTileUrl = function(tile, zoom){
		switch (zoom) {

			// 2x3 [6] 512x768
			case 9:
				if (tile.x>=82 && tile.x<=83 && tile.y>=196 && tile.y<= 198) {
					url = tilepathzoom + zoom + '/' + tile.x + '_' + tile.y + '_' + zoom + '.png'
				} else {
					url = tilepathblank;
				}
				break;

			// 3x5 [15] 768x1280
			case 10:
				if (tile.x>=165 && tile.x<=167 && tile.y>=392 && tile.y<= 396) {
					url = tilepathzoom + zoom + '/' + tile.x + '_' + tile.y + '_' + zoom + '.png'
				} else {
					url = tilepathblank;
				}
				break;

			// 5x9 [45] 1280x2304
			case 11:
				if (tile.x>=330 && tile.x<=334 && tile.y>=785 && tile.y<= 793) {
					url = tilepathzoom + zoom + '/' + tile.x + '_' + tile.y + '_' + zoom + '.png'
				} else {
					url = tilepathblank;
				}
				break;

			// 9x18 [630]
			case 12:
				if (tile.x>=660 && tile.x<=668 && tile.y>=1570 && tile.y<= 1587) {
					url = tilepathzoom + zoom + '/' + tile.x + '_' + tile.y + '_' + zoom + '.png'
				} else {
					url = tilepathblank;
				}
				break;

			// 18x35 [630] 4608x8960 original image
			// 20x36 [720] 5120x9216 will downsize more nicely for next time 1320-1339, 3140,3175
			case 13:
				if (tile.x>=1321 && tile.x<=1338 && tile.y>=3141 && tile.y<= 3175) {
					url = tilepathzoom + zoom + '/' + tile.x + '_' + tile.y + '_' + zoom + '.png'
				} else {
					url = tilepathblank;
				}
				break;

			// 36x70 [2520] 9216x17920 original image
			case 14:
				if (tile.x>=2642 && tile.x<=2677 && tile.y>=6282 && tile.y<= 6351) {
					url = tilepathzoom + zoom + '/' + tile.x + '_' + tile.y + '_' + zoom + '.png'
				} else {
					url = tilepathblank;
				}
				break;

			default:
				url = tilepathblank;
		}

		return url;
	};
	tileset.isPng = function() { return false; }
	tileset.getOpacity = function() { return 0.9; }

	var customMap = new GMapType(
		//[G_HYBRID_MAP.getTileLayers()[0], tileset],
		[tileset],
		new GMercatorProjection(24),
		'Vector', {		shortName:		'DNP',
						tileSize:		256,
						maxResolution:	14,
						minResolution:	9,
						textColor: '#999999',
						linkColor: '#5368A6',
						radius: 6378137	}
	);

	return customMap;

} // END DnpMapClean


/** ===========================================================================================================
 * JavaScript prototype for the DNP-styled Google Maps custom zoom controls
 *
 */
function DnpZoomControl() { }
	
	DnpZoomControl.prototype = new GControl();
	
	DnpZoomControl.prototype.initialize = function(map) {
		var container = document.createElement("div");
		
		var zoomInDiv = document.createElement("div");
			this.setButtonStyle_(zoomInDiv);
			this.setZoomInStyle_(zoomInDiv);
			container.appendChild(zoomInDiv);
			zoomInDiv.appendChild(document.createTextNode("+"));
			//zoomInDiv.innerHTML = '<img src="/sites/all/themes/dnp/_assets/img/gmap-zoomcontrol-in.png" width="18" height="18" />';
			GEvent.addDomListener(zoomInDiv, "click", function() {
				map.zoomIn();
			});
		
		var zoomOutDiv = document.createElement("div");
			this.setButtonStyle_(zoomOutDiv);
			this.setZoomOutStyle_(zoomOutDiv);
			container.appendChild(zoomOutDiv);
			zoomOutDiv.appendChild(document.createTextNode("-"));
			GEvent.addDomListener(zoomOutDiv, "click", function() {
				map.zoomOut();
			});
		
		map.getContainer().appendChild(container);
		return container;
	}
	
	DnpZoomControl.prototype.getDefaultPosition = function() {
		return new GControlPosition(G_ANCHOR_BOTTOM_LEFT, new GSize(400, 18));
	}
	
	DnpZoomControl.prototype.setButtonStyle_ = function(button) {
		button.style.textDecoration = "none";
		button.style.color = "#FFFFFF";
		button.style.backgroundColor = "#333333";
		button.style.font = "small Arial";
		button.style.border = "0px solid black";
		button.style.padding = "0px";
		button.style.margin = "0px";
		button.style.textAlign = "center";
	
		button.style.position = "absolute";
		button.style.top = "0px";
		button.style.height = "18px";
		button.style.width = "18px";
		button.style.cursor = "pointer";
		button.style.overflow = "hidden";
		button.style.textIndent = "-9000px";
	}
	DnpZoomControl.prototype.setZoomInStyle_ = function(button) {
		button.style.left = "0px";
		button.style.background = "url(/_assets/img/gmap/gmap-zoomcontrol-in.png) no-repeat 0 0";
	}
	DnpZoomControl.prototype.setZoomOutStyle_ = function(button) {
		button.style.left = "18px";
		button.style.background = "url(/_assets/img/gmap/gmap-zoomcontrol-out.png) no-repeat 0 0";
	}
	
	
	
	
	
/** ===========================================================================================================
 * Map Layers control
 *
 * Modified from http://econym.org.uk/gmap/layercontrol.htm
 */	
function DnpLayersControl() {}
	DnpLayersControl.prototype = new GControl();
	
	DnpLayersControl.prototype.initialize = function(map) {
		var layersDiv = document.createElement("div");
		layersDiv.style.width = "60px";
		layersDiv.style.height = "18px";
		layersDiv.style.background = "url(/_assets/img/gmap/gmap-typecontrol-layers.png) no-repeat 0 0";
		layersDiv.style.overflow = "hidden";
		layersDiv.style.textIndent = "-9000px";
		layersDiv.appendChild(document.createTextNode("Layers"));

		map.getContainer().appendChild(layersDiv);
		
		GEvent.addDomListener(layersDiv, "mouseover", function() {
			map.addControl(layerControl);
		});
		
		return layersDiv;
	}

	DnpLayersControl.prototype.getDefaultPosition = function() {
		return new GControlPosition(G_ANCHOR_BOTTOM_LEFT, new GSize(310, 0));
	}

	// The "Layer" control displays the "More..." plus the checkboxes
	// The checkbox info is passed in the "opts" parameter

function LayerControl(opts) {
	this.opts = opts;
}
	LayerControl.prototype = new GControl();

	LayerControl.prototype.initialize = function(map) {
		var container = document.createElement("div");
		container.style.border = "1px solid black";
		container.style.fontSize = "12px";
		container.style.fontFamily = "helvetica, arial, sans-serif";
		container.style.width="150px";
		container.style.backgroundColor = "#ffffff";
		//container.innerHTML = '<center><b>Layers<\/b><\/center>';

		for (var i=0; i<this.opts.length; i++) {
			if (layers[i].Visible) { var c = 'checked';	} else { var c = ''; }
			container.innerHTML += '<input type="checkbox" onclick="toggleLayer('+i+')" ' +c+ ' /> '+this.opts[i]+'<br>';
		}

		map.getContainer().appendChild(container);

		// === This doesn't do what I want. It kills the control if I mouseover a checkbox ===
		// === If you know how to do this better, let me know ===
		//GEvent.addDomListener(container, "mouseout", function() {
		//  map.removeControl(layerControl);
		//});

		setTimeout("map.removeControl(layerControl)",5000);

		return container;
	}

	LayerControl.prototype.getDefaultPosition = function() {
		return new GControlPosition(G_ANCHOR_BOTTOM_LEFT, new GSize(310, 18));
	}
	

// ==== toggleLayer adds and removes the layers ====
function toggleLayer(i) {
	if (layers[i].Visible) {
		layers[i].hide();
	} else {
		if(layers[i].Added) {
			layers[i].show();
		} else {
			map.addOverlay(layers[i]);
			layers[i].Added = true;
		}
	}
	layers[i].Visible = !layers[i].Visible;
}


// Create the GLayer()s, and set them  Visible=false Added=false
// If you want a GLayer open by default, addOverlay() it and set it  Visible=true Added=true
var layers = [];
	layers[0] = new GGeoXml("http://deltanationalpark.org/xml/map_images/");
	layers[0].Visible = false;
	layers[0].Added = false;

	layers[1] = new GGeoXml("http://deltanationalpark.org/xml/map_videos/");
	layers[1].Visible = false;
	layers[1].Added = false;

	layers[2] = new GGeoXml("http://deltanationalpark.org/xml/map_placemarks/");
	layers[2].Visible = false;
	layers[2].Added = false;

	layers[3] = new GLayer("org.wikipedia.en");
	layers[3].Visible = false;
	layers[3].Added = false;
		
	layers[4] = new GLayer("com.panoramio.all");
	layers[4].Visible = false;
	layers[4].Added = false;
	
	layers[5] = new GLayer("com.youtube.all");
	layers[5].Visible = false;
	layers[5].Added = false;
	

// create the layerControl, but don't addControl() it
// pass it an array of names for the checkboxes
var layerControl = new LayerControl(["Images", "Videos", "Placemarks", "Wikipedia", "Panaramio Photos", "YouTube"]);



/** =========================================================================================================== */

/*
function gmap_layer(url) {
	if (map) {
		var geoXml = new GGeoXml(url, function() {
			if (geoXml.loadedCorrectly()) {
				map.addOverlay(geoXml);
				return 1;
			} else {
				return 0;
			}
		});
	} 
}
*/





	
/** ===========================================================================================================
 * Loads a KML file into the map from a given URL
 * FUNCTION IS IN USE AS OF 09 AUG 2009
 *
 * @access	public
 * @param	url
 * @return	1 if successful, o if failure
 */
function gmap_load_kml(url) {
	waitToLoad = function() {
		if (map) {
			//map.removeOverlay(geoXml);
			map.clearOverlays();
			var geoXml = new GGeoXml(url, function() {
				if (geoXml.loadedCorrectly()) {
					map.addOverlay(geoXml);
					//geoXml.gotoDefaultViewport(map);
					return 1;
				} else {
					return 0;
				}
			});
		} else {
			setTimeout(waitToLoad, 500);
		}
	}
	waitToLoad();
}
