var map = null;
var map_center = null;
var geocoder = null;
var home = null;

var defAddress = 'Enter your zip code, city or full address';

var plants = [
	["Alvin W Vogtle Georgia", new GLatLng(33.143005, -81.76574)], ["Arkansas Nuclear One Arkansas", new GLatLng(35.310278, -93.23138)], ["Beaver Valley Pennsylvania", new GLatLng(40.623322, -80.43056)],
	["Braidwood Illinois", new GLatLng(41.243611, -88.22916)], ["Browns Ferry Alabama", new GLatLng(34.703889, -87.11861)], ["Brunswick North Carolina", new GLatLng(33.958225, -78.01013)],
	["Byron Illinois", new GLatLng(42.074167, -89.28194)], ["Callaway Missouri", new GLatLng(38.761667, -91.78)], ["Calvert Cliffs Maryland", new GLatLng(38.431892, -76.44235)],
	["Catawba South Carolina", new GLatLng(35.051667, -81.07)], ["Clinton Illinois", new GLatLng(40.172222, -88.835)], ["Columbia Washington formerly WNP2", new GLatLng(46.471111, -119.3338)],
	["Comanche Peak Texas", new GLatLng(32.298333, -97.785)], ["Cooper Nebraska", new GLatLng(40.361944, -95.64138)], ["Crystal River 3 Florida", new GLatLng(28.9575, -82.69833)],
	["DavisBesse Ohio", new GLatLng(41.596667, -83.08638)], ["Diablo Canyon California", new GLatLng(35.210861, -120.8561)], ["Donald C Cook Michigan", new GLatLng(41.975391, -86.56591)],
	["Dresden Illinois", new GLatLng(41.389722, -88.26805)], ["Duane Arnold Iowa", new GLatLng(42.100556, -91.77722)], ["Enrico Fermi Michigan", new GLatLng(41.962778, -83.2575)],
	["Farley Joseph M Farley Alabama", new GLatLng(31.223056, -85.11166)], ["Fort Calhoun Nebraska", new GLatLng(41.520278, -96.07722)], ["Ginna New York", new GLatLng(43.277893, -77.3101)],
	["Grand Gulf Nuclear Generating Station Mississippi", new GLatLng(32.006667, -91.04833)], ["H B Robinson South Carolina", new GLatLng(34.402778, -80.15833)], ["Hatch Edwin I Hatch Georgia", new GLatLng(31.934167, -82.34388)],
	["Hope Creek & Salem New Jersey", new GLatLng(39.467778, -75.53805)], ["Indian Point New York", new GLatLng(41.269745, -73.95233)], ["Kewaunee Wisconsin", new GLatLng(44.342222, -87.53611)],
	["LaSalle County Illinois", new GLatLng(41.245556, -88.66916)], ["Limerick Pennsylvania", new GLatLng(40.226655, -75.58731)], ["McGuire North Carolina", new GLatLng(35.4325, -80.94833)],
	["Millstone Connecticut", new GLatLng(41.311984, -72.16854)], ["Monticello Minnesota", new GLatLng(45.333611, -93.84916)], ["Nine Mile Point/Fitzpatrick New York", new GLatLng(43.520833, -76.40694)],
	["North Anna Virginia", new GLatLng(38.060556, -77.78944)], ["Oconee South Carolina", new GLatLng(34.793889, -82.89805)], ["Oyster Creek New Jersey", new GLatLng(39.814732, -74.20507)],
	["Palisades Michigan", new GLatLng(42.322778, -86.31444)], ["Palo Verde Arizona", new GLatLng(33.389241, -112.865)], ["Peach Bottom Pennsylvania", new GLatLng(39.758333, -76.26805)],
	["Perry Ohio", new GLatLng(41.800833, -81.14333)], ["Pilgrim Massachusetts", new GLatLng(41.945, -70.57833)], ["Point Beach Wisconsin", new GLatLng(44.281111, -87.53666)],
	["Prairie Island Minnesota", new GLatLng(44.621667, -92.63305)], ["Quad Cities Illinois", new GLatLng(41.726389, -90.31)], ["River Bend Louisiana", new GLatLng(30.756667, -91.33333)],
	["San Onofre California", new GLatLng(33.368889, -117.555)], ["Seabrook New Hampshire", new GLatLng(42.898889, -70.85083)], ["Sequoyah Tennessee", new GLatLng(35.226389, -85.09166)],
	["Shearon Harris North Carolina", new GLatLng(35.633333, -78.955)], ["South Texas Texas", new GLatLng(28.7955, -96.049)], ["St Lucie Florida", new GLatLng(27.348611, -80.24638)],
	["Surry Virginia", new GLatLng(37.165556, -76.69777)], ["Susquehanna Pennsylvania", new GLatLng(41.088867, -76.14898)], ["Three Mile Island Pennsylvania", new GLatLng(40.153889, -76.72472)],
	["Turkey Point Florida", new GLatLng(25.434167, -80.33055)], ["Vermont Yankee Vermont", new GLatLng(42.778772, -72.5131)], ["Virgil C Summer South Carolina", new GLatLng(34.2985, -81.31485)],
	["Waterford Louisiana", new GLatLng(29.995, -90.47138)], ["Watts Bar Tennessee", new GLatLng(35.602778, -84.78944)], ["Wolf Creek Kansas", new GLatLng(38.238889, -95.68888)]
];

