//<![CDATA[

    var map;
    var myPano;   
    var panoClient;
	var panoDiv;
	var Z1;// Custom Pano
	//var detailsDiv;
    var houseMarker;
    var firstTime;
	var facing;
	var svOverlay;
	var svOn = false;
	var currentLatLng;// should retire this
	var currentPanoLatLng;
	var currentYaw = 0;// Sloane St / Albert Park Dr = 0.   Cambridge Rd = 216
	var currentPitch = 5;
	var currentPanoZoom = 0;
	var currentMapLatLng;
	var currentMapZoom = 14;
	var iconPerson;
	var icon12;
	//var introDiv;
	var messageDiv; 
	var linkDiv;
	var linkURLDiv;
	var directoryDiv;
	var overlayButtonDiv;
	var qsParm = new Array();// Query string
	var clPanoramio;
	var clWikipedia;
	var clPioneerWalk;
	//var clAerial1;
	//var m417RR;

function initialize() {
	qs();// Get query string
	if (qsParm["mco"]) {// If there's a query string
		var map_latlong = qsParm["mco"].split(",");
		var pano_latlong = qsParm["pco"].split(",");
		currentMapLatLng = new GLatLng(map_latlong[0],map_latlong[1]);// Use it
		currentPanoLatLng = new GLatLng(pano_latlong[0],pano_latlong[1]);// Use it
		currentLatLng = new GLatLng(pano_latlong[0],pano_latlong[1]);// should be retired
		if (qsParm["mz"]) { currentMapZoom = Number(qsParm["mz"]); }
		if (qsParm["pz"]) { currentPanoZoom = Number(qsParm["pz"]); }
		if (qsParm["py"]) { currentYaw = Number(qsParm["py"]); }
		if (qsParm["pp"]) { currentPitch = Number(qsParm["pp"]); }
	} else {
		//-38.012768, 175.32579 // Sloane St / Albert Park Dr
		//-38.0074,175.32984 // Cambridge Rd / Arawata St
		currentPanoLatLng = new GLatLng(-38.012768, 175.32579); // Else use default
		currentMapLatLng = new GLatLng(-38.012768, 175.32579);
		currentLatLng = new GLatLng(-38.012768, 175.32579);// Needs to be retired, but currently keeps loadup correct.
	}
	svOverlay = new GStreetviewOverlay();
	panoClient = new GStreetviewClient();      
	map = new GMap2(document.getElementById("map_canvas"), {mapTypes: [G_NORMAL_MAP, G_SATELLITE_MAP, G_HYBRID_MAP]});//, {minResolution:10}
	//map.setUIToDefault();
	//var extLargeMapControl = new ExtLargeMapControl();

	G_NORMAL_MAP.getMinimumResolution = function(){return 6}; 
	G_SATELLITE_MAP.getMinimumResolution = function(){return 6};
	G_HYBRID_MAP.getMinimumResolution = function(){return 6};
	map.setCenter(currentMapLatLng, currentMapZoom);
	var topLeft = new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(7,35));
	map.addControl(new GLargeMapControl(), topLeft);// Default control
	//map.addControl(extLargeMapControl, topLeft);// Custom Control
	map.addControl(new GMapTypeControl());
	map.addControl(new GOverviewMapControl());
	map.enableGoogleBar();
	var lsc = new google.maps.LocalSearch(); 
	/*var searchOptions = {
		searchFormHint : "Enter street address",
		searchFormTitle : "Hello",
		resultList : google.maps.LocalSearch.RESULT_LIST_SUPPRESS, // Suppress results list
		suppressInitialResultSelection : true,
		onMarkersSetCallback : searchMarkersSet
      };
	map.addControl(new google.maps.LocalSearch(searchOptions), new GControlPosition(G_ANCHOR_BOTTOM_LEFT, new GSize(80,0)));*/

	
		GEvent.addListener(map, "moveend", function() {// Listen for map movement
			linkDiv.style.visibility = "hidden";// Hide Cutom URL
		});
		GEvent.addListener(map, "zoomend", function() {
			if(map.getZoom() > 14) { // Limit zoom
				toggleMarkers('on');// show markers
			} else {
				toggleMarkers('off');
			}
		});
		makeMarkers();

      houseMarker = new GMarker(currentPanoLatLng, {title:'You are here', draggable: true, icon:iconPerson});
      map.addOverlay(houseMarker); 
	//alert(houseMarker.getLatLng());
      //houseMarker.openInfoWindowHtml("Drag me to angle the panorama towards a potential building location.");
		GEvent.addListener(houseMarker, "dragstart", function() {
			houseMarker.setImage("/town/2/images/icons/iconMe_N.png");
		});
		GEvent.addListener(houseMarker, "dragend", function() {
			var cLatLng = houseMarker.getLatLng();
			panoClient.getNearestPanorama(cLatLng, showPanoData);
			onLocChange(cLatLng);
			pointMe(currentYaw);
		});
		GEvent.addListener(houseMarker,"dblclick", function() {
			currentMapZoom = map.getZoom()+1;
			map.setCenter(currentMapLatLng, currentMapZoom);
		});
      
		panoDiv = document.getElementById("pano");
		myPano = new GStreetviewPanorama(panoDiv);
		//panoClient.getNearestPanorama(houseMarker.getLatLng(), showPanoData);
		panoClient.getNearestPanorama(currentPanoLatLng, showPanoData);
		// Listen for new POV
		GEvent.addListener(myPano,"initialized",function(a) {// Currently only works when moving from one pano to the next
			onLocChange(a.latlng);
			//alert("initialized");
		});
		GEvent.addListener(myPano, "yawchanged", onPOVChange);
		GEvent.addListener(myPano, "pitchchanged", onPOVChange);
		GEvent.addListener(myPano, "zoomchanged", onPOVChange);
		
		Z1 = document.getElementById("location");
		Z1.style.visibility="hidden";
		//detailsDiv = document.getElementById("details");
		//makeMarkers();
		// Intro
		//introDiv = document.getElementById("intro");
		//introDiv.style.visibility = "visible";

		messageDiv = document.getElementById("Message");
		linkDiv = document.getElementById("linkDiv");
		linkURLDiv = document.getElementById("linkURLDiv");
		makeLines();
		// Additional Layers
		clPanoramio = new GLayer("com.panoramio.all");
		clPanoramio.added = false;
		clWikipedia = new GLayer("org.wikipedia.en");
		clWikipedia.added = false;

		directoryDiv = document.getElementById("map_menu");
		overlayButtonDiv = document.getElementById("map_cp");

		// ground overlay
		var boundaries1 = new GLatLngBounds(new GLatLng(-38.011985, 175.32298), new GLatLng(-38.00763, 175.32845));
		//clAerial1 = new GGroundOverlay("/town/2/images/towncentre1-1854.jpg", boundaries1);
		//map.addOverlay(clAerial1);
		//clAerial1.added = false;
		GEvent.addListener(map, "maptypechanged", onMapTypeChange);// Listen for maptype change
    }
	
