// JavaScript Document

// global vars
var fltStartLat = 40.748024;
var fltStartLon = -73.983943;
var intStartZoom = 16;
var strMapType = G_NORMAL_MAP;

var objPoint = null;
var isDisplay = false;
var objMap = null;
var objDir = null;
var objMarker = null;
var objToolTip = null;
var objHandle = null;

// image preloading
var wmImg = new Array();
function wmPreload(strPath) {
	var objImg = new Image();
	objImg.src = strPath;
	wmImg.push(objImg);
}

// create custom icon
var icoIcon = new GIcon();
icoIcon.image = '/images/icons/maps/067_norm.png';
icoIcon.shadow = '/images/icons/maps/shadow40.png';
icoIcon.iconSize = new GSize(19, 23);
icoIcon.shadowSize = new GSize(30, 23);
icoIcon.iconAnchor = new GPoint(9, 11);
icoIcon.infoWindowAnchor = new GPoint(9, 11);
wmPreload('/images/icons/maps/067_norm.png');

var icoIconReckson = new GIcon();
icoIconReckson.image = '/images/icons/maps/reckson.png';
icoIconReckson.shadow = '/images/icons/maps/shadow40.png';
icoIconReckson.iconSize = new GSize(19, 23);
icoIconReckson.shadowSize = new GSize(30, 23);
icoIconReckson.iconAnchor = new GPoint(9, 11);
icoIconReckson.infoWindowAnchor = new GPoint(9, 11);
wmPreload('/images/icons/maps/reckson.png');

// geocoder
function wmGeocode(strAddress, strCity, strState) {
	var objGeocoder = new GClientGeocoder();
	if (!strState) {
		objPoint = new GLatLng(strAddress, strCity);
		wmCenter();
	} else {
		objGeocoder.getLatLng(strAddress + ', ' + strCity + ', ' + strState,
			function(point) {
				if (point) {
					objPoint = point;
					wmCenter();
				}
			}
		);
	}
}

// add marker to map
function wmAddMarker(point) {
	var objIcon = null;
	if ($('imgReckson')) {
		objIcon = icoIconReckson;
	} else {
		objIcon = icoIcon;
	}
	if (point) {
		objMarker = new GMarker(point, {icon:objIcon} );
		objMarker.setCursor('help');
		objMap.addOverlay(objMarker);
	} else {
		if (isDisplay == false) {
			objMarker = new GMarker(objPoint, {icon:objIcon} );
			objMap.addOverlay(objMarker);
			isDisplay = true;
		}
	}
	GEvent.addListener(objMarker, 'mouseover', function() {
		//objMarker.L[0].style.cursor = 'url("http://www.google.com/intl/it_ALL/mapfiles/openhand.cur"), pointer';
		//objDebug = $('debug');
//		for (keyvar in objMarker) {
//			objDebug.innerHTML = objDebug.innerHTML + keyvar + '&#8212;' + objMarker[keyvar] + '<br />';
//		}
//		alert(objMarker.Zk);
//		objDebug.innerHTML = objDebug.innerHTML + '<br />' + $('mtgt_undefined').style.cursor;
//		$('mtgt_undefined').style.cursor = 'url("http://www.google.com/intl/it_ALL/mapfiles/openhand.cur"), pointer';
	});
	// display tooltip if present
	if (objToolTip) {
		objToolTip.marker = objMarker;
		wmToolTipPosition();
	}
}

// used to render map
function wmCenter(strAddress, strCity, strState) {
	if (!objPoint) { wmGeocode(strAddress, strCity, strState); }
	if (objPoint) {
		objMap.setCenter(objPoint, intStartZoom);
		wmAddMarker();
	}
}

// get directions
function wmDirections(strFrom, strTo) {
	var objDirections = $('map_directions');
	// reset map
	objDirections.innerHTML = '';
	objMap.clearOverlays();
	isDisplay = false;
	wmAddMarker();
	if (objToolTip) {
		GEvent.removeListener(objHandle);
		objToolTip.style.display = 'none';
	}
	// get directions
	objDir = new GDirections(objMap, objDirections);
	// add event handler for errors
	GEvent.addListener(objDir, 'error', wmDirErrors);
	// display directions
	objDir.load('from: ' + strFrom + ' to: ' + strTo);
}

