var paikkaType = '';
var paikkaId = '';
var geocoder = '';
var latLng = '';

var gm_map = '';
var gm_gd  = '';
var gm_gc  = '';
  
$(document).ready( function () {

	if (!$("#googlemaps")) return false;

//	Params
	var params = $("#googlemaps").attr("title").split("|");
	paikkaType = params[0];
	paikkaId = params[1];
	$("#googlemaps").attr("title", "");

//	Map
	gm_map = new GMap2( document.getElementById("googlemaps") );
//  gm_map.setCenter(new GLatLng(62.240393, 25.742635), 5);
//  gm_map.setCenter(new GLatLng(64.217689, 26.482544), 5);
	gm_map.setCenter(new GLatLng(65.036761, 26.537476), 5);
	gm_map.enableScrollWheelZoom();
	gm_map.addControl(new GSmallMapControl());
	geocoder = new GClientGeocoder();
	loadpaikkas();
});

function loadpaikkas () {
  
	var params = new Object();
	if (paikkaType) params["type"] = paikkaType;
	if (paikkaId)   params["id"] = paikkaId;
  
	var puisto_icon = new GIcon({
		image: "/files/lahiliikuntapaikat/cssimg/green.png",
		iconSize: new GSize(22, 30),
		iconAnchor: new GPoint(22, 30)
	});
	
	var alapiha_icon = new GIcon({
		image: "/files/lahiliikuntapaikat/cssimg/yellow.png",
		iconSize: new GSize(22, 30),
		iconAnchor: new GPoint(22, 30)
	});
	
	var ylapiha_icon = new GIcon({
		image: "/files/lahiliikuntapaikat/cssimg/blue.png",
		iconSize: new GSize(22, 30),
		iconAnchor: new GPoint(22, 30)
	});
	
	var muu_icon = new GIcon({
		image: "/files/lahiliikuntapaikat/cssimg/red.png",
		iconSize: new GSize(22, 30),
		iconAnchor: new GPoint(22, 30)
	});
  
	$.ajax({
		type:  "GET",
		async: false,
		cache: true,
		url:   "/lahiliikuntapaikat.xml",
		data:  params,
		dataType: 'xml',
		success: function (data) {
  
		$("paikka", data).each( function () {
			var pid = $(this).attr("id");
			var lat = $(this).find("lat").text();
			var lng = $(this).find("lng").text();
			var type = $(this).find("type").text();
			var address = $(this).find("address").text();
			var title = type +" - "+ $(this).find("title").text();

			var osoite = address;

			if ( type.indexOf(paikkaType) != -1 /* paikkaType == type */ || paikkaId == pid || paikkaType == 0) {
				if (type=="muu") {
					if (geocoder) {
					geocoder.getLocations(
					  osoite,
					  function(point) {
						if (!point || point.Status.code != 200) {
							
							var latLng = new GLatLng(lat, lng);
							var paikka = new GMarker(
							latLng, { "title": title, "icon": muu_icon } 
							);
							
							var paikkaUrl = "perustiedot_" + type + "?id="+ pid;
							GEvent.addListener(paikka, "click", function () { document.location = paikkaUrl });
							gm_map.addOverlay(paikka);
							
							
						} else {
						
						
							var place = point.Placemark[0];
							var latLng = new GLatLng(place.Point.coordinates[1],place.Point.coordinates[0]);
							var paikka = new GMarker(
							latLng, { "title": title, "icon": muu_icon } 
							);
							
							var paikkaUrl = "perustiedot_" + type + "?id="+ pid;
							GEvent.addListener(paikka, "click", function () { document.location = paikkaUrl });
							gm_map.addOverlay(paikka);
							
							
						}
						if (paikkaId) gm_map.setCenter(latLng, 15);
					  }
					);
				  } else {
				  }
				}
	
				else if (type=="alakoulupiha") {
				if (geocoder) {
					geocoder.getLocations(
					  osoite,
					  function(point) {
						if (!point || point.Status.code != 200) {
							
							var latLng = new GLatLng(lat, lng);
							var paikka = new GMarker(
							latLng, { "title": title, "icon": alapiha_icon } 
							);
							
							var paikkaUrl = "perustiedot_" + type + "?id="+ pid;
							GEvent.addListener(paikka, "click", function () { document.location = paikkaUrl });
							gm_map.addOverlay(paikka);
							
							
						} else {
						
						
							var place = point.Placemark[0];
							var latLng = new GLatLng(place.Point.coordinates[1],place.Point.coordinates[0]);
							var paikka = new GMarker(
							latLng, { "title": title, "icon": alapiha_icon } 
							);
							
							var paikkaUrl = "perustiedot_" + type + "?id="+ pid;
							GEvent.addListener(paikka, "click", function () { document.location = paikkaUrl });
							gm_map.addOverlay(paikka);
							
							
						}
						if (paikkaId) gm_map.setCenter(latLng, 15);
					  }
					);
				  } else {
				  }
				}
	
				else if (type=="ylakoulupiha") {
				if (geocoder) {
					geocoder.getLocations(
					  osoite,
					  function(point) {
						if (!point || point.Status.code != 200) {
							
							var latLng = new GLatLng(lat, lng);
							var paikka = new GMarker(
							latLng, { "title": title, "icon": ylapiha_icon } 
							);
							
							var paikkaUrl = "perustiedot_" + type + "?id="+ pid;
							GEvent.addListener(paikka, "click", function () { document.location = paikkaUrl });
							gm_map.addOverlay(paikka);
							
							
						} else {
						
						
							var place = point.Placemark[0];
							var latLng = new GLatLng(place.Point.coordinates[1],place.Point.coordinates[0]);
							var paikka = new GMarker(
							latLng, { "title": title, "icon": ylapiha_icon } 
							);
							
							var paikkaUrl = "perustiedot_" + type + "?id="+ pid;
							GEvent.addListener(paikka, "click", function () { document.location = paikkaUrl });
							gm_map.addOverlay(paikka);
							
							
						}
						if (paikkaId) gm_map.setCenter(latLng, 15);
					  }
					);
				  } else {
				  }
				}
	
				else {
				if (geocoder) {
					geocoder.getLocations(
					  osoite,
					  function(point) {
						if (!point || point.Status.code != 200) {
							
							var latLng = new GLatLng(lat, lng);
							var paikka = new GMarker(
							latLng, { "title": title, "icon": puisto_icon } 
							);
							
							var paikkaUrl = "perustiedot_" + type + "?id="+ pid;
							GEvent.addListener(paikka, "click", function () { document.location = paikkaUrl });
							gm_map.addOverlay(paikka);
							
							
						} else {
						
						
							var place = point.Placemark[0];
							var latLng = new GLatLng(place.Point.coordinates[1],place.Point.coordinates[0]);
							var paikka = new GMarker(
							latLng, { "title": title, "icon": puisto_icon } 
							);
							
							var paikkaUrl = "perustiedot_" + type + "?id="+ pid;
							GEvent.addListener(paikka, "click", function () { document.location = paikkaUrl });
							gm_map.addOverlay(paikka);
							
							
						}
						if (paikkaId) gm_map.setCenter(latLng, 15);
					  }
					);
				  } else {
				  }
				}
   
//if (paikkaId) gm_map.setCenter(latLng, 15);
    
			}
		});
		}
	});
}