// Query String
// Should include: lat, lng, y (yaw), p (pitch), z (zoom)
function qs() {
	//var url = window.location.toString();
	//var vtaQuery = (url.substring(url.lastIndexOf("vta")+3));// Try to extract vta string
	//if (vtaQuery.length>3) { var query = vtaQuery; }// If there is one, use it
	//else { var query = window.location.search.substring(1); }// Else try for a standard query string
	var query = window.location.search.substring(1);
	var parms = query.split('&');
	for (var i=0; i<parms.length; i++) {
		var pos = parms[i].indexOf('=');
		if (pos > 0) {
			var key = parms[i].substring(0,pos);
			var val = parms[i].substring(pos+1);
			qsParm[key] = val;
		}
	}
}

function showPanoData(panoData) {
   if (panoData.code == 200) {
		//messageDiv.style.visibility = "hidden";// could be redundant
		messageDiv.innerHTML = '';// could be redundant
		} else {
        messageDiv.style.visibility = "visible";
		messageDiv.innerHTML = 'Sorry, that location isn\'t currently available';    
		return;
      }
      var angle = computeAngle(houseMarker.getLatLng(), panoData.location.latlng);
      if (firstTime == 1) {// This naming is backwards really
       // myPano.setLocationAndPOV(panoData.location.latlng, {yaw: angle});// original
		myPano.setLocationAndPOV(panoData.location.latlng, {yaw: currentYaw});//{pitch: yaw: zoom: viewHeight:} 
      } else {
        myPano.setLocationAndPOV(panoData.location.latlng, {pitch: currentPitch, yaw: currentYaw, zoom:currentPanoZoom});// First view
		if (!qsParm["mco"]) {// If there's a query string, don't show the intro message
			messageDiv.style.visibility = "visible";
			messageDiv.innerHTML = '<div style="margin-bottom:10px;color:#000000;font-size:14px;font-weight:bold;">Welcome to Virtual Te Awamutu.</div><div style="margin-bottom:7px;color:#000000;font-size:13px;">See below for instructions and help.</div><div style="color:#0000EE;font-size:12px;text-decoration:underline;cursor:pointer;">Hide this message</div>';
		}
        firstTime = 1;
      }
//myXX = document.getElementById("dropmenu1");
//myXX.style.zIndex=10000;
    }

    function computeAngle(endLatLng, startLatLng) {
     var DEGREE_PER_RADIAN = 57.2957795;
     var RADIAN_PER_DEGREE = 0.017453;// Original

      var dlat = endLatLng.lat() - startLatLng.lat();
      var dlng = endLatLng.lng() - startLatLng.lng();
      // We multiply dlng with cos(endLat), since the two points are very closeby,
      // so we assume their cos values are approximately equal.
      var yaw = Math.atan2(dlng * Math.cos(endLatLng.lat() * RADIAN_PER_DEGREE), dlat)
             * DEGREE_PER_RADIAN;
      return wrapAngle(yaw);
   }

   function wrapAngle(angle) {
    if (angle >= 360) {
      angle -= 360;
    } else if (angle < 0) {
     angle += 360;
    }
    return angle;
  };

