// Using Google Maps API [http://code.google.com/apis/maps/index.html]

// Specify a Google Maps API key for each domain where this file is used
// To obtain a key for a domain, got to http://code.google.com/apis/maps/signup.html
var apiKeys = {
	"127.0.0.7":"ABQIAAAAadFUJdA-VEu3WxVztkxNgxRIUUvo04OUp2XHF5rus0gqi6IJYxTcNEdGPG_aTdxFC5JXtLgvgEzA8g",
	"maak-je-punt.nl":"ABQIAAAAadFUJdA-VEu3WxVztkxNgxTtVDmvtYzr7G4gB1j4HLt0SCM0AxT504QJTirjBsYtW3wse8A25BeF5A",
	"make-your-mark.nl":"ABQIAAAAadFUJdA-VEu3WxVztkxNgxTI1TcUKwaaG7H2LKqKGa5LFR3MOxRjSMux-DU01nCDueyBMhvO0U3ikg",
	"www.maak-je-punt.nl":"ABQIAAAAadFUJdA-VEu3WxVztkxNgxQgQGHfHxozHXt6pwSuZsJJIY3yuxRVMQbwf9wc28qImJwLKMaIPjhidQ",
	"www.make-your-mark.nl":"ABQIAAAAadFUJdA-VEu3WxVztkxNgxR8eiRKe9HrgtRlVvChwQ40gfcDExSoaEda18oDeUBC_3g04AzvZptIrQ"
};

// Set language
var lang = location.host.indexOf("make") >= 0 ? "en" : "nl";

// Load Google Maps API
var key = apiKeys[location.host];
document.write('<script src="http://maps.google.com/maps?file=api&v=2&hl=' + lang + '&key=' + key + '"></script>');

// Load map and place markers
function loadMap() {
	if(GBrowserIsCompatible()) {
		// Center map at Gouda
		var map = new GMap2(document.getElementById("map"));
		map.setCenter(new GLatLng(52.0167, 4.7141), 13);
		map.enableScrollWheelZoom();
		map.enableContinuousZoom();

		// play icon
		var icon = new GIcon(G_DEFAULT_ICON, 'http://www.google.com/mapfiles/dd-start.png');

		// Add markers for fixed locations
		if(locations != null) {
			var markers = new Array();
			var html = new Array();
			for(var i = 0; i < locations.length; i++) {

				// create array holding tabs html
				html[i] = new Array();

				// create tab with slide show of images
				if(media != null && media[i] != null && media[i].length > 0) {
					var imgTab = '<div class="info"><p>Set: <span id="set' + i + '" style="display:none">0</span>';
					for(var n = 0; n < media[i].length; n++) {
						imgTab +=  '<a href="javascript:set(' + i + ',' + n + ')">' + (n+1) + '</a> &nbsp; ';
					}
					imgTab += '</p><img id="img' + i + '" src="' + media[i][0][0] + '" width="240" height="320" />';
					imgTab += '<div class="buttons"><a href="javascript:prev(' + i + ')"><img src="/images/left.gif" width="13" height="16" /></a>&nbsp;&nbsp;<span id="cnt' + i + '">' + 1 + ' / ' + media[i][0].length + '</span>&nbsp;&nbsp;<a href="javascript:next(' + i + ')"><img src="/images/right.gif" width="13" height="16" /></a>';
					imgTab += '<div class="startstop"><a href="javascript:stop()"><img id="stop" src="/images/stop.gif" width="13" height="13" /></a></div></div>';
					imgTab += '</div>';
					html[i].push(new GInfoWindowTab(lang == 'en' ? 'Images' : "Foto's", imgTab));
				}

				// create tab with list of audio files
				if(audio != null && audio[i] != null && audio[i].length > 0) {
					var audioTab = '<div class="info"><p>';
					for(var n = 0; n < audio[i].length; n++) {
						var file = audio[i][n];
						audioTab += '<a href="' + file + '" onclick="playAudio(this.href);return false">' + file + '</a><br/>';
					}
					audioTab += '</p></div>';
					html[i].push(new GInfoWindowTab('Interviews', audioTab));
				}

				if(html[i].length > 0) {
					var marker = new PdMarker(new GLatLng(locations[i].lat, locations[i].lng), icon);
					marker.bindInfoWindowTabsHtml(html[i]);
				}
				else {
					var marker = new PdMarker(new GLatLng(locations[i].lat, locations[i].lng));
				}

				marker.setTooltip(locations[i][lang]);
				marker.setOpacity(80);
				map.addOverlay(marker);
				markers[i] = marker;
			}
		}

	}
	else {
		location.href = "index.html";
	}
}

function show(index) {
	if(markers != null && index < markers.length) {
		GEvent.trigger(markers[index], 'click');
	}
}

function playAudio(url) {
/*
	var url = this.getUserData();
	if(url.indexOf('://') < 0) {
		url = 'sounds/' + url;
	}
*/
	var player = window.audioplayer || document.audioplayer;
	if(player != null) {
		player.setParam('url', url);
		player.restart();
		player.doPlay();
	}
	else alert('player not found');
}

function stopAudio() {
	var player = window.audioplayer || document.audioplayer;
	if(player != null) {
		player.doStop();
	}
}

function basename(s) {
	var p = s.lastIndexOf('/');
	if(p >= 0) {
		s = s.substr(++p);
	}
	return s;
}

function set(id, index) {
	var img = document.getElementById('img' + id);
	var set = document.getElementById('set' + id);
	var cnt = document.getElementById('cnt' + id);
	if(img != null && set != null && cnt != null) {
		img.src = media[id][index][0];
		set.innerHTML = index;
		cnt.innerHTML = '1 / ' + media[id][index].length;
	}
}

function prev(id) {
	var img = document.getElementById('img' + id);
	var set = document.getElementById('set' + id);
	var cnt = document.getElementById('cnt' + id);
	if(img != null && set != null && cnt != null) {
		var counter = cnt.innerHTML.split(' / ');
		if(counter.length == 2) {
			if(counter[0] > 1) {
				--counter[0];
			}
			else {
				counter[0] = counter[1];
			}
			img.src = media[id][set.innerHTML][counter[0] - 1];
			cnt.innerHTML = counter.join(' / ');
		}
	}
}

function next(id) {
	var img = document.getElementById('img' + id);
	var set = document.getElementById('set' + id);
	var cnt = document.getElementById('cnt' + id);
	if(img != null && set != null && cnt != null) {
		var counter = cnt.innerHTML.split(' / ');
		if(counter.length == 2) {
			if(counter[0] < media[id][set.innerHTML].length) {
				++counter[0];
			}
			else {
				counter[0] = 1;
			}
			img.src = media[id][set.innerHTML][counter[0] - 1];
			cnt.innerHTML = counter.join(' / ');
		}
	}
}