function makeIcon(file, w, h){
	var ic = new GIcon(G_DEFAULT_ICON);
	var hs = new GPoint(w/2, h/2);
	ic.image = file;
	ic.iconSize = new GSize(w, h);
	ic.shadow = ic.shadowSize = null;
	ic.iconAnchor = hs;
	ic.infoWindowAnchor = hs;
	return ic;
}

function initialize() {
	document.forms[0].address.value = defAddress;
	if (GBrowserIsCompatible()) {
		map = new GMap2(document.getElementById("map_canvas"));
		map_center = new GLatLng(39.828175, -98.579500);
		geocoder = new GClientGeocoder();

		map.setMapType(G_HYBRID_MAP);
		map.setCenter(map_center, 5);
		geocoder.setBaseCountryCode('US');

		var red_icon = makeIcon('/maps/red.png', 32, 32);
		var yel_icon = makeIcon('/maps/yel.png', 32, 32);
		var hom_icon = makeIcon('/maps/home.png', 32, 32);
		home = new GMarker(map_center, {icon: hom_icon, title: 'You are here', hide: true});
		map.addOverlay(home);
		for (var i = 0; i < plants.length; i++) {
			var p = plants[i];
			map.addOverlay(p[2] = new GMarker(p[1], { icon: yel_icon, title: p[0] }));
			map.addOverlay(p[3] = new GMarker(p[1], { icon: red_icon, title: p[0], hide: true }));
			map.addOverlay(p[4] = new GPolyline([p[1]], '#ff3333', 2, 0.6));
			p[4].hide();
		}
	}
}

function plantHighlight(plant, b, dist){
	if(b){
		plant[3].show();
		var txt = '<div id="plant_info"><h1>' + plant[0] + '</h1><br/>' + dist + ' miles from you</div>';
		plant[3].bindInfoWindowHtml(txt);
		if(plant[3].GD)
			plant[3].GD.title = plant[0] + '. ' + dist + ' miles.';
		plant[2].hide();
		plant[4].insertVertex(1, home.getLatLng());
		plant[4].show();
	}else{
		plant[2].show();
		plant[3].hide();
		plant[3].bindInfoWindowHtml(null);
		if(plant[3].GD)
			plant[3].GD.title = plant[0];
		plant[4].hide();
		plant[4].deleteVertex(1);
	}
}

function setHome(response){
	home.hide();
	if(!response || response.Status.code != 200){
		map.setCenter(map_center, 4);
		for (var i = 0; i < plants.length; i++) {
			plantHighlight(plants[i], false);
		}
		map.openInfoWindowHtml(map_center, 'Sorry, we could not find your address.');
	}else{
		var place = response.Placemark[0];
		var point = new GLatLng(place.Point.coordinates[1], place.Point.coordinates[0]);
		home.setPoint(point);
		home.show();
		var b = new GLatLngBounds(point, point);
		var pl = [];
		for (var i = 0; i < plants.length; i++) {
			var p = plants[i];
			var dist = Math.ceil(p[1].distanceFrom(point) / 1609);
			var hig = ( dist <= 200 );
			plantHighlight(p, hig, dist);
			if(hig){
				b.extend(p[1]);
				pl.push([dist, p]);
			}
		}

		var str = '<div id="home_info"><h1>' + place.address + '</h1>';
		if(pl.length > 0){
			str += 'has ' + pl.length + ' nuclear plants in 200 miles zone. It is in'
			pl.sort(function(a,b){return a[0]-b[0]});
			str += '<ul>';
			for(var i = 0; i < pl.length; ++i){
				str += '<li><div>'+Math.floor(pl[i][0])+'</div> miles from '+pl[i][1][0]+'</li>';
			}
			str += '</ul>';
		}else{
			str += 'Pretty safe place. No nuclear plants in 200 miles zone.';
		}
		str += '</div>';

		map.setCenter(b.getCenter(), 7);
		home.bindInfoWindowHtml(str);
		home.openInfoWindowHtml(str);
	}
}

function showAddress(address) {
	if (geocoder) {
		geocoder.getLocations( address, setHome );
	}
	return false;
}

function clearInput(input){
	if(input.value == defAddress)
		input.value = "";
}
