//<![CDATA[

var map = null; var bOK = true;
var address = ""; var web; var sid;

function showAddress(iaddress, iweb, isid) {

if (boxVisible("map")) { toggleBox("map"); }
else {
  if (map != null) { map.getInfoWindow().hide(); toggleBox("map"); map.returnToSavedPosition(); } 
  else if (!bOK) { alert('Unable to map: ' + address); } 
  else {
	toggleBox("map");
	address = iaddress; web = iweb; sid = isid;
	google.load("maps", "2.x", { callback: drawMap, "other_params": "sensor=false" });
  }
}
}

function drawMap()
{
	includeCode("text/javascript", "/include/extmaptypecontrol_packed.js", null);
	includeCode("text/javascript", "/include/dragzoom_packed.js", null);
	includeCode("text/javascript", "/include/contextmenucontrol_packed.js", null);
}

function loadGmap() {
	bOK = GBrowserIsCompatible();
	if (bOK) {

	geocoder = new GClientGeocoder();
	geocoder.getLocations(address, function addAddressToMap(response) 
	{
	  bOK = (response && response.Status.code == 200);
	  if (bOK) 
	  {
	    map = new GMap2(document.getElementById("map"), { googleBarOptions: {style: 'new', adsOptions: { client: 'partner-pub-8127310465817130'}}} );
		map.removeMapType(G_HYBRID_MAP);
		map.addMapType(G_PHYSICAL_MAP);
		map.addControl(new GOverviewMapControl()); map.addControl(new GSmallMapControl()); 
		var pos = new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(50,10));
		map.addControl(new GScaleControl(), pos);

		// Add ContextMenuControl to the map without 'What's here?' item
		var opts = { whatsHere: false };
		map.addControl(new ContextMenuControl(opts));

		// first set of options is for the visual overlay.
		var boxStyleOpts = {opacity: .2, border: "2px solid red"}
		// second set of options is for everything else
		var otherOpts = {buttonHTML: "<img src='/images/zoom-button.gif' />", buttonZoomingHTML: "<img src='/images/zoom-button-activated.gif' />", buttonStartingStyle: {width: '24px', height: '24px'},
		  backButtonHTML: '<img title="Zoom Back Out" src="/images/zoomout.gif">',  backButtonStyle: {display:'none',marginTop:'5px',width:'25px', height:'23px'}, backButtonEnabled: true, overlayRemoveTime: 1500};
		map.addControl(new DragZoomControl(boxStyleOpts, otherOpts, {}));
		map.addControl(new ExtMapTypeControl({showTraffic: true, showTrafficKey: true}));
		
		place = response.Placemark[0]; point = new GLatLng(place.Point.coordinates[1], place.Point.coordinates[0]);
		map.setCenter(point, 13);

		map.enableScrollWheelZoom(); map.enableDoubleClickZoom(); map.enableContinuousZoom();
		map.enableGoogleBar();

		// map.addOverlay(new GLayer("com.panoramio.all"));
		map.addOverlay(new GLayer("org.wikipedia"));

		var marker = new GMarker(point);
		map.addOverlay(marker);

		var adPos = new GControlPosition(G_ANCHOR_BOTTOM_RIGHT, new GSize(0, 125));
		adsManagerOptions = { maxAdsOnMap: 2, style: 'adunit', channel: '4773234828', position: adPos};
		var adsManager = new GAdsManager(map, 'ca-pub-8127310465817130', adsManagerOptions); 
		adsManager.enable();

		area = place.AddressDetails.Country.AdministrativeArea;
		if (area) {subArea = area.SubAdministrativeArea;}
		if (subArea) { locality = subArea.Locality;} else if (area) { locality = area.Locality; }
		if (locality) { thoroughfare = locality.Thoroughfare; postalcode = locality.PostalCode; } else if (subArea) { thoroughfare = subArea.Thoroughfare; postalcode = subArea.PostalCode; } else if (area) { thoroughfare = area.ThoroughFare; postalcode = area.PostalCode; }

		if (web != "") { web = '<a target="_blank" href="' + web + '" onClick="trackClick(' + sid + ', 0)"><b>' + web.toUpperCase() + '</b></a><br>'; }
		if (thoroughfare) {web = web + thoroughfare.ThoroughfareName + ' ';}
		web = web + '<a href="http://maps.google.com/maps?daddr={' + escape(place.address) + '}"><b>Directions</b></a><br>'
		if (locality) {web = web + locality.LocalityName;} if (subArea) { if (locality) {web = web + ', ';} web = web + subArea.SubAdministrativeAreaName; }
		if (locality || subArea) {web = web + ', ';}
		if (area) {web = web + area.AdministrativeAreaName + ' ';} if (postalcode) {web = web + postalcode.PostalCodeNumber;}
		if (area || postalCode) {web = web + ', ';}
		web = web + place.AddressDetails.Country.CountryNameCode;

		GEvent.addListener(marker, "click", function() { marker.openInfoWindowHtml(web); });
	  } else { toggleBox("map"); window.status = 'Warning: Geocoding failed, Unable to map: ' + address; }
	}
	);
	} else { toggleBox("map"); window.status = 'Warning: GBrowserIsCompatible failed, Unable to map: ' + address; }
}

function unloadGMap() {
	if (address != "") {GUnload();}
}

//]]>