/**
 * 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 GMAP = {
	map: '',
	initMap: function() {
		if ( document.getElementById( 'map' ) && typeof GBrowserIsCompatible !== 'undefined' && GBrowserIsCompatible() ) {
			GMAP.map = new google.maps.Map2( document.getElementById( 'map' ) );
			GEvent.addDomListener( GMAP.map, 'load', function() {
				setTimeout( 'GMAP.GKeyboardPatch( GMAP.map );', 3000 );
			});
			GMAP.map.setCenter( new GLatLng(50.748456,7.097392), 15 );
			GMAP.map.addControl( new google.maps.SmallMapControl() );
			GMAP.map.addControl( new google.maps.MapTypeControl() );
			new GKeyboardHandler( GMAP.map );
			
			// Set latitude and longitude
			var point = new GLatLng( 50.748456757575814, 7.097392901826859 );
			// Create a marker
			marker = new GMarker( point );
			// Add the marker to map
			GMAP.map.addOverlay( marker );
			GMAP.map.enableScrollWheelZoom();
			GMAP.map.enableContinuousZoom();
		}
	},
	
	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++;
		}
	}
}

var EAFRA = {
	init: function(){
		// Add abbr element in IE 6-7
		document.createElement('abbr');
		// Initialize skiplinks in IE
		EAFRA.initFocus();
		// Fix WordPress navigation
		EAFRA.fixNav();
		// Prevent multiple form submit
		// EAFRA.captureSubmit();
	},
	
	initFocus: function() {
		$( '#skiplinks a' ).bind( 'focus', EAFRA.toggleTarget ).bind( 'blur', EAFRA.toggleTarget );
	},
	toggleTarget: function( e ) {
		$( this.hash ).toggleClass( 'target' );
	},
	
	fixNav: function() {
		$( '#nav a' ).removeAttr( 'title' );
		$( '#nav li.current_page_item a' ).removeAttr( 'href' );
	},
	
	captureSubmit: function() {
		$( '#submit' ).click( EAFRA.disableSubmit );
	},
	disableSubmit: function() {
		setTimeout( "$( '#submit' ).attr( 'disabled', 'disabled' )", 200 );
	},
	
	map: '',
	initMap: function() {
		if ( document.getElementById( 'map' ) && typeof GBrowserIsCompatible !== 'undefined' && GBrowserIsCompatible() ) {
			EAFRA.map = new google.maps.Map2( document.getElementById( 'map' ) );
			GEvent.addDomListener( EAFRA.map, 'load', function() {
				setTimeout( 'EAFRA.GKeyboardPatch( EAFRA.map );', 3000 );
			});
    		EAFRA.map.setCenter( new GLatLng(50.748456757575814, 7.097392901826859 ), 15 );
			EAFRA.map.addControl( new google.maps.SmallMapControl() );
			EAFRA.map.addControl( new google.maps.MapTypeControl() );
			new GKeyboardHandler( EAFRA.map );
									
			// Set latitude and longitude
			var point = new GLatLng( 50.748456757575814, 7.097392901826859  );
			// Create a marker
			marker = new GMarker( point );
			var location = $( '#location' );
			GEvent.addListener(marker, 'click', function(){
				marker.openInfoWindowHtml( '<strong>' + location.find( 'h3' ).text() + '</strong><br />' + location.find( 'p.adr' ).html());
			});
			// Add the marker to map
			EAFRA.map.addOverlay(marker);
			EAFRA.map.enableScrollWheelZoom();
			EAFRA.map.enableContinuousZoom();			
			
			// get the geo latitude and longitude and display vcard(s) on map
			var currentMarker = false;
			var baseIcon = new GIcon(G_DEFAULT_ICON);
			baseIcon.shadow = "http://www.google.com/mapfiles/shadow50.png";
			baseIcon.iconSize = new GSize(20, 34);
			baseIcon.shadowSize = new GSize(37, 34);
			baseIcon.iconAnchor = new GPoint(9, 34);
			baseIcon.infoWindowAnchor = new GPoint(9, 2);
			
			$('.vcard').each(function(i){
				var vcard = $(this);
				var lat = vcard.find('.latitude').text();
				var lon = vcard.find('.longitude').text();
				var letter = (i < 3) ? '' : String.fromCharCode('A'.charCodeAt(0) + (i - 3));
				if ( letter === '' ) {
					letter = ( i === 1 ) ? 'M' : 'I';
				}
				vcard.find( 'a.fn' ).attr( 'href', '#' + letter/*.toLowerCase()*/ ).click( function() {
					if ( currentMarker ) {
						EAFRA.map.removeOverlay(currentMarker);
					}
					$('.current-vcard').removeClass('current-vcard');
					vcard.addClass('current-vcard');
					var point = new GLatLng(lat, lon);
					
					// Create a lettered icon for this point using our icon class
					var letteredIcon = new GIcon(baseIcon);
					letteredIcon.image = 'http://www.google.com/mapfiles/marker' + letter + '.png';
					
					// Set up our GMarkerOptions object
					markerOptions = { icon:letteredIcon };
					var marker = new GMarker(point, markerOptions);
					
					currentMarker = marker;
					GEvent.addListener(marker, 'click', function(){
						marker.openInfoWindowHtml( '<strong>' + vcard.find( 'h4' ).text() + '</strong><br />' + vcard.find( 'span.adr' ).html());
					});
					EAFRA.map.addOverlay(marker);
					EAFRA.map.panTo(point);
					return false;
				});
			});
		}
	},
		
	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++;
		}
	},
	
	// Normalize tabindex attribute
	nTabindex: function() {
		return ( document.body && document.body.tabIndex === 0 ) ? 'tabIndex' : 'tabindex';
	}
}
$(document).ready(function() {
	EAFRA.init();
});
