// Google Maps main object
var G_MAP;

var G_MAP_KC = {
	location: new GLatLng(48.145367,17.109965),
	title: "KC Dunaj",
	address: "námestie SNP 30 (4. posch.), Bratislava",
	marker: null,
	dom: null,
	is_shown: false,
	concerts: [ 1 ]
};

var G_MAP_CC = {
	location: new GLatLng(48.117440, 17.103943),
	title: "CC Centrum",
	address: "Jiráskova 3, Petržalka",
	marker: null,
	dom: null,
	is_shown: false,
	concerts: [ 2, 3 ]
};

var G_MAP_MIR = {
	location: new GLatLng(48.144693,17.107898),
	title: "Mirbachov palác",
	address: "Františkánske námestie č. 11, Bratislava",
	marker: null,
	dom: null,
	is_shown: false,
	concerts: [ 4 ]
};

var G_MAP_ZRKADLO = {
	location: new GLatLng(48.143741, 17.109854),
	title: "Zrkadlová sieň Primaciálneho paláca",
	address: "Primaciálne námestie, Bratislava",
	marker: null,
	dom: null,
	is_shown: false,
	concerts: [ 5 ]
};


// Configures the google maps
function map_load() {
	if (! GBrowserIsCompatible()) {return;}

	G_MAP = new GMap2(document.getElementById("map"));
	G_MAP.addControl(new GLargeMapControl());
	G_MAP.addControl(new GMapTypeControl());

	var center = new GLatLng(48.138485, 17.10494);
	var zoom_level = 13;
	G_MAP.setCenter(center, zoom_level);

	// Add the concert locations to the map
	map_add(G_MAP_KC);
	map_add(G_MAP_CC);
	map_add(G_MAP_ZRKADLO);
	map_add(G_MAP_MIR);

	// Configure the overlay used to display the maps and register a callback
	// that will ensure that the maps div is resized properly. The resize is
	// very important because the overlay's animation will confuse the map and
	// we will endup with grey sections in the map.
	var centered = false;
	$('*[rel="#map-overlay"]').overlay({
			// Don't use effects, specially not the 'apple' one otherwise the
			// map can't be centered properly.
			top: '2.5%',
			onLoad: function() {
				G_MAP.checkResize();
				if (! centered) {
					G_MAP.setCenter(center, zoom_level);
					centered = true;
				}
			}
	});
}


// Adds a place to the main map
function map_add(place) {
	if (! G_MAP) {return;}

	var marker = new GMarker(place.location);
	G_MAP.addOverlay(marker);

	GEvent.addListener(marker, "click", function() {
		map_show(place);
	});

	// Make the map marker behave like a toggle
	GEvent.addListener(marker, "infowindowopen", function() {
		place.is_shown = true;
	});
	GEvent.addListener(marker, "infowindowclose", function() {
		place.is_shown = false;
	});

	place.marker = marker;
}


// Displays the info of a marker on the main map
function map_show(place) {
	if (! G_MAP) {return;}

	if (place.is_shown) {
		place.marker.closeInfoWindow();
		return;
	}

	if (! place.dom) {
		// Create a <div class="concert"> and append the concerts to it
		var div = document.createElement('div');
		div.setAttribute('class', 'concerts');
		for (var i = 0; i < place.concerts.length; ++i) {
			var concert_id = place.concerts[i];
			var nodes = $('#concert-' + concert_id).clone(); // Always an array
			div.appendChild(nodes[0]);
		}
		place.dom = div;
	}

	place.marker.openInfoWindow(place.dom);
}


function escape_html(text) {
	return text
		.replace(/&/g, "&amp;")
		.replace(/</g, "&lt;")
		.replace(/>/g, "&gt;")
	;
}


// Load and unload the maps when needed to
$(document).ready(map_load);
$(window).unload(GUnload);