//var loc0001 = "-38.00866, 175.32105";

function menuGoTo(lat, lng, yaw, pitch, zo) {
	//panoDiv.style.visibility = "hidden";
	currentLatLng = new GLatLng(lat, lng);
	currentMapLatLng = new GLatLng(lat, lng);
	currentPanoLatLng = new GLatLng(lat, lng);
	//myPano.retarget(panoDiv);// Workaround - retarget is an undocumented method to remove the current panorama and reset the container. This clears the pano to prevent image jumping
	tempFix();
	myPano.setLocationAndPOV(currentPanoLatLng, {yaw: yaw, pitch: pitch});
	//map.setCenter(currentLatLng, zo);// Use this version to set zoom
	map.setCenter(currentMapLatLng);// or this version to leave zoom as it is
	onLocChange(currentPanoLatLng);
	//houseMarker.setLatLng(currentLatLng);
	//panoDiv.style.visibility = "visible";
}

// When location or POV Changes: http://econym2.googlepages.com/temp_streetviewpanorama3.htm
function onLocChange(a) {
	messageDiv.innerHTML = '';
	//GLog.write('LatLng: ' + a);
	currentLatLng = a;
	currentMapLatLng = a;
	currentPanoLatLng = a;
	onPOVChange();
	houseMarker.setLatLng(a);
	//panoDiv.style.visibility = "visible";// Move to a later point when initialization but has been fixed by Google
	//alert("onLocChange");
	messageDiv.style.visibility = "hidden";
}
function onPOVChange() {
	var pov = myPano.getPOV();
	currentYaw = Math.round(pov.yaw);
	if (currentYaw > 360) { currentYaw -= 360; }// Bugfix: Correct for angles greater than 360 degrees
	//detailsDiv.innerHTML = '<table cellpadding="0" cellspacing="0" align="right"><tr><td>' + sepCoords(currentLatLng) + '</td><td width="10">&nbsp;</td><td>Facing: ' + getCompass(currentYaw) + ' (' + currentYaw + '&deg;)<br>Pitch: ' + Math.round(pov.pitch) + ', Zoom: ' + Math.round(pov.zoom) + '</td></tr></table>';
	pointMe(currentYaw);
	linkDiv.style.visibility = "hidden";// Hide Cutom URL
}

