/*

Patch standard Google Map for keyboard accessibility

(remember, accessibility isn't just about the blind ... sighted users with mobility impairments may also wish
 to use your maps, and currently the weird "keyboard access" option that gmap has is suboptimal in that it requires
 weird hacking to first "activate" the map to enable the - not exposed/announced - keyboard shortcuts, which may also
 override further page controls or conflict with assistive technologies)

Thanks to Patrick H. Lauke / www.splintered.co.uk / redux@splintered.co.uk / December 2007

expects GMap2 object as parameter

*/
/*

Patch standard Google Map for keyboard accessibility

(remember, accessibility isn't just about the blind ... sighted users with mobility impairments may also wish
 to use your maps, and currently the weird "keyboard access" option that gmap has is suboptimal in that it requires
 weird hacking to first "activate" the map to enable the - not exposed/announced - keyboard shortcuts, which may also
 override further page controls or conflict with assistive technologies)

Thanks to Patrick H. Lauke / www.splintered.co.uk / redux@splintered.co.uk / December 2007

expects GMap2 object as parameter

*/


function addEvent( obj, type, fn )
{
	if (obj.addEventListener)
		obj.addEventListener( type, fn, false );
	else if (obj.attachEvent)
	{
		obj["e"+type+fn] = fn;
		obj[type+fn] = function() { obj["e"+type+fn]( window.event ); }
		obj.attachEvent( "on"+type, obj[type+fn] );
	}
}

function removeEvent( obj, type, fn )
{
	if (obj.removeEventListener)
		obj.removeEventListener( type, fn, false );
	else if (obj.detachEvent)
	{
		obj.detachEvent( "on"+type, obj[type+fn] );
		obj[type+fn] = null;
		obj["e"+type+fn] = null;
	}
}
// addEvent( document.body, load, showgmap )
function showgmap(latitude, longitude) {
	// alert( "lat:" + latitude + "\n long:" + longitude );

	var rand_query_string = 1 + Math.floor(Math.random()*9999)
	var xmlfilename = 'langdoncommunity.kml?' + rand_query_string;
	// alert( xmlfilename );
	if (GBrowserIsCompatible()) {
	// alert( "lat:" + latitude + "\n long" + longitude );
		var map = new GMap2(document.getElementById("map"));
		// GEvent.addDomListener(map, "load", function() { setTimeout('GKeyboardPatch(map);',3000); });
		//var centerpoint = new GLatLng('0','0');
		map.setCenter(new GLatLng( longitude, latitude ), 15, G_NORMAL_MAP);
		
		//map.enableContinuousZoom();
		//map.enableScrollWheelZoom();
	
		GDownloadUrl( xmlfilename, function(data, responseCode) {
			
			map.addControl(new GLargeMapControl());
			map.addControl(new GMapTypeControl());
			map.addControl(new GScaleControl());
			var centerX=0.0, centerY=0.0;
			var xml = GXml.parse(data);
			var markers = xml.documentElement.getElementsByTagName("Placemark");
			for (var i = 0; i < markers.length; i++) {
				var point = new GLatLng(parseFloat(markers[i].getElementsByTagName("latitude")[0].firstChild.nodeValue), parseFloat(markers[i].getElementsByTagName("longitude")[0].firstChild.nodeValue));
				map.addOverlay(marker = new GMarker(point));
				//centerX=parseFloat(markers[i].getElementsByTagName("latitude")[0].firstChild.nodeValue)+parseFloat(centerX);
	
				//centerY=parseFloat(markers[i].getElementsByTagName("longitude")[0].firstChild.nodeValue)+parseFloat(centerY);
				// Show this marker's index in the info window when it is clicked
					marker.html = markers[i].getElementsByTagName("description")[0].firstChild.nodeValue;
					GEvent.addListener(marker, "click", function() {
						this.openInfoWindowHtml('<p class="googlemapdisplay">'+this.html+'</p>');
					});
			}		
			//centerX=centerX/markers.length;
			//centerY=centerY/markers.length;
			//var centerpoint = new GLatLng(centerX,centerY);
			// map.setCenter(centerpoint, 15, G_NORMAL_MAP);
		});	
		//new GKeyboardHandler(map);
		
	}		
}

function GKeyboardPatch(m) {
	var m = m.getContainer(); // GMap2 method
	var divs = m.getElementsByTagName('DIV');
	var button, span, button_style = 'width:100%;height:100%;background:transparent;border-width:0px;border-color:#fff;border-style:solid;position:absolute;top:0;left:0;cursor:pointer;overflow:hidden;padding:0;margin:0;', span_style = 'display:block;visibility:hidden;padding:0;margin:0;';
	for (var i = 0; i < divs.length; i++) {
		if ((divs[i].getAttribute('log'))||((divs[i].getAttribute('id'))&&(divs[i].getAttribute('id')!='map_magnifyingglass'))) { // should really use hasAttribute, but IE doesn't implement this method
			button = document.createElement("BUTTON");
			span = document.createElement("SPAN");
			span.appendChild(document.createTextNode(divs[i].getAttribute('title')));
			button.appendChild(span);
			 // proper W3C DOM methods for styling
			button.setAttribute('style',button_style);
			span.setAttribute('style',span_style);
			// ...and now to make it work in IE
			button.style.cssText = button_style;
			span.style.cssText = span_style;
			divs[i].appendChild(button);
		}
		if (divs[i].getAttribute('log')) {
			// override the IE opacity filter that Google annoyingly sets
			divs[i].style.filter = '';
			// should really set to 'transparent', but this messes up the map controls in IE
			divs[i].style.background = 'url(http://www.google.com/intl/en_ALL/mapfiles/transparent.png)';
		}
	}
}
