


function examplesOnLoad(place, ind, placetypes, period, highlightPlace, latlng, zoom, ptype) {
	var map = new PMap(document.getElementById('mapContainer'));
	// Delays map load
	map.enableLoadDelay();

	var indoptions = {
		'ind':ind,
		'period':period,
		'ptype':ptype
	};

	map.addControl(new PSmallMapControl());
	map.enableDoubleClickZoom();
	if (place == "united states") {
		map.setCenter(new PLatLng(37.8902, -98.9129), 2);
		if (ind)
			showIndicator(map, indoptions);

		// add params to map link
		var mapLink = document.getElementById('mapLink');
		if (mapLink)
			mapLink.href = "/maps?i=" + ind;
	}
	else if (latlng && zoom) {
		map.setCenter(new PLatLng(latlng[0],latlng[1]), zoom);
		if (ind)
			showIndicator(map, indoptions);
	}
	else {
		centerPlace(place, map, indoptions, placetypes, highlightPlace);
	}

	// Turns off map load delay
	if (!ind)
		map.disableLoadDelay();
}

function centerPlace(place, map, indoptions, placetypes, highlightPlace) {
	var ind = indoptions.ind;

	if (!placetypes)
		var placetypes = [PPlaceType.CITY];
	var geocoder = new PClientGeocoder();
	// Find place and zoom to bounds
	if (geocoder && place.replace(/^\s+|\s+$/g,"") != "") {
		geocoder.getPlace(
			place,
			function(places) {
				if (places.length == 0)
					alert("Place \"" + place + "\" not found");
				else {
					// Add marker and open info window
					map.setCenterBounds(places[0].getBounds());
				}
				if (ind)
					showIndicator(map, indoptions);

				// add params to map link
				var mapLink = document.getElementById('mapLink');
				if (mapLink)
					mapLink.href = "/maps?p=" + places[0].id + "&i=" + ind;

				if (highlightPlace)
					examplesAddPolygons(map, places[0].getVertices()[0]);
			}, 10, null, placetypes
		);
	}
}

function showIndicator(map, indoptions) {
	var ind = indoptions.ind;
	var period = indoptions.period;
	var ptype = indoptions.ptype;

	if (ptype) {
		PPlaceTypeConfig.PLEGEND.zooms[map.getZoom()] = ptype;
	}

	if (ind == 9627156)
		PColorRamp.DEFAULT = new PColorRamp('default', [new PColor('5D4770'),new PColor('E6CCE0'),new PColor('FAFAB6')]);
	else
		PColorRamp.DEFAULT = new PColorRamp('default', [new PColor("FFE6F4"), new PColor("E6CCE0"), new PColor("CCB1CC"), new PColor("B29AB8"), new PColor("9B839B"), new PColor("866F94"), new PColor("725B82"), new PColor("5D4770")]);

	PIndicatorLoader.load([ind], function(inds) {
		if (period)
			inds[0].setPeriod(period);
		map.setIndicator(inds[0]);
		var title = document.getElementById('mapTitle');
		title.innerHTML = inds[0].getFullLabel();
	});
	map.getLegend().setNumberOfBreaks(5);
	map.getLegend().setColorRamp(PColorRamp.DEFAULT);
	var widget = new PTRFOnMapWidget(map, document.getElementById("onmaplegend"), P_WIDGET_INDICATOR_ID);

	// Need to add a zoomend event so double click zooms refresh the widget
	PEvent.addListener(map, 'zoomend', function() {
		map.refreshWidget(widget);
	});

	// show map after indicator is on
	map.disableLoadDelay();
}

function examplesAddPolygons(map, vertices) {
	var poly = new PPolyline(vertices);
	map.addOverlay(poly);
}