function getCompass(yaw) {// get compass heading for yaw value
	var hg = 'N';// Heading - North by default
	if (yaw > 22 && yaw < 68) { hg = 'NE'; }
	else if (yaw >= 68 && yaw < 112) { hg = 'E'; }
	else if (yaw >= 112 && yaw < 157) { hg = 'SE'; }
	else if (yaw >= 157 && yaw < 202) { hg = 'S'; }
	else if (yaw >= 202 && yaw < 247) { hg = 'SW'; }
	else if (yaw >= 247 && yaw < 292) { hg = 'W'; }
	else if (yaw >= 292 && yaw < 337) { hg = 'NW'; }
	return hg;
}

function sepCoords(latlng) {
	var nz_coords = latlng.toString().replace('(', '');
	nz_coords = nz_coords.replace(')', '');
	var nz_coords_array = nz_coords.split(', ');
	var nz_coord_lat = nz_coords_array[0];// 5 decimal places (for navcontrols)
	var nz_coord_lon = nz_coords_array[1];
//	var nz_coord_lat2 = roundNumber(nz_coords_array[0], 2);// 2 decimal places (for HUD)
//	var nz_coord_lon2 = roundNumber(nz_coords_array[1], 2);
	var newCoords = 'Latitude:&nbsp;&nbsp; '+nz_coord_lat+'<br>Longitude: '+nz_coord_lon;
	return newCoords;
}

// Street View overlay
function svOverlayToggle() {
	if (svOn == true) {
		map.removeOverlay(svOverlay);
		document.optionsForm.svOverlay.checked = false;
		svOn = false;
	} else {
		map.addOverlay(svOverlay);
		document.optionsForm.svOverlay.checked = true;
		svOn = true;
	}
}

// Toggle Custom Panorama
function toggleLoc(loc_title, loc_file){
//loc_title = 'Rose Gardens';
//loc_file = '20070212-rosegardens-2880x360.jpg';
		//if (Z1.style.visibility=="hidden") {
			Z1.style.visibility="visible";
			Z1.style.display="block";
			// Write the location HTML:
			Z1.innerHTML = '<div><object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" width="1000" height="310" id="panorama" align="middle"><param name="allowScriptAccess" value="sameDomain" /><param name="movie" value="/town/2/panorama.swf?nz_file='+loc_file+'&nz_title='+loc_title+'" /><param name="quality" value="high" /><param name="bgcolor" value="#ffffff" /><embed src="/town/2/panorama.swf?nz_file='+loc_file+'&nz_title='+loc_title+'" quality="high" bgcolor="#ffffff" width="1000" height="310" name="panorama" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" /></object></div>';
			//}
		//else {
			//Z1.style.visibility="hidden";
			//Z1.style.display="none";
			//Z1.innerHTML = "";
			//}
}
function closeLoc(){
	Z1.style.visibility="hidden";
	Z1.style.display="none";
	Z1.innerHTML = "";
}

// Custom Markers

