var map = null;
var geocoder = null;
var gtarget = null;
var gloc = {};
var gstations = new Array();
var gregion = 'conus';
var placemark;
var loctval;

var MapRegion = {

	getBounds: function (name) {
		switch (name) {
			case 'conus':
				return {'TL': [], 'BR': [], 'C': [-95,37], 'zoom': 3};
			break;

			case 'northeast':
				return {'TL': [-109.6427,45.2161], 'BR': [-85.6376,31.123], 'C': [-97.6402,38.1696], 'zoom': 5};
			break;

			case 'eastcentral':
				return {'TL': [-94.5231,44.5066], 'BR': [-69.2643,30.2813], 'C': [-81.8937,37.39395], 'zoom': 5};
			break;

			case 'southeast':
				return {'TL': [-99.2629,39.0467], 'BR': [-74.293,24.0764], 'C': [-86.77795,31.56155], 'zoom': 5};
			break;

			case 'northcentral':
				return {'TL': [-109.4216,50.4211], 'BR': [-82.9204,37.8427], 'C': [-96.171,44.1319], 'zoom': 5};
			break;

			case 'central':
				return {'TL': [-109.6427,45.2161], 'BR': [-85.6376,31.123], 'C': [-97.64015,38.16955], 'zoom': 5};
			break;

			case 'southcentral':
				return {'TL': [-112.2742,39.5438], 'BR': [-87.3627,24.1934], 'C': [-99.81845,31.8686], 'zoom': 5};
			break;

			case 'northwest':
				return {'TL': [-126.3381,50.9177], 'BR': [-103.3564,38.1403], 'C': [-114.84725,44.529], 'zoom': 5};
			break;

			case 'westcentral':
				return {'TL': [-126.3966,46.8827], 'BR': [-101.0173,32.6726], 'C': [-113.70695,39.77765], 'zoom': 5};
			break;

			case 'southwest':
				return {'TL': [-125.841,42.877], 'BR': [-99.8477,26.9711], 'C': [-112.84434999999999,34.92405], 'zoom': 5};
			break;

			case 'usak':
				return {'TL': [], 'BR': [], 'C': []};
			break;

			case 'ushi':
				return {'TL': [], 'BR': [], 'C': []};
			break;
		}
	}

};

function setMapTargetDiv (div) {
	gtarget = div;
}

function setMapStations (stations) {
	gstations = stations;
}

function setMapLoc (loclat, loclon, loczoom, locnoicon) {
	gloc = {lat: loclat, lon: loclon, zoom: loczoom, noicon: locnoicon};
	initStationMap();
}

function setMapRegion (region) {
	gregion = region;
}

function initStationMap () {
	tdiv = gtarget;
	if (GBrowserIsCompatible()) {
		map = new GMap2($(tdiv));
		map.clearOverlays();
		point = new GLatLng(gloc.lat, gloc.lon);
		map.setCenter(point, gloc.zoom);

		if (!gloc.noicon) {
			//marker = new GMarker(point);
			stnicon = plotStationMarker();
			marker = new GMarker(point, {title: '', icon: stnicon});
			map.addOverlay(marker);
		}
	}
    else {
      alert("Sorry, the Google Maps API is not compatible with this browser");
    }
}

function getStationAddress (address, tdiv, tval, tmodal) {
	if (GBrowserIsCompatible()) {
		map = new GMap2($(tdiv));

		geocoder = new GClientGeocoder();
		geocoder.getLocations(address, function(response) {
			map.clearOverlays();
			if (!response || response.Status.code != 200) {
				alert("Sorry, we were unable to geocode that address");
			}
			else {
				place = response.Placemark[0];
				point = new GLatLng(place.Point.coordinates[1], place.Point.coordinates[0]);
				map.setCenter(point, 11);
				marker = new GMarker(point);
				map.addOverlay(marker);
				//marker.openInfoWindowHtml(place.address);
				placemark = place;
				loctval = tval;

				if (tmodal) setLocModalFields(tmodal);
			}
		});
	}
    else {
      alert("Sorry, the Google Maps API is not compatible with this browser");
    }
}

function showStations () {
	tdiv = gtarget;
	stations = gstations;
	region = gregion;

	map = new GMap2($(tdiv));
	map.clearOverlays();

	map.addControl(new GScaleControl(),new GControlPosition(G_ANCHOR_BOTTOM_RIGHT,new GSize(8,20)));
	map.addControl(new GSmallMapControl ());
	map.addControl(new GMapTypeControl())

	r = MapRegion.getBounds(region);
	point = new GLatLng(r.C[1], r.C[0]);
	map.setCenter(point, r.zoom);
	for (i=0; i < stations.length; i++) {
		stnpos = new GLatLng(stations[i].lat, stations[i].lon);
		//marker = new GMarker(stnpos);
		stnicon = plotStationMarker(stations[i].data);
		marker = new GMarker(stnpos, {title: '', icon: stnicon});
		marker.stnid = stations[i].id;
		map.addOverlay(marker);
		GEvent.addListener(marker, "click", function() { top.location.href='stationwx.php?id=' + this.stnid; });
	}
}

function plotStationMarker (data) {
	var stnicon = new GIcon();
	if (data) {
		stnicon.image = 'http://www.pwsweather.com/' + 'includes/libs/GMapIcon.php?stn=' + data.id + '&w=20&h=20&t=' + data.tempF + '&ws=' + data.windSpd + '&wd=' + data.windDir;
		stnicon.shadow = null;
		stnicon.iconSize = new GSize(20, 20);
		stnicon.shadowSize = null;
		stnicon.iconAnchor = new GPoint(10, 10);
		stnicon.infoWindowAnchor = new GPoint(5, 5);
	}
	else {
		stnicon.image = 'http://www.pwsweather.com/' + 'images/stnicon.png';
		stnicon.iconSize = new GSize(43, 73);
		stnicon.iconAnchor = new GPoint(22, 73);
		stnicon.shadow = 'http://www.pwsweather.com/' + 'images/stnicon_shadow.png';
		stnicon.shadowSize = new GSize(66, 73);
		stnicon.infoWindowAnchor = new GPoint(5, 5);
	}

	return stnicon;
}

function getTempMarker (temp) {
	temp = Math.floor(temp/10);
	temp *= 10;
	return './images/wxstn/tempicons/' + temp + '.png';
}

