var map;
var country;
var countries_zoom = {"Netherlands":5,
                      "Austria":5,
                      "Denmark":5,
                      "United Kingdom":3,
                      "Genmany":4,
                      "Hungary":4,
                      "Slovakia":5,
                      "Slovenia":5,
                      "Spain":3,
                      "Belgium":5,
                      "Czech Republic":5,
                      "France":3}

function initializeLocationMap(country, city, event_id) {
	map = new GMap2(document.getElementById("location_right_"+event_id));
	map.disableDoubleClickZoom();
	map.disableDragging();
	// It needs to call setCenter for initializing.
	map.setCenter(new GLatLng(0, 0), 8);

	searchLocationMap(country, city, event_id);
}

var geocoder = new GClientGeocoder();
function searchLocationMap(country, city, event_id) {
	geocoder.getLatLng(country, function convertCountry(latlng) {
  	if (latlng) {
      this.country = country;
      setCenterWithCountry(latlng);
      superimposeLayer(); //add custom tiles
      
      if (city != "") { markCity(country, city, event_id); }
  	}
	});
}

var cityMark;
function markCity(country, city, event_id) {
  var city = city + ', ' + country;
	geocoder.getLatLng(city, function convertCity(latlng) {
  	if (latlng) {
  		$("#map_message_" + event_id).text("");
      if (cityMark) { map.removeOverlay(cityMark); }
      cityMark = new GMarker(latlng);
			map.addOverlay(cityMark);
      $("#latitude").val(latlng.y);
      $("#longitude").val(latlng.x);
      map.setCenter(latlng, map.getZoom());
  	} else {
  		$("#map_message_" + event_id).text(country + " did not match any locations.");
  	}
	});
}

function setCenterWithCountry(latlng) {
  if (country == "Russia") {          //the center of Russia country is a plain, in map we can't see any city
    map.setCenter(new GLatLng(55.01543, 44.05518), 2);
    return;
  }
  if (countries_zoom[country]){
    map.setCenter(latlng, countries_zoom[country]);
  } else {
    map.setCenter(latlng, 4);
  }
}

function superimposeLayer() {
  var tilelayer = new GTileLayer(new GCopyrightCollection(), 0, 1);
  tilelayer.getTileUrl = function(tile, zoom) {
    if (country == "United Kingdom") {
      if (tile.x == 3 && tile.y == 2) {
        return tiles_path(country, 'left');
      } else if (tile.x == 4 && tile.y == 2) {
        return tiles_path(country, 'right');
      }
    } else if (country == "Netherlands") {
    }
    else if (country == "Belgium") {
      if (tile.x == 16 && tile.y == 9) {
        // return tiles_path("black","");
      }
    }
    else if (country == "Czech Republic") {
      if (tile.x == 10 && tile.y == 9) {
        //   return tiles_path("black","");
      }
    }
    else if (country == "Slovakia") { //slovania
      if (tile.x == 17 && tile.y == 10) {
       //    return tiles_path("black","");
      }
    }
    else if (country == "Slovenia") { //slovania
      if (tile.x == 17 && tile.y == 10) {
           //return tiles_path("black","");
      }
    }
    else if (country == "Germany") {
      if (tile.x == 7 && tile.y == 5) {
        //   return tiles_path("black","");
      }
    }
    else if (country == "Hungary") {
      if (tile.x == 8 && tile.y == 5) {
          // return tiles_path("black","");
      }
    }
    else if (country == "Italy") {
      if (tile.x == 8 && tile.y == 5) {
           //return tiles_path("black","");
      }
    }
    else if (country == "Poland") {
      if (tile.x == 8 && tile.y == 5) {
           //return tiles_path("black","");
      }
    }
    else if (country == "Portugal") {
      if (tile.x == 7 && tile.y == 6) {
          // return tiles_path("black","");
      }
    }
    else if (country == "Spain") {//3,2;3.3; 4,2;4,3
      if (tile.x == 4 && tile.y == 3) {
           //return tiles_path("black","");
      }
    }
    return "";

  };
  tilelayer.getOpacity = function() {return 1.0}
  map.addOverlay(new GTileLayerOverlay(tilelayer));
}

function tiles_path(country, choose){

	if(choose != ""){ choose = "_" + choose;}
	return "/img2/events/map_tiles/" + country.replace(/ /, "") + choose + ".png";

}