function makeMarkers() {
	iconPerson = new GIcon();
	iconPerson.image = "/town/2/images/icons/iconMe_N.png";
	iconPerson.shadow = "";
	iconPerson.iconSize = new GSize(50, 50);
	iconPerson.shadowSize = new GSize(0, 0);
	iconPerson.iconAnchor = new GPoint(25, 35);
	//iconPerson.infoWindowAnchor = new GPoint(5, 8);

	icon12 = new GIcon();
	icon12.image = "/town/2/images/loc12.png";
	icon12.shadow = "";
	icon12.iconSize = new GSize(12, 20);
	icon12.shadowSize = new GSize(0, 0);
	icon12.iconAnchor = new GPoint(6, 20);
	//icon12.infoWindowAnchor = new GPoint(5, 8);

	markerAnchorPark = createMarker(-38.00486,175.30702, 'Anchor Park','2009-02-07_anchorpark_2880x310.jpg');
	markerASBpark = createMarker(-38.00279,175.316395, 'ASB Park','2009-02-07_asbpark_2880x310.jpg');
	markerCentennialReserve = createMarker(-38.01201, 175.31245, 'Centennial Reserve','2009-02-07_centennialreserve_2880x310.jpg');
	markerRosegarden = createMarker(-38.0079, 175.32803, 'Rose Gardens','20070212-rosegardens_2880x360.jpg');
	markerMempark = createMarker(-38.00446, 175.32422, 'War Memorial Park','20070217-warmemorialpark_2880x292.jpg');
	markerMemparkField = createMarker(-38.00425, 175.32532, 'Park Field','2009-02-07_memorialpark-field_2880x310.jpg');
	markerMemparkPioneer = createMarker(-38.00401, 175.32338, 'Pioneer Walk','2009-02-08_mempark-pioneer_2880x310.jpg');
	markerSunkenCross = createMarker(-38.00474, 175.32517, 'Sunken Cross','2009-02-08_sunkenscross_2880x310.jpg');
	markerSculpturePark = createMarker(-38.00852,175.33093, 'Sculpture Park','2009-02-08_sculpturepark_2880x310.jpg');
	markerTrainStation = createMarker(-38.00835, 175.30978, 'Train Station','2009-02-08_trainstation_2880x310.jpg');
	markerVictoriaPark = createMarker(-38.01167, 175.3199, 'Victoria Park','2009-02-08_victoriapark_2880x310.jpg');
	//m417RR = createMarker(-38.000100, 175.317240, 'Dave and Ange\'s House','417rr2.jpg');

	var mm = new GMarkerManager(map); 
	mm.addMarker(markerAnchorPark,15,17); 
	mm.addMarker(markerASBpark,15,17);
	mm.addMarker(markerCentennialReserve,15,17);
	mm.addMarker(markerRosegarden,13,17);
	mm.addMarker(markerMempark,14,17);
	mm.addMarker(markerMemparkField,16,17);
	mm.addMarker(markerMemparkPioneer,16,17);
	mm.addMarker(markerSunkenCross,16,17);
	mm.addMarker(markerSculpturePark,15,17);
	mm.addMarker(markerTrainStation,16,17);
	mm.addMarker(markerVictoriaPark,15,17);
	mm.refresh();
}



function createMarker(lat, lng, title, img) {
	var point1 = new GLatLng(lat, lng);
	var marker = new GMarker(point1, {title:title, icon:icon12});
	img = '/town/2/images/panos/' + img;
	GEvent.addListener(marker, "click", function() {
		toggleLoc(title,img);
	});
	return marker;
}

function toggleMarkers(onoff) {
	if (onoff == 'on') {
		// Turn on
	} else {
		// Turn off
	}
}


function pointMe(yaw) {// Point the person marker in the right direction
	var hg = 'N';// Heading - North by default
	if (yaw >= 11.25 && yaw < 33.75) { hg = 'NNE'; }
	else if (yaw >= 33.75 && yaw < 56.25) { hg = 'NE'; }
	else if (yaw >= 56.25 && yaw < 78.75) { hg = 'ENE'; }
	else if (yaw >= 78.75 && yaw < 101.25) { hg = 'E'; }
	else if (yaw >= 101.25 && yaw < 123.75) { hg = 'ESE'; }
	else if (yaw >= 123.75 && yaw < 146.25) { hg = 'SE'; }
	else if (yaw >= 146.25 && yaw < 168.75) { hg = 'SSE'; }
	else if (yaw >= 168.75 && yaw < 191.25) { hg = 'S'; }
	else if (yaw >= 191.25 && yaw < 213.75) { hg = 'SSW'; }
	else if (yaw >= 213.75 && yaw < 236.25) { hg = 'SW'; }
	else if (yaw >= 236.25 && yaw < 258.75) { hg = 'WSW'; }
	else if (yaw >= 258.75 && yaw < 281.25) { hg = 'W'; }
	else if (yaw >= 281.25 && yaw < 303.75) { hg = 'WNW'; }
	else if (yaw >= 303.75 && yaw < 326.25) { hg = 'NW'; }
	else if (yaw >= 326.25 && yaw < 348.75) { hg = 'NNW'; }
	houseMarker.setImage("/town/2/images/icons/iconMe_" + hg + ".png");
}


