// Change Log
// MAS 08-02-17 Removed Astoria location

var map;	// holds map definition
var directions;
function load() {
	if (GBrowserIsCompatible()) {
		map = new GMap2(document.getElementById("map"));
		map.addControl(new GSmallMapControl());	// size and pan control
		map.addControl(new GMapTypeControl());	// lets visitor switch between map and satellite mode
		map.addControl(new GScaleControl()); 	// scale control
// MAS 08-02-17		map.setCenter(new GLatLng(40.76533145759273, -73.89644622802734), 12);	// centered betweent the two stores
		map.setCenter(new GLatLng(40.755764, -73.883357), 12);	// centered on Jackson Heights location
		var dir = document.getElementById('directions');
		directions = new GDirections(map, dir); 
		GEvent.addListener(directions, 'error', 
						   function() {
							   var msg = document.getElementById('message');
							   msg.innerHTML = '<p>Error retrieving directions: ' + directions.getStatus().code + '</p>';
						   });
		// Add the two stores to the map
// MAS 08-02-17		getcoords(1, 40.767387, -73.910493, "Papa's Empanadas", '25-51A Steinway Street', 'Astoria', '11103', '718-726-1400');
		getcoords(2, 40.755764, -73.883357, "Papa's Empanadas", '84-17 Northern Boulevard', 'Jackson Heights', '11372', '718-507-0400');
// MAS 08-02-17
		showLocation(2);	// Show pictures for Jackson Heights on page load
	}
}

function getcoords(storeNum, lat, lon, busname, address, city, zipcode, phone) {
	var geocoder = new GClientGeocoder();
	var mgr = new GMarkerManager(map);	// manages the push pins
	var info = formatInfo(lat, lon, busname, address, city, zipcode, phone);
	addMarker(mgr, storeNum, lat, lon, info, 12);
	mgr.refresh();
}

function formatInfo(lat, lon, busname, address, city, zipcode, phone) {	
	var info = busname;
	info += '<br />';
	info += address;
	info += '<br />';
	info += city + ' ' + zipcode + ' ';
	info += '<a href="javascript://" class="mapinfo" onclick="getDirections(' + "'" + city + "', " + lat + ', ' + lon + ');">Get Directions</a>';
	info += '<br />';
	info += phone;
	return info;
} 

function addMarker(mgr, storeNum, lat, lon, info, zoomLevel) {
	var marker = new GMarker(new GLatLng(lat, lon));	// create the marker
	GEvent.addListener(marker, "click", function() {	// add the listener to show the info window
		marker.openInfoWindowHtml(info);
		showLocation(storeNum);
	});
	mgr.addMarker(marker, zoomLevel);	// add the new marker
}

function getDirections(city, lat, lon) {
	var url = 'getdirections.php?city=' + city + '&lat=' + lat + '&lon=' + lon;
	GDownloadUrl(url, function(data, responseCode) {
		clearMessage();
		var tgt = document.getElementById("directions");
		tgt.innerHTML = data;
		window.scrollBy(0, 500);
		document.forms[0].address.focus();
	});
}

function showDirections() {
	clearMessage();
	var dir = document.getElementById('directions');
	var src = document.forms[0];
	var addr = src.address.value;
	var city = src.city.value;
	var lat = src.lat.value;
	var lon = src.lon.value;
	
	dir.innerHTML = '<h2>Driving Directions</h2>';	// clear the destination
	dir.innerHTML += '<h3>' + addr + ' to ' + city + ' Location</h3>';
	dir.innerHTML += '<p>Click on one of the steps below for a detail view of it in the map above.</p>';

	var query = addr + ' to ' + city + ' Location@' + lat + ',' + lon;
	directions.clear();	// clear the last query
	directions.load(query);
}

function clearMessage() {
	var msg = document.getElementById('message');
	msg.innerHTML = '';
}