
//call variables that are general to the file
//if these variables aren't called out here, we can't search with the geocoder
var markers = [];
var geocoder = null;
var map = null;

var loc = window.location.toString();
var localhost = loc.indexOf('localhost') > -1;

if(localhost) var rootUrl = '/framework/maps/';
	else var rootUrl = 'http://www.nanotechproject.org/maps/';

function load() {
    
    if (GBrowserIsCompatible())  {
	//set up the map with all necessary controls
	map = new GMap2(document.getElementById("map"));
	map.addControl(new GLargeMapControl());
	map.addControl(new GMapTypeControl());
	map.enableDoubleClickZoom();
	map.enableContinuousZoom();
	map.enableScrollWheelZoom();
	map.addControl(new GScaleControl());
	//map.setCenter(new GLatLng(48, -118), 3);
	map.setCenter(new GLatLng(38, -95), 4);

	//call the geocoder and marker manager
	geocoder = new GClientGeocoder();
	var mgr = new GMarkerManager(map);
	
	//create the functions we'll need
	
	//we start with the basic marker creation function
	//note that this function does not display the marker, it just returns it as a variable
	//we'll need a map.addOverlay(marker) call after this function if we want a pin on the map
	//this function could be rolled into the pinSelector function below, but in case we need it in
	//later revisions, I haven't done so
	function createMarker(spot, text, mark) {
	    var marker = new GMarker(spot, mark);
	    GEvent.addListener(marker, "click", function() {
		marker.openInfoWindowHtml("<font face=\"Arial\" size=2 style='color: #000'>" + text + "</font>");
	    });
	    return marker;
	}
	
	//for one zoom level, we need pins that have no info window, but carry a number
	function createCountingPin(spot, total) {
		var marker = new GMarker(spot, countPin(total));
		return marker;
	}

	//we also need a function to make the dots, which vary in size
	function dotmaker(spot, total, color, text) {
		var size = (total + 4) 
		var dot = new GIcon();
		dot.image = rootUrl + "pins/bigdot" + color + ".png";
		dot.iconSize = new GSize(size, size);
		dot.iconAnchor = new GPoint(size/2, size/2);
		dot.infoWindowAnchor = new GPoint (0, 0);
		var marker = new GMarker(spot, dot);
		return marker;
	}
	
	//we need to set up the colored pins
	//this is done using an icon class, so first we set up the base icon
	var pin = new GIcon();
	pin.image = rootUrl + "pins/pin_20_3.png";
	pin.shadow = "http://labs.google.com/ridefinder/images/mm_20_shadow.png";
	pin.iconSize = new GSize(12,20);
	pin.shadowSize = new GSize(22,20);
	pin.iconAnchor = new GPoint(6,20);
	pin.infoWindowAnchor = new GPoint (5,1);
	
	//and then we create a function to create icons of the proper color
	//this function will be called within a createMarker call
	function pinSelect(cat) {
	    if (cat == "Academic and Government Research") {
		var color = "purple";
	    } else if (cat == "Materials") {
		var color = "navy";
	    } else if (cat == "Tools and Instruments") {
		var color = "red";
	    } else if (cat == "Medicine and Health") {
		var color = "green";
	    } else if (cat == "Imaging and Microscopy") {
		var color = "yellow";
	    } else if (cat == "Energy and Environmental Applications") {
		var color = "blue";
	    } else if (cat == "Electronics") {
		var color = "orange";
	    } else if (cat == "Organization") {
		var color = "white";
	    } else {
		//this allows a function to call a color directly
		var color = cat;
	    }
	    if (type = "pin") {
		var icon = new GIcon(pin);
		icon.image = rootUrl + "pins/pin_20_" + color + ".png";
	    }
	    return icon;
	}
	
	//we'll also be using numbered pins
	//this is also done through an icon class; here's the base setup
	var nopin = new GIcon();
	nopin.image = rootUrl + "pins/pin_34_1.png"
	nopin.shadow = "http://www.google.com/mapfiles/shadow50.png"
	nopin.iconSize = new GSize (20, 34);
	nopin.shadowSize = new GSize (37, 34);
	nopin.iconAnchor = new GPoint (9, 34);
	nopin.infoWindowAnchor = new GPoint (9, 2);
	nopin.infoShadowAnchor = new GPoint (18, 25);
	
	//and, again, a function to pick the right pin
	function countPin(total) {
	    var icon = new GIcon(pin);
	    icon.image = rootUrl + "pins/pin_20_" + total + ".png";
	    return icon;
	}

			
	//Dot Creation Function
	//this creates the dots without an info window
	function createDot(spot, size) {
		var marker = new GMarker(spot, mark);
		GEvent.addListener(marker, "click", function() {
			map.panTo(spot);
			map.zoomIn();
		});
		return marker;
	}
	
	//now we need functions to turn data files into markers
	//these files contain some superfluous fields in order to make the transition from the geocoder simpler
	//this code is adapted from http://www.econym.demon.co.uk/googlemaps/
	//geocoding is done by http://www.batchgeocode.com

	//add pins for each company when zoomed in
	//note that the file needs to have fields in the following order:
	//company name|website|latitude|longitude|category 
	parser = function(doc) {
	    var markerslarge = [];
	    // split the document into lines
	    lines = doc.split("\n");
	    for (var i=0; i<lines.length; i++) {
		if (lines[i].length>1) {
		    //split the lines into parts
		    parts = lines[i].split("|");
		    var company = (parts[0]);
		    var website = (parts[1]);
		    var lat = parseFloat(parts[2]);
		    var lng = parseFloat(parts[3]);
		    var cat = (parts[4]);
		    var point = new GLatLng(lat,lng);
		    var markerlabel = "<strong>" + company + "</strong><br><a href=\"" + website + "\">" + website + "<\/a><br>" + cat;
		    //create the marker array
		    var markerlarge = createMarker(point, markerlabel, pinSelect(cat));
		    markerslarge.push(markerlarge);
		}
	    }
	    var mgr = new GMarkerManager(map);
	    mgr.addMarkers(markerslarge, 8, 17);
	    mgr.refresh();
	}
	
	//add 3-dig zip-based markers based on bar-delimited files
	//note that the file needs to have fields in the following order:
	//city name|state|city total|latitude|longitude 
	cityparser = function(doc) {
	    var markerssmall = [];
	    // split the document into lines
	    lines = doc.split("\n");
	    for (var i=0; i<lines.length; i++) {
		if (lines[i].length>1) {
		    //split the lines into parts
		    parts = lines[i].split("|");
		    var total = parseFloat(parts[0]);
		    var lat = parseFloat(parts[1]);
		    var lng = parseFloat(parts[2]);
						var color = (parts[3]);
		    var point = new GLatLng(lat,lng);
		    var markersmall = dotmaker(point, total*3+3, color, "");
		    markerssmall.push(markersmall);
		}
	    }
	    var mgr = new GMarkerManager(map);
	    mgr.addMarkers(markerssmall, 2, 7);
	    mgr.refresh();
	}


	// process the file and plot the marker array
	GDownloadUrl(rootUrl + "PointData.txt", parser);
	GDownloadUrl(rootUrl + "CircleData2.txt", cityparser);
	
    }
}

function showAddress(address) {
	geocoder.getLatLng(
		address,
		function(point) {
			if (!point) {
				alert(address + " not found");
			} else {
				map.setCenter(point, 9);
			}
		}
	);
}
	
function showHotLoc(lat, lng, zoom) {
	map.setCenter(new GLatLng(lat, lng), zoom);
}

