﻿

function initialize() {
	//alert('initialize');
	//var latlng = new GLatLng(37.25, -96.35); //US center
	//var mapTypeControlOptions = { style: google.maps.MapTypeControlStyle.DROPDOWN_MENU };
	//var myOptions = { zoom: 3, center: latlng, mapTypeId: google.maps.MapTypeId.ROADMAP, scrollWheel: false, mapTypeControlOptions: mapTypeControlOptions };
	var latlng = new google.maps.LatLng(37.25, -96.35);
	var myOptions = {
		zoom: 3,
		center: latlng,
		mapTypeId: google.maps.MapTypeId.ROADMAP,
		mapTypeControlOptions: { style: google.maps.MapTypeControlStyle.DROPDOWN_MENU }
	};
	
	window.googlemap = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
	window.infoWindow = new google.maps.InfoWindow();
	window.geocoder = new google.maps.Geocoder();

	if (typeof (addInstructorMarkers) == 'function') addInstructorMarkers();
}

function addInstructorMarkers() {
	//var m = createInstructorMarkersArray();//created when Default.aspx?GetMarkers script is included.
	//var styles = { height:35, width:35, opt_textColor:"white", url : "http://gmaps-utility-library.googlecode.com/svn/trunk/markerclusterer/images/people35.png" };

	var styles = [ {
		url: 'res/people35.png',
		height: 35,
		width: 35,
		opt_anchor: [16, 0],
		opt_textColor: '#ffffff',
		opt_textSize: 10
	}, {
		url: 'res/people45.png',
		height: 45,
		width: 45,
		opt_anchor: [24, 0],
		opt_textColor: '#ffffff',
		opt_textSize: 11
	}, {
		url: 'res/people55.png',
		height: 55,
		width: 55,
		opt_anchor: [32, 0],
		opt_textColor: '#ffffff',
		opt_textSize: 12
}];
	
	//window.markerClusterer = new MarkerClusterer(window.googlemap, m, styles);

	//var mcOptions = { maxZoom: 8, gridSize:30 };//, styles:styles };
//window.markerClusterer = new MarkerClusterer(window.googlemap, m, mcOptions);

	var mgr = new MarkerManager(window.googlemap);
	google.maps.event.addListener(mgr, 'loaded', function() {
		var markers = createStateMarkers();   //contained in MapMarkers.ashx which renders JS
		mgr.addMarkers(markers, 2, 8); //min zoom 2, max zoom 5
		markers = createCityMarkers();  //contained in MapMarkers.ashx which renders JS
		mgr.addMarkers(markers, 6);
		mgr.refresh();
	});

}

function addMarker(strTitle, nLat, nLng) {
	var markerOptions = { map: window.googlemap, title: strTitle, position: new google.maps.LatLng(nLat, nLng) };
	var marker = new google.maps.Marker(markerOptions);
	return marker;
}
//shorthand add marker function for 
function AM(strTitle, nLat, nLng, id) {
	var markerOptions = { map: window.googlemap, title: strTitle, position: new google.maps.LatLng(nLat, nLng), clickable: true };
	var marker = new google.maps.Marker(markerOptions);
	marker.instructorId = (id == null ? "" : id);

	GEvent.addListener(marker, 'click', function(e) {
		if (infoWindow == null) infoWindow = new google.maps.InfoWindow({ content: 'test html' });
		infoWindow.setContent('this is test html');
		infoWindow.open(googlemap, googlemap.getCenter());
	});
}

//shorthand for building a STATE marker
function I_SM(strTitle, nLat, nLng, dataKey) {
	//var stateMarkerIcon = new google.maps.MarkerImage('res/people35.png');
	var markerOptions = { title: strTitle, position: new google.maps.LatLng(nLat, nLng), clickable: true };
	var marker = new google.maps.Marker(markerOptions);
	marker.dataKey = dataKey;

	google.maps.event.addListener(marker, 'click', stateMarker_click);

	return marker;
}
function stateMarker_click(event) {
	var marker = this; //wierd to me, but marker is 'this'
	window.googlemap.setZoom(6);  //zoom to state level
	window.googlemap.panTo(marker.getPosition());
	$("#F1_txtLocation").val(marker.dataKey);
	$("#F1_btnGo").click();
}

/// Shorthand function for building an instructor marker
function I_NM(strTitle, nLat, nLng, dataKey) {
	var markerOptions = { title: strTitle, position: new google.maps.LatLng(nLat, nLng), clickable: true };
	var marker = new google.maps.Marker(markerOptions);
	marker.dataKey = dataKey;

	google.maps.event.addListener(marker, 'click', instructorMarker_click);

	return marker;
}
function instructorMarker_click(event) {
	var marker = this; //wierd to me, but marker is 'this'
	window.googlemap.panTo(marker.getPosition());
	$("#F1_txtLocation").val(marker.dataKey);
	$("#F1_btnGo").click();
}

function goToState(stateAbbr) {
	window.googlemap.clearOverlays();


}

function addAddressToMap(response) {
	window.googlemap.clearOverlays();
	if (!response || response.Status.code != 200) {
		alert("\"" + address + "\" not found");
	} else {
		place = response.Placemark[0];
		point = new GLatLng(place.Point.coordinates[1],
                        place.Point.coordinates[0]);
		marker = new GMarker(point);
		map.addOverlay(marker);
		marker.openInfoWindowHtml(place.address + '<br>' +
      '<b>Country code:</b> ' + place.AddressDetails.Country.CountryNameCode);
	}
}
function setMapViewport(response) {
	if (!response || response.Status.code != 200) {
		return;
	} else {

	}
}
function findInstructors(locText, dontCenterMap) {
	try {
		if (dontCenterMap != true) dontCenterMap = false;
		
		//if (infoWindow != null) infoWindow.hide();
		if (window.googlemap.getCenter() == null) alert("findInstructors(\"" + locText + "\"");
		var txtLocation = $("#F1_txtLocation");
		if (locText != null) { txtLocation.val(locText); }
		var address = txtLocation.val();
		if (window.geocoder && !dontCenterMap) {
			//alert('locText=' + locText);
			window.geocoder.geocode({ 'address': address }, function(results, status) {
				if (status == google.maps.GeocoderStatus.OK) {
					switch (results[0].types[0]) {
						case "administrative_area_level_1": window.googlemap.setZoom(6); break; //state zoom
						case "locality": window.googlemap.setZoom(11); break; //city zoom
						case "postal_code": window.googlemap.setZoom(13); break;
					}

					window.googlemap.panTo(results[0].geometry.location);
				} else {
					alert("Geocode was not successful for the following reason: " + status);
				}
			});
		}
	}
	catch(err) { alert('exception occurred in findInstructors: ' + err); return false; }
}



