// Use log() instead of console.log()
var _console_ = window.console;
var _console_log_ = _console_ && _console_.log;
function log() {
	_console_log_ && _console_log_.apply( _console_, arguments );
}
log( 'calling this before google maps should fix console for whole session' );
function logEvent( obj, type, event ) {
	GEvent.addListener( obj, event, function() {
		log( type + ':' + event );
	});
}

// JavaScript Document
jQuery( function( $ ) {
				 				 
$(document).pngFix(); 
			 
//////////////////////////////////////////
//////////// GLOBAL VARS /////////////////

var $window = $(window), $document = $(document);
var $map = new Object(), map;
var firstRun = true;
var icons = {};

/////////////  END  ///////////////////////
//////////////////////////////////////////

var airports = { "array": [{ 'lat':50.102744, 'lng':14.256649, "title":"Prague Airport", "description":"Ruzyne international airport in Prague", "handle":"prague_airport_01", "airport_page":"/prague-airport" }, { 'lat':55.419199, 'lng':37.909956, "title":"Domodedovo Airport", "description":"Domodedovo international airport in Moscow", "handle":"domodedovo_airport_02", "airport_page":"/domodedovo-airport" }] };


function initMap() {
	
	if( ! GBrowserIsCompatible() )
		return;
	
	$map = $('#map');
	map = new GMap2( $map[0], {
		mapTypes: [
			G_NORMAL_MAP,
			G_SATELLITE_MAP,
			G_HYBRID_MAP,
			G_PHYSICAL_MAP
		]
	});
	
	map.addControl( new GMapTypeControl() );
	
	map.enableContinuousZoom();
	map.enableDoubleClickZoom();
	map.enableScrollWheelZoom();
	
	new GKeyboardHandler(map);
	
	map.addControl( new GLargeMapControl(),
		new GControlPosition( G_ANCHOR_TOP_LEFT, new GSize(5,65) )
	);
	
	
	GEvent.addListener( map, 'moveend', function() {				 
		
	});
	
	GEvent.addListener( map, 'zoomend', function() {
		
	});
	
	GEvent.addListener( map, 'infowindowopen', function() {
		
	});
	
	GEvent.addListener( map, 'extinfowindowopen', function() {
		
	});
	
	GEvent.addListener( map, 'extinfowindowclose', function( ) {
		resetMap( 12 );
	});
	
	
	GEvent.addListener( map, 'maptypechanged', function() {
	
	});
	
	resetMap( 2 );
	createIcons();
	placeMarkers( airports );
}

function resetMap( zoom )
{
	var lLat = 50.102744;  
	var lLon = 14.256649; 	
	var lastMapPos = new GLatLng(lLat, lLon);
	map.setCenter(lastMapPos, zoom, G_NORMAL_MAP);
}

function createIcons() {
		// TODO: refactor
		var base = icons.base = new GIcon( G_DEFAULT_ICON );
		base.image = 'images/360airports_pin.png';
		base.shadow = 'images/360airports_shadow.png';
		base.iconSize = new GSize( 58, 26 );
		base.shadowSize = new GSize( 58, 26 );
		base.iconAnchor = new GPoint( 7, 32 );
		base.infoWindowAnchor = new GPoint( 5, 8 );  // HACK - these are fudge numbers - I don't think I got it quite right in the ExtInfoWindow code
		base.printImage = null;
		base.mozPrintImage = null;
		base.printShadow = null;
		base.transparent = null;
}


function placeMarkers( airports ) {
			
	$(airports.array).each( function( index, airport ) {
		
		var latlng = airport.latlng = new GLatLng( airport.lat, airport.lng );					 
	
		var pin = {
		icon: icons.base, rest: '360airports_pin.png', over: '360airports_pin_over.png'};
	
		
		var opts = { 
		  icon: pin.icon,
		  title: airport.title
		};
		
		var marker = airport.marker = new GMarker( latlng, opts );
		
		marker.pin = pin;
		
		marker.state = 'up';
	
		map.addOverlay( marker );
		
		GEvent.addListener(marker, "mouseover", function() {
			marker.state = 'over';
			marker.setImage( 'images/' + pin.over );
		});
		
		GEvent.addListener(marker, "mouseout", function() {
			marker.state = 'up';
			marker.setImage( 'images/' + pin.rest );
		});
		
		GEvent.addListener( marker, 'click', function() {
			
			document.location = airport.airport_page;
		});
		
	});
	
}


	
T = function( name, values, give) {
	name = name.split(':');
	var url = '/zomega/' + name[0] + '.html', part = name[1];
	if( T.urls[url] )
		return ready();
	$.ajax({ url:url, dataType:'text', success:success, error:error });
	
	function success( data, status ) {
		var a = data.replace( /\r\n/g, '\n' ).split( /\n::/g );
		var o = T.urls[url] = {};
		for( var i = 1, n = a.length;  i < n;  ++i ) {
			var s = a[i], k = s.match(/^\S+/), v = s.replace( /^.*\n/, '' );
			o[k] = $.trim(v);
		}
		ready();
	}
	
	function error( xhr, status, thrown ) {
		//onError( status );
	}
	
	function ready() {
		var text = T.urls[url][part].replace(
			/\{\{(\w+)\}\}/g,
			function( match, name ) {
				var value = values[name];
				return value != null ? value : match;
			});
		give && give(text);
		return text;
	}
};
	

initMap();

})
