var map;
var wms;
var ucounties;
var umcds;
var mad_labels;
var vectors;
var thethematic;
var minx = 0;
var miny = 0;
var maxx = 0;
var maxy = 0;
var createmyimage = 'f';

var theserver = 'http://aplmapserver.apl.wisc.edu/washmap2/'
//var theserver = 'http://localhost/walk/'
//var thecgi = 'http://www.washingtoncountyindicatorproject.org/cgi/'
var thecgi = 'http://www.washcoip.org/cgi/'
var id = '';
function SaveMap(){
var theextentstemp = map.getExtent().toBBOX().replace(/,/g, ', ');
var theextents = theextentstemp.split(",");
minx = theextents[0];
miny = theextents[1];
maxx = theextents[2];
maxy = theextents[3];
createmyimage = 't';
CreatetheMap()

}
function CreatetheMap(){
var thefield = '';
var thefname = '';
var thegeographies; 
if (document.getElementById("dd00").selectedIndex != 0){
	thefield = document.getElementById("dd00").options[document.getElementById("dd00").selectedIndex].value
	thefname = document.getElementById("dd00").options[document.getElementById("dd00").selectedIndex].text
}
else if (document.getElementById("dd90").selectedIndex != 0){
	thefield = document.getElementById("dd90").options[document.getElementById("dd90").selectedIndex].value
	thefname = document.getElementById("dd90").options[document.getElementById("dd90").selectedIndex].text
}

else if (document.getElementById("dd80").selectedIndex != 0){
	thefield = document.getElementById("dd80").options[document.getElementById("dd80").selectedIndex].value
	thefname = document.getElementById("dd80").options[document.getElementById("dd80").selectedIndex].text
}

else if (document.getElementById("ddOther").selectedIndex != 0){
	thefield = document.getElementById("ddOther").options[document.getElementById("ddOther").selectedIndex].value
	thefname = document.getElementById("ddOther").options[document.getElementById("ddOther").selectedIndex].text
}
else {}

thegeographies = document.getElementById("thegeoselect").options[document.getElementById("thegeoselect").selectedIndex].value

if (thefield == '') {
	window.alert("Please select a variable to map.")
}
else {
	var currentTime = new Date()
	id = (parseInt(1+currentTime.getMonth())) + "_" + currentTime.getDate() + "_" + currentTime.getFullYear() + "_" + currentTime.getHours() + "_" + currentTime.getMinutes() + "_" + currentTime.getSeconds()
	if (thethematic){map.removeLayer(thethematic)};
		var thelayernum = 'mylayer2';
		if (thegeographies == 'wash_mcds'){thelayernum = 'mylayer4'}
        thethematic = new OpenLayers.Layer.WMS(document.getElementById("thegeoselect").options[document.getElementById("thegeoselect").selectedIndex].text + "/" + thefname , theserver + "wms.ashx",
            {layers: eval("'" + thelayernum + "'"),
			tilesOrigin : "0,0",
			transparent: "true",
            format: "image/png", request: "GetMap", CRS: new OpenLayers.Projection("EPSG:900913"),
			P_LAYER: eval("'" + thegeographies + "'"),
			P_NAME: eval("'" + id + "'"),
			P_PRINT: eval("'" + createmyimage + "'"),
			XMIN: eval("'" + minx + "'"),
			YMIN: eval("'" + miny + "'"),
			XMAX: eval("'" + maxx + "'"),
			YMAX: eval("'" + maxy + "'"),
			P_FIELD: eval("'" + thefield + "'"),
            version: "1.3.0"}, {isBaseLayer: false, opacity: .6, 'displayOutsideMaxExtent': true, 'visibility': true});
			map.addLayer(thethematic);
	if (createmyimage == 't'){
		downloadImage(id)
	}
	else{
		StartAjaxRequest("updateLegend")
		vargeolist = ''
		ajaxRequest.open("GET", thecgi + "wash_legend.py?thegeo=" + thegeographies + "&thefield=" + thefield, true);
		ajaxRequest.send(null);
	}
}


}
function downloadImage(id){
	createmyimage = 'f';
	setTimeout('OpenNewpageSmall("download.php?thefile="  + id + ".gif", "Download")', 6000)
}
function updateLegend(){
if(ajaxRequest.readyState == 4){
	document.getElementById("mapkey").innerHTML = ajaxRequest.responseText;
   }
}
function Go(url,anchor){
 //var theid = GetParam('id')
 //window.location=url + "?id=" + theid + anchor;
 window.location=url 
}
function clearDrops(thename){

if (thename == "dd00"){
	document.getElementById("dd90").selectedIndex = 0
	document.getElementById("dd80").selectedIndex = 0
	document.getElementById("ddOther").selectedIndex = 0
}
else if (thename == "dd90"){
	document.getElementById("dd00").selectedIndex = 0
	document.getElementById("dd80").selectedIndex = 0
	document.getElementById("ddOther").selectedIndex = 0
}
else if (thename == "dd80"){
	document.getElementById("dd90").selectedIndex = 0
	document.getElementById("dd00").selectedIndex = 0
	document.getElementById("ddOther").selectedIndex = 0
}
else{
	document.getElementById("dd90").selectedIndex = 0
	document.getElementById("dd80").selectedIndex = 0
	document.getElementById("dd00").selectedIndex = 0
}


}
        function init(){
            var options = {
                // the "community" epsg code for spherical mercator
                projection: "EPSG:900913",
                // map horizontal units are meters
                units: "m",
                // this resolution displays the globe in one 256x256 pixel tile
                maxResolution: 156543.0339,
                // these are the bounds of the globe in sperical mercator
                maxExtent: new OpenLayers.Bounds(-20037508, -20037508,
                                                 20037508, 20037508),		
		    controls: []
            };
            map = new OpenLayers.Map('map', options);
            createLayers()//
	      map.addLayers([gnormal, gsat]);
            var usBounds = new OpenLayers.Bounds(
                mapExtent[0], mapExtent[1], mapExtent[2], mapExtent[3]);         
            zb = new OpenLayers.Control.ZoomBox(
                {title:"Zoom box: Selecting it you can zoom on an area by clicking and dragging."});
		var panner = new OpenLayers.Control.Navigation({title:'You can use the default mouse configuration'})
            var panel = new OpenLayers.Control.Panel({defaultControl: panner});
            panel.addControls([
                panner, 
                zb,
		    new OpenLayers.Control.ZoomToMaxExtent({title:"Zoom to the max extent"}),
		new OpenLayers.Control.PanZoomBar()
		]);

            ucounties = new OpenLayers.Layer.WMS("Counties" , theserver + "wms.ashx",
            {layers: "mylayer1,countylabels",
		tilesOrigin : "0,0",
		transparent: "true",
            format: "image/png", request: "GetMap", CRS: new OpenLayers.Projection("EPSG:900913"),
		P_LAYER: "wash_counties",
		P_FIELD: "",
            version: "1.3.0"}, {isBaseLayer: false, opacity: .6, 'displayOutsideMaxExtent': true, 'visibility': true});
        umcds = new OpenLayers.Layer.WMS("Municipalities" , theserver + "wms.ashx",
            {layers: "mylayer3",
		tilesOrigin : "0,0",
		transparent: "true",
            format: "image/png", request: "GetMap", CRS: new OpenLayers.Projection("EPSG:900913"),
		P_LAYER: "wash_mcds",
		P_FIELD: "",
            version: "1.3.0"}, {isBaseLayer: false, opacity: .6, 'displayOutsideMaxExtent': true, 'visibility': false});

            map.addControl(panel);
			map.addLayers([umcds, ucounties]);
            var myLayerswitcher = new OpenLayers.Control.LayerSwitcher({'ascending':false});
	      map.addControl(myLayerswitcher)
		myLayerswitcher.maximizeControl();
		map.addControl(new OpenLayers.Control.MousePosition())
		map.zoomToExtent(usBounds);
	StartAjaxRequest("updateDataDrops")
	vargeolist = ''
	ajaxRequest.open("GET", thecgi + "wash_drops.py", true);
	ajaxRequest.send(null); 
        }
		        function createLayers(){
 		// create Google layer
            gnormal = new OpenLayers.Layer.Google(
                "Google Map",
                {type: G_NORMAL_MAP, 'sphericalMercator': true}
            );
		            // create Google layer
            gsat = new OpenLayers.Layer.Google(
                "Google Satellite Map",
                {type: G_SATELLITE_MAP, 'sphericalMercator': true}
            );
	}
function updateDataDrops(){
if(ajaxRequest.readyState == 4){
	datadrop = document.getElementById("myDrops")
	datadrop.innerHTML = ajaxRequest.responseText;
   }

	//document.getElementById("error").innerHTML = ajaxRequest.responseText
}