// TOURS & PATHS
//var polyline;
function makeLines() {
	clPioneerWalk = new GPolyline([// Pioneer Walk
	new GLatLng(-38.008998, 175.329857),
	new GLatLng(-38.00878, 175.32943),
	new GLatLng(-38.00838, 175.32927),
	new GLatLng(-38.00815, 175.32905),
	new GLatLng(-38.00801, 175.32888),
	new GLatLng(-38.00757, 175.32847),
	new GLatLng(-38.00714, 175.32768),
	new GLatLng(-38.0068, 175.32599),
	new GLatLng(-38.00612, 175.32526),
	new GLatLng(-38.00552, 175.3253),
	new GLatLng(-38.00478, 175.32576),
	new GLatLng(-38.00473, 175.32559),
	new GLatLng(-38.00411, 175.32606),
	new GLatLng(-38.00365, 175.32488),
	new GLatLng(-38.004, 175.32397),
	new GLatLng(-38.00408, 175.32359),
	new GLatLng(-38.00394, 175.32314),
	new GLatLng(-38.0037, 175.32303),// bridge east
	new GLatLng(-38.00371, 175.32278),// bridge west
	new GLatLng(-38.00332, 175.32268),
	new GLatLng(-38.00267, 175.3215),
	new GLatLng(-38.00269, 175.3214),
	new GLatLng(-38.0031, 175.32094),
	new GLatLng(-38.00324, 175.31965),
	new GLatLng(-38.003718, 175.318767)
	], "#0000ff", 5);
	//map.addOverlay(clPioneerWalk);
	//clPioneerWalk.hide();
	clPioneerWalk.added = false;
}
/*function toggleLine() {
	if (polyline.isHidden() == true) {
		polyline.show();
		document.optionsForm.pioneer.checked = true;
	} else {
		polyline.hide();
		document.optionsForm.pioneer.checked = false;
	}
}*/

function toggleCustomLayer(button, layer) {
	if (layer.added == false) {// If it's the first time
		map.addOverlay(layer);// Load it
		layer.added = true;
		button.checked = true;
	} else {// show/hide
		if (layer.isHidden()) {
			layer.show();
			button.checked = true;
		} else {
			layer.hide();
			button.checked = false;
		}
	}
}



function tempFix() {// Fix until the initialization bug is sorted by google.
		var pov = myPano.getPOV();
		var currentYaw = pov.yaw;
//alert('1: '+currentYaw);
		var currentPitch = pov.pitch;
		myPano.remove();
		myPano = new GStreetviewPanorama(panoDiv, {yaw:0});
//alert('2: '+currentYaw);
		GEvent.addListener(myPano,"initialized",function(a) {// Currently only works when moving from one pano to the next
			onLocChange(a.latlng);
			//alert("initialized");
		});
		GEvent.addListener(myPano, "yawchanged", onPOVChange);
		GEvent.addListener(myPano, "pitchchanged", onPOVChange);
		GEvent.addListener(myPano, "zoomchanged", onPOVChange);
}

/*function toggleIntro() {
	if (introDiv.style.visibility == "visible") {
		introDiv.style.visibility = "hidden";
		introDiv.style.display="none";
		document.optionsForm.showIntro.checked = false;
	} else {
		introDiv.style.visibility = "visible";
		introDiv.style.display="block";
		document.optionsForm.showIntro.checked = true;
	}
}*/

// Directory
function toggleDirectory(thisBtn) {
	if (directoryDiv.style.visibility == "visible") {
		directoryDiv.style.visibility = "hidden";
		//thisBtn.innerHTML = "Show Directory";
	} else {
		directoryDiv.style.visibility = "visible";
		overlayButtonDiv.style.visibility = "hidden";
		linkDiv.style.visibility = "hidden";
		//thisBtn.innerHTML = "Hide Directory";
	}
}