// directions error handler
function wmDirErrors() {
	var objDirections = document.getElementById('map_directions');
	if (objDir.getStatus().code == G_GEO_UNKNOWN_ADDRESS) {
		objDirections.innerHTML = '<p>No corresponding geographic location could be found for one of the specified addresses. This may be due to the fact that the address is relatively new, doesn\'t include a city or state, or it may be incorrect.</p>';
	} else if (objDir.getStatus().code == G_GEO_SERVER_ERROR) {
		objDirections.innerHTML = '<p>A geocoding or directions request could not be successfully processed, yet the exact reason for the failure is not known.</p>';
	} else if (objDir.getStatus().code == G_GEO_MISSING_QUERY) {
		objDirections.innerHTML = '<p>Please enter an address.</p>';
	} else if (objDir.getStatus().code == 604) {
		objDirections.innerHTML = '<p>Please enter an address.</p>';
	}
}

// initialize map and page
function wmLoad() {
	// test for browser compatibility using built-in Google function
	if (GBrowserIsCompatible()) {
		// create map, add controls, and set properties
		objMap = new GMap2($('map'));
		objMap.addControl(new mapTypeControl());
		objMap.addControl(new smallZoomControl());
		objMap.enableContinuousZoom();
		// retrieve address or hardcoded corporate office and geocode
		if ($('txtLat') && $('txtLon')) {
			wmGeocode($('txtLat').value, $('txtLon').value);
		} else {
			var strAddress = document.getElementsByTagName('h1')[1].innerHTML;
			var arrAddress = strAddress.split(',');
			if (arrAddress.length == 3) {
				wmGeocode(arrAddress[0], arrAddress[1], arrAddress[2]);
			} else {
				wmGeocode('420 Lexington Avenue', 'Manhattan', 'NY');
			}
		}
		// retrieve tooltip if present
		if ($('tooltip')) {
			objToolTip = $('tooltip');
			// preload background image
			wmPreload('/images/backgrounds/bg_contact_map.png');
			// move tooltip with map
			objHandle = GEvent.addListener(objMap, 'move', function() {
				wmToolTipPosition();
			});
		}
	} else {
		// let client know their browser is not compatible with Google Maps and,
		// if possible, hide the map div
		alert('Your browser is unable to display Google Maps.');
		if (document.getElementById) {
			objMap = $('map');
			objMap.style.backgroundColor = '#ebebeb';
			objStats.style.backgroundColor = '#fff';
			objStats.style.backgroundImage = 'none';
			objStats.innerHTML = '<p class="response">Cannot display Google Maps</p>';
		}
	}
}

// tooltip
function wmToolTipPosition() {
	if (objToolTip.marker) {
		var objPoint = objMap.getCurrentMapType().getProjection().fromLatLngToPixel(objMap.getBounds().getSouthWest(), objMap.getZoom());
		var objOffset = objMap.getCurrentMapType().getProjection().fromLatLngToPixel(objToolTip.marker.getPoint(), objMap.getZoom());
		var objIcon = objToolTip.marker.getIcon();
		var objAnchor = objIcon.iconAnchor;
		var intWidth = objIcon.iconSize.width;
		var offMap = new GControlPosition(G_ANCHOR_BOTTOM_LEFT, new GSize(objOffset.x - objPoint.x - objAnchor.x + intWidth, -objOffset.y + objPoint.y + objAnchor.y)).offset;
		if (offMap.width <= 15 || offMap.width >= 680 || offMap.height <= 17 || offMap.height >= 420) {
			objToolTip.style.display = 'none';
		} else {
			if (objToolTip.style.display == 'none' && objToolTip.marker) {
				objToolTip.style.display = 'block';
			}
		}
		var objPos = new GControlPosition(G_ANCHOR_BOTTOM_LEFT, new GSize(objOffset.x - objPoint.x - objAnchor.x + intWidth - 84, -objOffset.y + objPoint.y + objAnchor.y + 51)); 
		objPos.apply(objToolTip);
	}
}

// listeners
// ----------------------------------------------
Event.observe(window, 'load', wmLoad, false);
Event.observe(window, 'unload', GUnload, false);