//variables nd, sd, kd, bond must be filled with data about buildings from backend! This variables should be inicialized in body of document

function init(){
	mapData = {base: {lat: 50.083651, lng: 14.418054, zoom: 15},
		icon: {path: "./graphics/ico/", size: {x: 20, y: 30}, offset: {x: 5, y: 30},
			shadow: {fileName: "shadow.png", size: {x: 35, y: 24}, offset: {x: 5, y: 30}}},
		point: {nd: nd, sd: sd, kd: kd, bond: bond}};
	loadMap(mapData, "map");
}
function showOnMap(pointName){
	var aPoint = mapData.point[pointName];
	aPoint.marker.openInfoWindowHtml(infoWindowHtml(aPoint));
}

function createBaseIcon(mapData){
	var baseIcon = new GIcon();
	baseIcon.shadow = mapData.icon.path+mapData.icon.shadow.fileName;
	baseIcon.iconSize = new GSize(mapData.icon.size.x, mapData.icon.size.y);
	baseIcon.shadowSize = new GSize(mapData.icon.shadow.size.x, mapData.icon.shadow.size.y);
	baseIcon.iconAnchor = new GPoint(mapData.icon.offset.x, mapData.icon.offset.y);
	baseIcon.infoWindowAnchor = new GPoint(9, 2);
	baseIcon.infoShadowAnchor = new GPoint(mapData.icon.shadow.offset.x, mapData.icon.shadow.offset.y);
	mapData.icon.base = baseIcon;
	return baseIcon;
}

function createMarker(point) {
  var icon = new GIcon(mapData.icon.base);
  icon.image = mapData.icon.path+point.selector+".ico.map.png";
	markerOptions = { icon:icon };

  var marker = new GMarker(new GLatLng(point.geo.lat, point.geo.lng), markerOptions);
  GEvent.addListener(marker, "click", function() {
    marker.openInfoWindowHtml(infoWindowHtml(point));
		});
	point.marker = marker;
  return marker;
}

function infoWindowHtml(point){
	return "<div class='infoWindow'><img src='"+mapData.icon.path+point.selector+".thumb.map.png"+"' class='infoImage' /><strong>"+point.title+ "</strong><br/> "+ point.address+"<p><small>"+point.description+"</small></p></div>";
}

function loadMap(mapData, targetDiv) {
  if (GBrowserIsCompatible()) {
    var map = new GMap2(document.getElementById(targetDiv));
    mapData.map = map;
    initMap();
		baseIcon = createBaseIcon(mapData);

	  map.addControl(new GSmallMapControl());
		map.addControl(new GMapTypeControl());

		for(var aPoint in mapData.point){
	  	map.addOverlay(createMarker(mapData.point[aPoint]));
	  	}
	  
		if(window.onLoadShowPointWithSelector != undefined )
		  showOnMap(window.onLoadShowPointWithSelector);
			
	}
}

function initMap(){
	mapData.map.setCenter(new GLatLng(mapData.base.lat, mapData.base.lng), mapData.base.zoom);
}

