var map;
var current_layer_name;
var current_region_uri;
var click_location;
var show_in_popup = 0;

function rnd(x) {
	return Math.round(x * 1000) / 1000;
}

function doClick() {
	req = new JSONscriptRequest('http://www.govtrack.us/perl/wms/get-region.cgi?layer=' + current_layer_name + '&lat=' + click_location.lat() + '&long=' + click_location.lng() + '&format=json&json_callback=wms_info_callback');
	req.buildScriptTag();
	req.addScriptTag();
}

function setLayer(layer_name) {
	var WMS_URL = 'http://www.govtrack.us/perl/wms/wms.cgi?';
	var G_MAP_LAYER_FILLED = createWMSTileLayer(WMS_URL, layer_name, null, "image/png", null, null, null, .5);
	var G_MAP_OVERLAY = createWMSOverlayMapType([G_NORMAL_MAP.getTileLayers()[0], G_MAP_LAYER_FILLED], "anonymous");
	G_MAP_OVERLAY.layer = layer_name;
	map.setMapType(G_MAP_OVERLAY);

	DHTML_ShowHide("introtext", 0);
	DHTML_ShowHide("title_intro", 0);
	
	DHTML_ShowHide("dyntext", 1);
	
	DHTML_ShowHide("introtext_" + "states", 0);
	DHTML_ShowHide("introtext_" + "cd-110", 0);
	DHTML_ShowHide("introtext_" + "sldu", 0);
	DHTML_ShowHide("introtext_" + "sldl", 0);
	DHTML_ShowHide("introtext_" + layer_name, 1);

	DHTML_ShowHide("title_" + "states", 0);
	DHTML_ShowHide("title_" + "cd-110", 0);
	DHTML_ShowHide("title_" + "sldu", 0);
	DHTML_ShowHide("title_" + "sldl", 0);
	DHTML_ShowHide("title_" + layer_name, 1);

	current_layer_name = layer_name;
	if (click_location != null) doClick();
	
	DHTML_ShowHide("embedinfo", 1);
	
	current_region_uri = null;
	
	updateEmbedInfo();
	
	SetInnerHtml('mymapscript', '<a href="http://maps.google.com/ig/add?synd=mpl&amp;pid=mpl&amp;moduleurl=http%3A//www.govtrack.us/perl/wms/gmapplet.cgi%3Flayer%3D' + layer_name + '"><img src="http://buttons.googlesyndication.com/fusion/add.gif" border="0" alt="Add to Google"/></a></div>');
}

function loadDynPage(regionuri) {
	SetInnerHtml('dyntext', 'Loading information. Please stand by...');
	SetInnerHTMLFromAjaxResponse("/perl/local-info.cgi?layer=" + map.getCurrentMapType().layer + "&uri=" + escape(regionuri), 'dyntext');
}

function updateEmbedInfo() {
	var b = map.getBounds();
	DHTML_SetText("embedargs",
		"layer=" + current_layer_name
		+ "&bounds=" + rnd(b.getSouthWest().lng()) + "," + rnd(b.getNorthEast().lat()) + "," + rnd(b.getNorthEast().lng()) + "," + rnd(b.getSouthWest().lat())
		+ (current_region_uri == null ? "" : "&region=" + current_region_uri)
		);
}

function initMap() {
	if (!GBrowserIsCompatible()) {
		alert("This page uses Google Maps, which is unfortunately not supported by your browser.");
	} else {
		document.getElementById("googlemap").style.height = (screen.height - 485) + "px";
		document.getElementById("leftpane").style.height = (screen.height - 485 - 150) + "px";
		map = new GMap2(document.getElementById("googlemap"));
		map.enableContinuousZoom();
		map.enableScrollWheelZoom();

		map.removeMapType(G_NORMAL_MAP);
		map.removeMapType(G_SATELLITE_MAP);
		map.removeMapType(G_HYBRID_MAP);
		
		map.addControl(new GLargeMapControl());
		map.addControl(new GMapTypeControl());
		//map.addControl(new GOverviewMapControl());
		map.addControl(new GScaleControl());
		map.setCenter(new GLatLng(39, -96), 4);

		GEvent.addListener(map, "click", function (overlay, point) {
			if (point && current_layer_name != null) {
				//current_layer_name = map.getCurrentMapType().layer;
				click_location = point;
				doClick();
			}
		});	

		GEvent.addListener(map, "move", updateEmbedInfo);
	}
}

function wms_info_callback(wms_listing) {
	var point = click_location; 
	
	if (wms_listing.length == 0) {
		map.openInfoWindowHtml(point, "<div style='font-size: 90%'>There is no district here.</div>");
		return;
	}
	
	uri = wms_listing[wms_listing.length-1][0]; // uri of last (smallest) matching region

	if (!show_in_popup) {
		map.openInfoWindowHtml(point, "<div style='font-size: 90%'>Details are loading to the left for this location.</div>");
		loadDynPage(uri);
	} else {
		RunAjax("/perl/local-info.cgi?layer=" + map.getCurrentMapType().layer + "&uri=" + escape(uri), 
			function(text) {
				map.openInfoWindowHtml(point, "<div style='font-size: 90%; overflow: auto; height: 14em' id='dyntext'>" + text + "</div>");
			});
	}
	
	current_region_uri = uri;
}

function endsWith(a, b) {
	if (a == null || b == null) return false;
	if (b.length > a.length) return false;
	return a.substring(a.length-b.length) == b;
}

						
