/**
 * Keyboard-accessible Google Maps
 * 
 * Written by Patrick H. Lauke (@patrick_h_lauke) for Dev.Opera
 * http://dev.opera.com/articles/view/keyboard-accessible-google-maps/
 * 
 * Modified by Martin Kliehm (@kliehm) for Learning the World
 * http://learningtheworld.eu/2009/keyboard-accessible-google-maps/
 * 
 * Code licensed under the MIT License:
 * http://www.opensource.org/licenses/mit-license.php
 */

var ACCESSIBLE_GMAP = {
	map: '',	
	initMap: function(mapid, lat, lng, title, contents) {
		if (document.getElementById(mapid) && typeof GBrowserIsCompatible !== 'undefined' && GBrowserIsCompatible()) {
			ACCESSIBLE_GMAP.map = new GMap2(document.getElementById('map'));
			GEvent.addDomListener(ACCESSIBLE_GMAP.map, 'load', function() {
				setTimeout('ACCESSIBLE_GMAP.GKeyboardPatch(ACCESSIBLE_GMAP.map);', 3000);
			});
			ACCESSIBLE_GMAP.map.setCenter(new GLatLng(lat, lng), 15);
			ACCESSIBLE_GMAP.map.addControl(new google.maps.SmallMapControl());
			ACCESSIBLE_GMAP.map.addControl(new google.maps.MapTypeControl());
			new GKeyboardHandler(ACCESSIBLE_GMAP.map);
			
			// Create a marker (if required)
			if (title != undefined && title != "") {
				// Set latitude and longitude
				var point = new GLatLng(lat, lng);
				// Create a marker
				var pin = new GIcon();
				pin.image = "/_designs/scripts/gmap-icons/map-marker.png";
				pin.iconSize = new GSize(30, 35);
				pin.shadow = "/_designs/scripts/gmap-icons/map-marker-shadow-base.png";
				pin.shadowSize = new GSize(54, 35);
				pin.iconAnchor = new GPoint(13, 33);
				pin.infoWindowAnchor = new GPoint(13, 33);
				var marker = new GMarker(point, { icon: pin });
				// Add the marker to map
				ACCESSIBLE_GMAP.map.addOverlay(marker);
				// Bind the info window
				marker.bindInfoWindowHtml('<h3>' + title + '</h3>' + contents);
			}
			
			ACCESSIBLE_GMAP.map.enableScrollWheelZoom();
			ACCESSIBLE_GMAP.map.enableContinuousZoom();
		}
	},
	
	addMarker: function(label, lat, lng, title, contents, listingLink) {
		var point = new GLatLng(lat, lng);
		// Create a marker
		var pin = new GIcon();
		pin.image = "/_designs/scripts/gmap-icons/map-marker-" + label + ".png";
		pin.iconSize = new GSize(36, 45);
		pin.shadow = "/_designs/scripts/gmap-icons/map-marker-shadow.png";
		pin.shadowSize = new GSize(56, 45);
		pin.iconAnchor = new GPoint(17, 43);
		pin.infoWindowAnchor = new GPoint(17, 43);
		var marker = new GMarker(point, { icon: pin });
		// Add the marker to map
		ACCESSIBLE_GMAP.map.addOverlay(marker);
		// Bind the info window
		marker.bindInfoWindowHtml('<h3>' + title + '</h3>' + contents);
		listingLink.click(function(e) {
			e.preventDefault();
			GEvent.trigger(marker, "click");
			$('html,body').animate({scrollTop: $("#map").offset().top - 40}, 500);
		});
                
                 function querySt(ji) {
	hu = window.location.search.substring(1);
	gy = hu.split("&");
	for (i=0;i<gy.length;i++) {
		ft = gy[i].split("=");
		if (ft[0] == ji) {
			return ft[1];
		}
	}
	return "";
}
                 if ($(".getDirections").length) {
			//used for get directions when a pin is clicked
			GEvent.addListener(marker, "click", function(){
				//get address
				var toLoc = "";
				if ($.trim(listingLink.next("span").eq(0).text()) !== "") {
					toLoc = " in " + listingLink.next("span").eq(0).text();
				}
				$("#gd-panel .dt-to").text(listingLink.parents(".search-result").eq(0).find("h3").text() + toLoc);
				$("#gd-panel .d-to").val(lat + "," + lng);
                
				if ($(".opp-result").length) {
					if ($.trim(listingLink.next("address").eq(0).text()) !== "") {
						toLoc = " in " + listingLink.next("address").eq(0).text();
					}
					$("#gd-panel .dt-to").text($("h2.result-heading").eq(0).text() + toLoc);
					$("#gd-panel .d-to").val(lat + "," + lng);
					
				}
				
				//clear directions
				$("#gd-panel .clear").trigger("click");
			});
		}
	},
	
	GKeyboardPatch: function(map) {
		var button, divs = map.getContainer().getElementsByTagName('div');
		var i = 0;
		while (divs[i]) {
			if (divs[i].getAttribute('log') || (divs[i].getAttribute('title') && divs[i].getAttribute('title') != '')) {
				button = document.createElement('button');
				button.setAttribute('value', divs[i].getAttribute('title'));
				divs[i].appendChild(button);
				if (divs[i].getAttribute('log')) { // only control buttons
					// override the IE opacity filter that Google annoyingly sets
					divs[i].style.filter = '';
					// should really set to 'transparent'
					divs[i].style.background = 'url(http://www.google.com/intl/en_ALL/mapfiles/transparent.png)';
				}
			}
			i++;
		}
	}
}
