var map;
function SetMapPoint(X,Y,title,xabstract,ximage,mapid,icon,iconwidth,iconheight) {
	map = new GMap2(document.getElementById(mapid));
	var gpObj = new GLatLng(X,Y);
	map.setCenter(gpObj,13);
	map.addControl(new GLargeMapControl());
	map.addControl(new GMapTypeControl());

	//  ======== Add a map overview ==========
	//  ======== save a reference to the control =========
	var ovcontrol = new GOverviewMapControl(new GSize(160,160));
	map.addControl(ovcontrol);

	//  ======== A function to adjust the positioning of the overview ========
	function positionOverview(x,y) {
		var omap=document.getElementById("map_overview");
		omap.style.left = x+"px";
		omap.style.top = y+"px";

		// == restyling ==
		omap.firstChild.style.border = "1px solid gray";

		omap.firstChild.firstChild.style.left="4px";
		omap.firstChild.firstChild.style.top="4px";
		omap.firstChild.firstChild.style.width="150px";
		omap.firstChild.firstChild.style.height="150px";
	}

	info = '<h3>'+title+'</h3><table width="400px"><tr><td>'+xabstract+'</td>';
	if (ximage != '') {
		info = info+'<td><img class="img" src="/public/data/'+ximage+'" alt="'+title+'" /></td>';
	}
	info = info + '</tr></table>';
	
	// ³]©wGIcon
	var iconMole = new GIcon(); 
	iconMole.image = icon;
	iconMole.shadow = icon;
	if (iconwidth == 0){
	iconMole.iconSize = '';
	iconMole.shadowSize = '';
	iconMole.iconAnchor = new GPoint(0, 0);
	}
	else {
	iconMole.iconSize = new GSize(iconwidth, iconheight);
	iconMole.shadowSize = new GSize(iconwidth, iconheight);
	iconMole.iconAnchor = new GPoint(iconwidth/2, iconheight);
	}
	iconMole.infoWindowAnchor = new GPoint(0, 0);
	
	var marker = new GMarker(gpObj , {icon:iconMole});
	map.addOverlay(marker);
	marker.openInfoWindowHtml(info);
	if (icon != '') {marker.setImage(icon);}
//	map.openInfoWindow(gpObj,info);
}

function SetMapAddress(lang,address,title,xabstract,ximage,mapid,icon,iconwidth,iconheight) {
	map = new GMap2(document.getElementById(mapid));
	geocoder = new GClientGeocoder();
	map.addControl(new GLargeMapControl());
	map.addControl(new GMapTypeControl());

	//  ======== Add a map overview ==========
	//  ======== save a reference to the control =========
	var ovcontrol = new GOverviewMapControl(new GSize(160,160));
	map.addControl(ovcontrol);

	//  ======== A function to adjust the positioning of the overview ========
	function positionOverview(x,y) {
		var omap=document.getElementById("map_overview");
		omap.style.left = x+"px";
		omap.style.top = y+"px";

		// == restyling ==
		omap.firstChild.style.border = "1px solid gray";

		omap.firstChild.firstChild.style.left="4px";
		omap.firstChild.firstChild.style.top="4px";
		omap.firstChild.firstChild.style.width="150px";
		omap.firstChild.firstChild.style.height="150px";
	}

	if (lang != "") {
		geocoder.setBaseCountryCode(lang);
	}
	info = '<h3>'+title+'</h3><table width="400px"><tr><td>'+xabstract+'</td>';
	if (ximage != '') {
		info = info+'<td><img class="img" src="/public/data/'+ximage+'" alt="'+title+'" /></td>';
	}
	info = info + '</tr></table>';
	
	// ³]©wGIcon
	var iconMole = new GIcon(); 
	iconMole.image = icon;
	iconMole.shadow = icon;
	if (iconwidth == 0){
	iconMole.iconSize = '';
	iconMole.shadowSize = '';
	iconMole.iconAnchor = new GPoint(0, 0);
	}
	else {
	iconMole.iconSize = new GSize(iconwidth, iconheight);
	iconMole.shadowSize = new GSize(iconwidth, iconheight);
	iconMole.iconAnchor = new GPoint(iconwidth/2, iconheight);
	}
	iconMole.infoWindowAnchor = new GPoint(0, 0);
	
	if(geocoder) {
	geocoder.getLatLng(address,
		function(latlng) {
			if(!latlng) {
				geocoder.getLocations(address,
				function(response) {
					if(!response || response.Status.code != 200) {
						window.alert(address+" not found");
					}
					else {
						var place = response.Placemark[0];
						var latlng = new GLatLng(place.Point.coordinates[1],place.Point.cooridinates[0]);
						map.setCenter(latlng,13);
						map.addOverlay(marker);
						if (icon != '') {marker.setImage(icon);}
						marker.openInfoWindowHtml(info);
					}
				}
				)
			}
				else {
				map.setCenter(latlng,13);
				var marker = new GMarker(latlng , {icon:iconMole});
				map.addOverlay(marker);
				if (icon != '') {marker.setImage(icon);}
				marker.openInfoWindowHtml(info);
			}
		}
	);
	}
}

function MoveMapCenter(type,LatLng,ZoomSize) {
	if (type == 'address') {
		if (geocoder) {
        geocoder.getLatLng(
          LatLng,
          function(point) {
            if (!point) {
              alert(LatLng + " not found");
            } else {
              map.setCenter(point, ZoomSize);
            }
          }
        );
		};
	}
	else
	{
		map.panTo(new GLatLng(type,LatLng));
		map.setZoom(ZoomSize);
	}
}

function MapSetCenter(Lat,Lng) {
	map.setZoom(13);
	map.panTo(new GLatLng(Lat,Lng));
}

function MapSetCenterByAddr(address) {
	map.setZoom(13);
	if(geocoder) {
	geocoder.getLatLng(address,
		function(latlng) {
			if(!latlng) {
				geocoder.getLocations(address,
				function(response) {
					if(!response || response.Status.code != 200) {
						window.alert(address+" not found");
					}
					else {
						var place = response.Placemark[0];
						var latlng = new GLatLng(place.Point.coordinates[1],place.Point.cooridinates[0]);
						map.panTo(latlng);
					}
				}
				)
			}
			else {
				map.panTo(latlng);
			}
		}
	);
	};
}
