if (window.addEventListener) {
	window.addEventListener("load", setupMap, false);
	window.addEventListener("unload", GUnload, false)
} else {
	if (window.attachEvent) {
		window.attachEvent("onload", setupMap);
		window.attachEvent("unload", GUnload)
	} else {
		window.onload = setupMap;
		window.unload = GUnload
	}
}

var map;
var locPanorama;
var panoClient;
var yellowManMarker;
var destMarker;
var lastMarkerLocation;
var overlayInstance = null;
var lastPosX;
var divMapCanvas;
var divPanorama;
var locationPOV;
var streetViewFlg = null;

function setupMap() {
	if (GBrowserIsCompatible()) {
	  if (lat != '0' && lng != '0') {
		if(document.getElementById("map_canvas")){
			divMapCanvas = document.getElementById("map_canvas");
			divMapCanvas.style.width = "602px";
			divMapCanvas.style.height = "301px";

			if(document.getElementById("panorama")){
				divPanorama = document.getElementById("panorama");
				divPanorama.style.width = "602px";
				divPanorama.style.height = "301px";

				if (vlat == '0' && vlng == '0') {
					divPanorama.style.display = "none";
				}

				panoClient = new GStreetviewClient();
				locPanorama = new GStreetviewPanorama(document.getElementById("panorama"));
			}

			map = new GMap2(document.getElementById("map_canvas"));
			var b = new GLatLng(lat, lng);
			var l = new GLatLng(vlat, vlng);
			var f;
			var g;
			if (!f) {
				f = -20
			} else {
				f = parseFloat(f)
			}
			locationPOV = {
					yaw: vyaw,
					pitch: vpitch,
					zoom: 0
			};
			lastMarkerLocation = l;
			map.setCenter(b, 18);
			var c = new GIcon(G_DEFAULT_ICON);
			var d = new GIcon(G_DEFAULT_ICON);
			d.image = "http://maps.gstatic.com/mapfiles/cb/man_arrow-0.png";
			d.transparent = "http://maps.gstatic.com/mapfiles/cb/man-pick.png";
			d.imageMap = [26, 13, 30, 14, 32, 28, 27, 28, 28, 36, 18, 35, 18, 27, 16, 26, 16, 20, 16, 14, 19, 13, 22, 8];
			d.iconSize = new GSize(49, 52);
			d.iconAnchor = new GPoint(25, 35);
			d.infoWindowAnchor = new GPoint(25, 5);

			destMarker = new GMarker(b, {
				icon: c
			});
			map.addOverlay(destMarker);

			if(panoClient){
				yellowManMarker = new GMarker(l, {
					icon: d,
					draggable: true,
					bouncy: true,
					bounceGravity: 0.5
				});

				map.addOverlay(yellowManMarker);

				panoClient.getNearestPanorama(l, showPano);
				GEvent.addListener(locPanorama, "initialized", onNewLocation);
				GEvent.addListener(locPanorama, "yawchanged", onYawChange);
				GEvent.addListener(locPanorama, "error", handleNoFlash);
				/*GEvent.addListener(destMarker, "click", function () {
					map.openInfoWindowHtml(destMarker.getLatLng(), "")
				});
				*/
				GEvent.addListener(yellowManMarker, "dragstart", function () {
					lastPosX = map.fromLatLngToContainerPixel(yellowManMarker.getLatLng()).x;
					iconSrc = "http://maps.gstatic.com/mapfiles/cb/man_fly_left.png";
					yellowManMarker.setImage(iconSrc)
				});
				GEvent.addListener(yellowManMarker, "drag", function () {
					var i;
					i = map.fromLatLngToContainerPixel(yellowManMarker.getLatLng()).x;
					if (i != lastPosX) {
						iconSrc = (i > lastPosX) ? "http://maps.gstatic.com/mapfiles/cb/man_fly_right.png" : "http://maps.gstatic.com/mapfiles/cb/man_fly_left.png";
						yellowManMarker.setImage(iconSrc);
						lastPosX = i
					}
				});
				GEvent.addListener(yellowManMarker, "dragend", onDragEnd);
			}

			// map.addControl(new GLargeMapControl());
			// map.addControl(new GScaleControl());
			// map.addControl(new GMapTypeControl());
			var customUI = map.getDefaultUI();
			customUI.controls.menumaptypecontrol = true;
			map.setUI(customUI);
			//overlayInstance = new GStreetviewOverlay();
			//map.addOverlay(overlayInstance);
		}
	  }else{ // no map
		document.getElementById("gmap").style.display = "none";
	  }
	}
}

function showPano(a) {
	if (a.code != 200) {
		yellowManMarker.setLatLng(lastMarkerLocation)
	} else {
		divPanorama.style.display = ""
			yellowManMarker.setLatLng(a.Location.latlng);
		lastMarkerLocation = a.Location.latlng;
		locPanorama.setLocationAndPOV(a.Location.latlng, locationPOV)
	}
}
function onYawChange(b) {
	var a = 16;
	var c = 360 / a;
	if (b < 0) {
		b += 360
	}
	guyImageNum = Math.round(b / c) % a;
	guyImageUrl = "http://maps.gstatic.com/mapfiles/cb/man_arrow-" + guyImageNum + ".png";
	yellowManMarker.setImage(guyImageUrl)
}
function onNewLocation(a) {
	yellowManMarker.setLatLng(a.latlng)
}
function onDragEnd() {
	yellowManMarker.setImage(onYawChange(locPanorama.getPOV().yaw));
	var a = yellowManMarker.getLatLng();
	if (locPanorama) {
		panoClient.getNearestPanorama(a, showPano)
	}
}
function handleNoFlash(a) {
	if (a == 603) {
		alert("Error: Flash doesn't appear to be supported by your browser");
		return
	}
};
