function createIcon(contextPath) {
	var icon = new GIcon();
	icon.image = contextPath + "/static/images/gicon.png";
	icon.shadow = contextPath + "/static/images/gicon_shadow.png";
	icon.iconSize = new GSize(12, 20);
	icon.shadowSize = new GSize(22, 20);
	icon.iconAnchor = new GPoint(6, 20);
	icon.infoWindowAnchor = new GPoint(5, 1);
	return icon;
}

function createIconSmall(contextPath) {
	var icon = new GIcon();
	icon.image = contextPath + "/static/images/gicon_small.png";
	icon.shadow = contextPath + "/static/images/gicon_shadow_small.png";
	icon.iconSize = new GSize(9, 15);
	icon.shadowSize = new GSize(16.5, 15);
	icon.iconAnchor = new GPoint(4.5, 15);
	icon.infoWindowAnchor = new GPoint(5, 1);
	return icon;
}

function addAddress(address, level, geocoder, f_createMarker) {
	if (address.coordinateFrom != null && address.coordinateFrom != "") {
		map.addOverlay(f_createMarker(new GPoint(address.longitude, address.latitude), address));
		return;
	}
	
	if ((level == 1 && address.address3 == "") ||
		(level == 2 && address.address2 == "")) {
		addAddress(address, level+1, geocoder, f_createMarker);
		return;
	}
	  			
	switch (level) {
		case 1:
			var fullAddress = address.address1 + ", " + 
							  address.address2 + ", " + 
							  address.address3 + ", " + 
							  address.city.name + ", " + 
							  address.city.country.code;
			break;
		case 2:
			var fullAddress = address.address1 + ", " + 
							  address.address2 + ", " + 
							  address.city.name + ", " + 
							  address.city.country.code;
			break;
		case 3:
			var fullAddress = address.address1 + ", " + 
							  address.city.name + ", " + 
							  address.city.country.code;
			break;
		case 4:
			var fullAddress = address.city.name + ", " + 
							  address.city.country.code;
			break;
		case 5:
			var fullAddress = address.city.country.code;
			break;
		default:
			return;
	}
	
	geocoder.getLatLng(
		fullAddress,
		function(point) {
			if (!point) {
				addAddress(address, level+1, geocoder, f_createMarker);
			} else {
				map.addOverlay(f_createMarker(point, address));
				addGeocode(address.unid, point.x, point.y);
			}
		}
	);
}

function initializeHomeMap(contextPath, data) {
	GEvent.addListener(map, "click", function(marker, point) {
		if (point != null)
			onClickMap(point.x, point.y);
	});

	var geocoder = new GClientGeocoder();
	var icon = createIconSmall(contextPath);
	for (var i = 0; i < data.length; i++) {
		addAddress(data[i], 1, geocoder, function(point) {
			var marker = new GMarker(point, icon);
			GEvent.addListener(marker, "click", function() {
				onClickMap(point.x, point.y);
			});
			return marker;
		});
	}
}

function initializeSearchMap(contextPath, data, lng, lat) {
	map.setCenter(new GLatLng(lat, lng), 2);

	var geocoder = new GClientGeocoder();
	var icon = createIconSmall(contextPath);
	for (var i = 0; i < data.length; i++) {
		addAddress(data[i], 1, geocoder, function(point, address) {
			var marker = new GMarker(point, icon);
			GEvent.addListener(marker, "click", function() {
				map.setCenter(new GLatLng(point.y, point.x));
				marker.openInfoWindowHtml("<div id='infoWindow'>Please wait...</div>");
				onClickMapMarker(address.unid);
			});
			return marker;
		});
	}
}

function initializeAccommodationMap(contextPath, lng, lat) {
	map.removeMapType(G_SATELLITE_MAP);
	map.addMapType(G_PHYSICAL_MAP);
	map.addControl(new GSmallMapControl());
	
	var point = new GLatLng(lat, lng);
	var icon = createIcon(contextPath);
	map.setCenter(point, 11);
	map.addOverlay(new GMarker(point, icon));
}

function populateInfoWindow() {
	var infoWindow = document.getElementById("infoWindow");
	var infoWindowContent = document.getElementById("infoWindowContent");
	infoWindow.innerHTML = infoWindowContent.innerHTML;
}