var map;
var xMarker = null;
var nearbyMarkers = [];
var nearbyType = "";
var baseIcon = new GIcon();
baseIcon.iconSize = new GSize(32, 32);
baseIcon.iconAnchor = new GPoint(4, 31);
baseIcon.infoWindowAnchor = new GPoint(9, 2);

var serviceIcon = new GIcon();
serviceIcon.iconSize = new GSize(16, 16);
serviceIcon.iconAnchor = new GPoint(14, 16);
serviceIcon.infoWindowAnchor = new GPoint(9, 2);

var nbIcon = new GIcon();
nbIcon.iconSize = new GSize(14, 30);
nbIcon.iconAnchor = new GPoint(0, 30);
nbIcon.infoWindowAnchor = new GPoint(9, 2);

var xIcon = new GIcon();
xIcon.iconSize = new GSize(24, 24);
xIcon.iconAnchor = new GPoint(12, 12);
var crossIcon = new GIcon(xIcon);
crossIcon.image = "/img/cross.png";

function load() {
	if (GBrowserIsCompatible()) {
		
		map = new GMap2(document.getElementById("moreinfomap"));
		map.addMapType(G_PHYSICAL_MAP);
		map.setCenter(new GLatLng(markerLat, markerLng), 15);

		map.addControl(new GLargeMapControl());
		map.addControl(new GMapTypeControl());
		map.enableContinuousZoom();
		map.enableScrollWheelZoom();

		var publisherID = 'ca-pub-4002245024903759';
		var adsManagerOptions = {
			maxAdsOnMap : 1,
			style: 'adunit',
			channel: '5626973176'
		};
		var adsManager = new GAdsManager(map, publisherID, adsManagerOptions);
		adsManager.enable();

		var alttext = "Blue Flag";
		var icon = "/img/blueflag.png";
		//if (markerType > 7) {
		//	icon = "/img/" + alttext + ".gif";
		//} else {
		//	icon = "/img/" + alttext + "Pin.png";
		//}
		var LatLng = new GLatLng(markerLat, markerLng);
		var marker = new createMarker(LatLng, "<div class='info_name'>" + markerName + "</div>", icon, alttext);  
		map.addOverlay(marker);
	}
}

function plak_sv_close() {
	document.getElementById("plak_sv_container").style.display = "none";
}

function plak_sv_show() {
	var sv = new GLatLng(markerLat, markerLng);
	panoramaOptions = { latlng:sv };
	var myPano = new GStreetviewPanorama(document.getElementById("sv_image"), panoramaOptions);
	document.getElementById("plak_sv_container").style.display = "block";
}

function createMarker(point, text, ico, alttext) {
	var letteredIcon = new GIcon(baseIcon);
	//if (markerType > 7) {
	//	letteredIcon = new GIcon(serviceIcon);
	//} else {
	//	letteredIcon = new GIcon(baseIcon);
	//}
	letteredIcon.image = ico;

	markerOptions = { icon:letteredIcon };
	var marker = new GMarker(point, markerOptions);

	GEvent.addListener(marker, "click", function() {
		marker.openInfoWindowHtml("<div class='info_div'><img src='" + ico + "' class='info_icon' alt='" + alttext + "'/> " + text + "</div>");
	});
	return marker;
}

function getPinColor (value) {
	var text;
	var type = parseInt(value);
	switch(type) {
		case 1:
			text = "Gray";
			break;
		case 2:
			text = "Blue";
			break;
		case 3:
			text = "Red";
			break;
		case 4:
			text = "Pink";
			break;
		case 5:
			text = "Purple";
			break;
		case 6:
			text = "Green";
			break;
		case 7:
			text = "Yellow";
			break;
		case 8:
			text = "saps";
			break;
		case 9:
			text = "school";
			break;
		case 10:
			text = "hospital";
			break;
		case 11:
			text = "other";
			break;
	}
	return text;
}

