
function OverlayLegendControl() {
	this.relationships = new Array();
	this.tileLayerOverlay = null;
	this.overlayShowing = false;
	
	this.legendNode = null;
	this.copyright = null;
}

OverlayLegendControl.prototype = new GControl(true, true); // printable, selectable

OverlayLegendControl.prototype.initialize = function(map) {

	var overlay = this;

  var container = document.createElement("div");
	container.className = "legend-border";

  var legend = document.createElement("div");
	legend.className = "legend";
	
  container.appendChild(legend);
	
	var header = document.createElement("div");
	header.className = "legend-header";
	header.innerHTML = "<h5>Disease Map</h5>";
	
	var overlayCombo = this.buildOptions();
	overlayCombo.align = "center";
	var p = document.createElement("p");

	header.appendChild(p);
	p.appendChild(overlayCombo);
	
	legend.appendChild(header);
	
  var zoomOut = document.createElement("div");
	zoomOut.style.display = "none";
	zoomOut.className = "legend-zoom";

	zoomOut.innerHTML = "<p>Zoom out to<br />view overlay</p>";
	legend.appendChild(zoomOut);

	this.zoomOut = zoomOut;
	this.legend = legend;
	this.container = container;
	
	
			GEvent.addListener(map, "zoomend", function(oldLevel, newLevel)
			{
				overlay.zoomLimit(newLevel);
			}
		);
		
		  GEvent.addDomListener(zoomOut, "click", function() {
    map.setZoom(overlay.tileLayerOverlay.getTileLayer().maxResolution());
  });
	
	GEvent.addDomListener(overlayCombo, "click", function() {

  });
	
	GEvent.addDomListener(overlayCombo, "change", function()
	{
		var i = overlayCombo.selectedIndex;
		overlay.hide();	

		if (i > 0)
		{
			overlay.tileLayerOverlay = overlay.relationships[i - 1].tileOverlay;
			overlay.legendNode = overlay.relationships[i - 1].node;
			legend.appendChild(overlay.legendNode);

			var zoom = map.getZoom();
			
			if (zoom > overlay.tileLayerOverlay.getTileLayer().maxResolution())
			{
				overlay.legendNode.style.display = "none";
				overlay.zoomOut.style.display = "block";
			}
			else
			{
					overlay.zoomOut.style.display = "none";
					map.addOverlay(overlay.tileLayerOverlay);
					overlay.overlayShowing = true;
					
					//overlay.legendNode.style.position = "relative";
					overlay.legendNode.style.display = "block";
					
					
			}
		}
		
		overlay.moveCopyright(map);
		
		// Hack for IE - mousewheel zoom won't work if combo is focussed.
		setTimeout(function() { overlayCombo.blur(); }, 1000);
  });
	
 // GEvent.addDomListener(legend, "click", function() {
 //   map.setCenter(new GLatLng(20.00, 0.0), 2, G_PHYSICAL_MAP);
 // });
 
	this.overlayCombo = overlayCombo;

  map.getContainer().appendChild(container);
	
	// Let map finish loading before adjusting copyright:
	setTimeout(function() { overlay.moveCopyright(map) });
	
  return container;
}

OverlayLegendControl.prototype.buildOptions = function()
{
	var overlayCombo = document.createElement("select");
	
	var opt = document.createElement("option");
	opt.appendChild(document.createTextNode("None"));
	overlayCombo.appendChild(opt);
	
  for (var i = 0; i < this.relationships.length; i++)
	{
		var opt = document.createElement("option");
		opt.value = i;
		opt.appendChild(document.createTextNode(this.relationships[i].title));
		overlayCombo.appendChild(opt);
	}
	return overlayCombo;
}

OverlayLegendControl.prototype.getDefaultPosition = function()
{
  return new GControlPosition(G_ANCHOR_BOTTOM_RIGHT, new GSize(0, 0));
}

OverlayLegendControl.prototype.addRelationship = function(o, t, n)
{
	//
	this.relationships.push({ tileOverlay:o, title:t, node:n });
}

OverlayLegendControl.prototype.zoomLimit = function(zoom)
{
	if (this.tileLayerOverlay != null)
	{
		if (zoom > this.tileLayerOverlay.getTileLayer().maxResolution())
		{
			if (this.overlayShowing)
			{
				map.removeOverlay(this.tileLayerOverlay);
				this.zoomOut.style.display = "block";
				this.overlayShowing = false;
				this.legendNode.style.display = "none";
			}
		}
		else
		{
			if (!this.overlayShowing)
			{
				map.addOverlay(this.tileLayerOverlay);
				this.zoomOut.style.display = "none";
				this.overlayShowing = true;
				this.legendNode.style.display = "block";
			}
		}
	}
}

OverlayLegendControl.prototype.hide = function()
{
	if (this.overlayShowing)
	{
		this.container.removeChild(this.legend);
		map.removeOverlay(this.tileLayerOverlay);
		this.overlayShowing = false;
		this.legend.removeChild(this.legendNode);
		this.container.appendChild(this.legend);
	}
	this.zoomOut.style.display = "none";
	
	this.tileLayerOverlay = null;
}

OverlayLegendControl.prototype.findCopyright = function(map)
{
	if (!this.copyright)
	{
		// Find copyright:
		var divs = map.getContainer().getElementsByTagName("div");
		for (i = 0; i < divs.length; i++)
		{
			var terms = divs[i].getElementsByTagName("a");
			if ((terms.length == 1) && (terms[0].innerHTML == "Terms of Use"))
			{
				var copyright = divs[i];
				break;
			}
		}
		this.copyright = copyright;
	}
	return this.copyright;
}

OverlayLegendControl.prototype.moveCopyright = function(map)
{
	var overlay = this;
					
	var copyright = overlay.findCopyright(map);
	if (copyright)
	{
		copyright.style.right = (overlay.container.offsetWidth + 4) + "px";
	}
	else
	{
		setTimeout(function() { overlay.moveCopyright(map) }, 1000);
	}
}

// We define the function first
function ResetZoomControl() {
}

// To "subclass" the GControl, we set the prototype object to
// an instance of the GControl object
ResetZoomControl.prototype = new GControl();

// Creates a one DIV for each of the buttons and places them in a container
// DIV which is returned as our control element. We add the control to
// to the map container and return the element for the map class to
// position properly.
ResetZoomControl.prototype.initialize = function(map) {
  var container = document.createElement("div");
	
	container.className = "button-outer";
	container.title="Reset view"

  var zoomInDiv = document.createElement("div");
  zoomInDiv.className = "button-inner";
  container.appendChild(zoomInDiv);
  zoomInDiv.appendChild(document.createTextNode("Reset"));
  GEvent.addDomListener(zoomInDiv, "click", function() {
    map.setCenter(new GLatLng(10.00, 0.0), 2, G_PHYSICAL_MAP);
  });

  map.getContainer().appendChild(container);
  return container;
}

ResetZoomControl.prototype.getDefaultPosition = function() {
  return new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(12, 7, "em", "px"));
}