function toggleOverlayMenu() {
	if (overlayButtonDiv.style.visibility == "visible") {
		overlayButtonDiv.style.visibility = "hidden";
	} else {
		overlayButtonDiv.style.visibility = "visible";
		directoryDiv.style.visibility = "hidden";
		linkDiv.style.visibility = "hidden";
	}
}

// Make custom link currentLatLng
function customLink() {
	if (linkDiv.style.visibility != "visible") {
		var linkurl = "http://www.teawamutu.co.nz/vta?";// Base URL for dynamic links
		var clink_mco = map.getCenter().toString().replace(/\(/,"");// Tidy up map coords
		var clink_mco = clink_mco.replace(/\)/,"");
		var clink_mco = clink_mco.replace(/, /,",");
		var clink_mco1 = roundNumber(Number(clink_mco.substring(0, clink_mco.indexOf(","))), 5);// Round each coord number
		var clink_mco2 = roundNumber(Number(clink_mco.substring(1+clink_mco.indexOf(","))), 5);
		var clink_pco = currentPanoLatLng.toString().replace(/\(/,"");// Tidy up map coords
		var clink_pco = clink_pco.replace(/\)/,"");
		var clink_pco = clink_pco.replace(/, /,",");
		var clink_pco1 = roundNumber(Number(clink_pco.substring(0, clink_pco.indexOf(","))), 5);
		var clink_pco2 = roundNumber(Number(clink_pco.substring(1+clink_pco.indexOf(","))), 5);
		var pov = myPano.getPOV();
		currentPitch = Math.round(pov.pitch);
		linkurl += "mco="+clink_mco1+","+clink_mco2;
		linkurl += "&mz="+map.getZoom();
		linkurl += "&pco="+clink_pco1+","+clink_pco2;
		linkurl += "&pz="+Math.round(pov.zoom);
		linkurl += "&py="+currentYaw;
		linkurl += "&pp="+currentPitch;
		linkDiv.style.visibility = "visible";
		//linkURLDiv.innerHTML = linkurl;
		document.getElementById("linkURLBox").value = linkurl;
		directoryDiv.style.visibility = "hidden";
		overlayButtonDiv.style.visibility = "hidden";
	} else {
		linkDiv.style.visibility = "hidden";
	}
}

// SEARCH
function searchMarkersSet(markers) { // note: markers is an array of LocalResult
  if (markers.length >= 1) {
	for (var i=0; i<markers.length; i++) {
		markers[i].marker.remove();
	}
	//var cLatLng = houseMarker.getLatLng();
	var cLatLng = map.getCenter();
	panoClient.getNearestPanorama(cLatLng, showPanoData);
	onLocChange(cLatLng);
	pointMe(currentYaw);
  }
}

function resetPegman() {
	//var cLatLng = map.getCenter();
	//panoClient.getNearestPanorama(cLatLng, showPanoData);
	houseMarker.setLatLng(map.getCenter());
}

function onMapTypeChange() {
	messageDiv.style.visibility = "hidden";
// NEEDS TO BE CORRECTLY RETIRED:
//	var cMapType = map.getCurrentMapType().getName(false);
//	if (clAerial1.added == false) {// If it's the first time
//		map.addOverlay(clAerial1);// Load it
//		clAerial1.added = true;
//	}
//	if (cMapType == "Satellite" || cMapType == "Hybrid") {
//		clAerial1.show();
//	} else {
//		clAerial1.hide();
//	}
}


// Round numbers
function roundNumber(rnum, rlength) {
	if (rnum > 8191 && rnum < 10485) {
		rnum = rnum-5000;
		var newnumber = Math.round(rnum*Math.pow(10,rlength))/Math.pow(10,rlength);
		newnumber = newnumber+5000;
	} else {
		var newnumber = Math.round(rnum*Math.pow(10,rlength))/Math.pow(10,rlength);
	}
	return newnumber;
}
// End Round numbers

//]]>