//<![CDATA[
		   
// Global variable		   
var IPRT = {};

// Map
IPRT.MAP = new GMap2(document.getElementById('map'));
IPRT.INIT = {LAT:41.9, LNG:-93.4, ZOOM: 6};	

// Marker
IPRT.ICON = new GIcon();
IPRT.ICON.image = "mm_20_yellow.png";
IPRT.ICON.shadow = "mm_20_shadow.png";
IPRT.ICON.iconSize = new GSize(12, 20);
IPRT.ICON.shadowSize = new GSize(22, 20);
IPRT.ICON.iconAnchor = new GPoint(6, 20);
IPRT.ICON.infoWindowAnchor = new GPoint(5, 1);

// ------------------------------------------------- functions ---------------------------------------------//

IPRT.loadMap = function () {
	// Add pan and zoom control
	IPRT.MAP.addControl(new GSmallZoomControl());	

	// Add control for map/satellite/hydrid
	IPRT.MAP.addControl(new GMapTypeControl());
		
	// Center and zoom map for Iowa
	IPRT.zoomToIowa();
	
	// Load data
	IPRT.processData();

};

// Zoom to Iowa, approximately
IPRT.zoomToIowa = function () {
	IPRT.MAP.closeInfoWindow();
	IPRT.MAP.setCenter(new GLatLng(IPRT.INIT.LAT, IPRT.INIT.LNG), IPRT.INIT.ZOOM);
}

// Process data
IPRT.processData = function () {
	var i;

	// Process data
	for (i = 0; i < IPRT.CITIES.length; i++) {

		// Load city selection dropdown
		document.forms.cityform.citysel.options[i+1] = new Option(IPRT.CITIES[i].NAME, i+1);

		// Create infowindow HTML
		IPRT.CITIES[i].INFOWINDOWHTML = '<h3>' + IPRT.CITIES[i].NAME + '</h3>' +
				'<p class="small">' + IPRT.CITIES[i].ORGANIZATIONS + '</p>';
		
		// Put markers on map
		IPRT.CITIES[i].LATLNG = new GLatLng(IPRT.CITIES[i].LATITUDE, IPRT.CITIES[i].LONGITUDE);
		IPRT.cityMarker(i);
	}
};

// Display city markers
IPRT.cityMarker = function (inCityIndex) {
	var maxWindowWidth = 200;

	// Create marker
	IPRT.CITIES[inCityIndex].MARKER = new GMarker(IPRT.CITIES[inCityIndex].LATLNG, IPRT.ICON);
	IPRT.MAP.addOverlay(IPRT.CITIES[inCityIndex].MARKER);
	
	// Add listener for marker
	if (IPRT.CITIES[inCityIndex].ORGANIZATIONS.length > 200) { maxWindowWidth = 300; }
		
	GEvent.addListener(IPRT.CITIES[inCityIndex].MARKER, "click", function() {
		IPRT.CITIES[inCityIndex].MARKER.openInfoWindowHtml(IPRT.CITIES[inCityIndex].INFOWINDOWHTML, {maxWidth:maxWindowWidth} );
	});
	
};

// Open info window of selected city from dropdown
IPRT.goToCity = function () {
	var cityIndex = document.cityform.citysel.value;
	if (cityIndex) {IPRT.showInfoWindow(cityIndex - 1);}
};

// Open an info window for a particular point
IPRT.showInfoWindow = function (inPointIndex)  {GEvent.trigger(IPRT.CITIES[inPointIndex].MARKER, "click"); };
 
//]]>