var mapId = '';

var zoomins = [
	[new GLatLng(43, 16), 4, "Europe"],
	[new GLatLng(38, 26.5), 6, "Greece"],
	[new GLatLng(52, 6.5), 6, 	"N. Germany"],
	[new GLatLng(45.5, 14), 6, "N. Italy"],
	[new GLatLng(-16.982331946,14.446045076), 6, "velo-afrique-2012"],
	[new GLatLng(52.342890635307604, 4.871063232421875), 6, "rigas-bicycle-netherlands"]
];

// ----------------------------------------------------------------------
/* convert the url query string into an array  */
function read_querystring() {
   var a_out = new Object();
   var s_loc = String(location.href);
   if (s_loc.indexOf('?')>0) {
      var p;
      var s_query = s_loc.substr(s_loc.indexOf('?')+1);
      var a_query = s_query ? s_query.split('&') : new Array();
      for(var i=0; i<a_query.length; i++) {
         p = a_query[i].split('=');
         a_out[p[0]] = p[1].replace(/\+/g, ' ');
      }
   } 
   return a_out;
}

function init() {
  var default_map = 'rigas-bicycle-netherlands'
  var query_vars = read_querystring();
  mapId = query_vars['mapid'];
  var explainLabel = document.getElementById("explainlabel");
  var hiddenMaps = (mapId == 'bartessera' || mapId == 'rigasthessalonikihome' || mapId == 'rigasathenshome' || mapId == 'grouptherapy' || mapId == 'trasysgreece' || mapId == 'gmapathens')
  explainLabel.style.display = hiddenMaps ? "none" : "inline";
  if (mapId == undefined || mapId == 'undefined') { // no var passed
    mapId = default_map;
  } 
  setSelectValue('selectMap', mapId);
  loadMapWithId(mapId);
}

function changeMap() {
  var selectedMapValue = document.getElementById("selectMap").value;
  var selectedMap = (selectedMapValue == 'Europe' || selectedMapValue == 'N. Italy' ||
	  selectedMapValue == 'N. Germany' ||selectedMapValue == 'Greece') ? 'rigastravel' : selectedMapValue;
  if (selectedMap != mapId) { // load a different map
	mapId = selectedMap;
    loadMapWithId(mapId);
	if (selectedMap == 'rigastravel') {
      if (navigator.appName == "Microsoft Internet Explorer") {
        map.setCenter(zoomins[getSelectedMapIndex()][0], zoomins[getSelectedMapIndex()][1]); 
      } else { // only IE supports loading and setting center at the same time
	    setSelectValue("selectMap", "Europe");
	  }
	}
  } else { // same map - only possible with rigas travel
    map.setCenter(zoomins[getSelectedMapIndex()][0], zoomins[getSelectedMapIndex()][1]); 
  }
}

function loadMapWithId(aMapId) {
  load('common/maps/' + aMapId + '.xml');
}

function setSelectValue(selectElementId, value) {
  var selectElement = document.getElementById(selectElementId);
  for (i = 0; i < selectElement.length; i++) {
    if( selectElement[i].value == value) {
	  selectElement.selectedIndex = i;
	}
  }
}

/** Get the selected map index in the table above */
function getSelectedMapIndex() {
  var selectedMap = document.getElementById("selectMap").value;
    for (var i = 0; i < zoomins.length; i++) {
      if (zoomins[i][2] == selectedMap) {
        selectedMap = i;
        return selectedMap;
      }
    }
}