function createNBMarker(point, text, ico, alttext, type, id) {
	var letteredIcon;
	if (type > 7) {
		letteredIcon = new GIcon(serviceIcon);
	} else {
		letteredIcon = new GIcon(nbIcon);
	}
	letteredIcon.image = ico;

	markerOptions = { icon:letteredIcon };
	var marker = new GMarker(point, markerOptions);

	GEvent.addListener(marker, "click", function() {
		marker.openInfoWindowHtml("<div class='info_div'><img src='" + ico + "' class='info_icon' alt='" + alttext + "'/><div class='info_name'> " + text + "</div><br/><div style='text-align: center;'><input class='plak_button' type='button' value='More info...' onclick=\"document.location.href='/moreinfo.php?id=" + id + "'\"/></div></div>");
	});

	GEvent.addListener(marker, "mouseover", function(latlng) {
		showName(text, latlng, type);
	});

	GEvent.addListener(marker, "mouseout", function() {
		hideName();
	});

	return marker;
}

function getNearby(type) {
	if (type == 1) {
		nearbyType = "place";
	} else {
		nearbyType = "service";
	}
	document.getElementById("nearby_button").disabled = true;
	document.getElementById("nearby_button1").disabled = true;
	if (nearbyMarkers.length > 0) {
		for (var n = 0; n < nearbyMarkers.length; n++) {
			map.removeOverlay(nearbyMarkers[n]);
		}
		nearbyMarkers = [];
	}
	AJAXCall("getNearby", encodeURI("lat=" + markerLat + "&lng=" + markerLng + "&id=" + markerID + "&type=" + type), "gotNearby(rep)");
}

function gotNearby(obj) {
	if (obj[0][0] == "none") {
		alert("Sorry. No "+ nearbyType + "s were found in the vicinity of this Plak.");
	} else {
		var marker;
		for (var n = 0; n < obj.length; n++) {
			var LatLng = new GLatLng(obj[n][0], obj[n][1]);
			var pin = getPinColor(obj[n][4]);
			var icon;
			if (obj[n][4] > 7) {
				icon = "/img/" + pin + ".gif";
			} else {
				icon = "/img/" + pin + "Pin.png";
			}
			marker = new createNBMarker(LatLng, obj[n][2], icon, obj[n][2], obj[n][4], obj[n][3]);
			map.addOverlay(marker);
			nearbyMarkers.push(marker);
		}
		if (obj.length == 1) {
			alert(obj.length + " " + nearbyType + " was found in the vicinity of this Plak. You might need to zoom out to it.");
		} else {
			alert(obj.length + " " + nearbyType + "s were found in the vicinity of this Plak. You might need to zoom out to see all of them.");
		}
	}
	document.getElementById("nearby_button").disabled = false;
	document.getElementById("nearby_button1").disabled = false;
}

function showName(name, latlng, type) {
	var div = document.createElement("div");
	div.id = "infopop";
	div.style.position = "relative";
	div.style.whiteSpace = "nowrap";
	div.innerHTML = name;
	if (type > 7) {
		div.style.top = (map.fromLatLngToDivPixel(latlng).y - 41) + 'px';
		div.style.left = (map.fromLatLngToDivPixel(latlng).x - 13) + 'px';
	} else {
		div.style.top = (map.fromLatLngToDivPixel(latlng).y - 54) + 'px';
		div.style.left = (map.fromLatLngToDivPixel(latlng).x + 8) + 'px';
	}
	map.getPane(G_MAP_FLOAT_PANE).appendChild(div);
}

function hideName() {
	var infopop = document.getElementById("infopop");
	if(infopop) {
		map.getPane(G_MAP_FLOAT_PANE).removeChild(infopop);
	}
}

function showImage(e, src) {
	var posx = 0;
	var posy = 0;
	if (e.pageX || e.pageY)     {
		posx = e.pageX;
		posy = e.pageY;
	} else if (e.clientX || e.clientY)     {
		posx = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
		posy = e.clientY + document.body.scrollTop + document.documentElement.scrollTop;
	}
	posx += 16;
	posy += 16;
	var imagediv = document.getElementById("imagediv");
	imagediv.style.left = posx + "px";
	imagediv.style.top = posy + "px";
	imagediv.innerHTML = "<img src='" + src + "' style='max-width: 300px;' alt='' />";
	imagediv.style.display = "block";
}

function hideImage() {
	document.getElementById("imagediv").style.display = "none";
}

function overResult(xLat, xLng) {
	if (xMarker != null) {
		map.removeOverlay(xMarker);
	}
	var point = new GPoint(xLng, xLat);
	var markerOptions = { icon:crossIcon };
	xMarker = new GMarker(point, markerOptions);

	map.addOverlay(xMarker);
}