var zoom = new Array();
    zoom["AUU"] = 16;
    zoom["AXU"] = 16;
    zoom["SUU"] = 15;
    zoom["LUU"] = 14;
    zoom["ZUU"] = 13;
    zoom["MUU"] = 12;
function initMap(lat, lon, geomatch, text, latlon){
    currentLatLng = new google.maps.LatLng(lat, lon);
    latLng = latlon ? latlon : currentLatLng;
    streetViewService = new google.maps.StreetViewService;
    streetViewService.getPanoramaByLocation(latLng, 55, function(result,status){
    	myOptions = {
    			zoom: zoom[geomatch],
    			center: latLng,
    			mapTypeId: google.maps.MapTypeId.ROADMAP
    	};
    	map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
    	image = new google.maps.MarkerImage(
    		'/Gfx/icon_map_marker_30x30.png',
    		new google.maps.Size(30,30),
    		new google.maps.Point(0,0),
    		new google.maps.Point(28,2)
    	);
    	marker = new google.maps.Marker({
    		position: currentLatLng,
    		map: map,
    		icon: image
    	});
    	if(geomatch != 'AUU') marker.setVisible(false);
    	streetImage = new google.maps.MarkerImage(
    		'/Gfx/icon_street_view_32x42.png',
    		new google.maps.Size(32,42),
    		new google.maps.Point(0,0),
    		new google.maps.Point(1,42)
    	);
    	streetMarker = new google.maps.Marker({
	        draggable: true,
	        position: currentLatLng,
	        map: map,
	        icon: streetImage
	    });
	    if(geomatch != 'AUU') streetMarker.setVisible(false);
	    infowindowOptions = {content: text};
	    infowindow = new google.maps.InfoWindow(infowindowOptions);
	    google.maps.event.addListener(marker, "click", function(){
	        infowindow.open(map, marker);
	    });
	    google.maps.event.addListener(streetMarker, "dragstart", function(){
	        infowindow.close();
	    });
	    google.maps.event.addListener(streetMarker, "dragend", function(){
	    	initMap(lat, lon, geomatch, text, streetMarker.getPosition());
	    });
		google.maps.event.addListener(map, "click", function(pos){
			initMap(lat, lon, geomatch, text, pos.latLng);
			streetMarker.setPosition(pos.latLng);
			streetMarker.setVisible(true);
		});
    	if (status == google.maps.StreetViewStatus.OK && (zoom[geomatch] >= 15 || latlon != null)){
    		nearestLatLng = result.location.latLng;
    		panoramaOptions = {
				   position: nearestLatLng,
			        pov: {
			   			heading: GetBearing(nearestLatLng,currentLatLng),
			            pitch: 0,
			            zoom: 1
		   			},
			        navigationControlOptions: {
			        	position: google.maps.ControlPosition.TOP_LEFT,
			            style: google.maps.NavigationControlStyle.SMALL
			        }
    		};
			document.getElementById("pano").style.display = "block";
			document.getElementById("map_canvas").style.width = "350px";
			google.maps.event.trigger(map, 'resize');
			map.setCenter(latLng);
			streetMarker.setVisible(true);
			panorama = new  google.maps.StreetViewPanorama(document.getElementById("pano"),panoramaOptions);
			google.maps.event.addListener(panorama, "position_changed", function(loc) {
			    streetMarker.setPosition(panorama.getPosition());
			});
    	} else {
    		panoerror();
    	}
    });
}
Number.prototype.toRad = function(){
    return this * Math.PI / 180;
}
Number.prototype.toDeg = function(){
    return this * 180 / Math.PI;
}
Number.prototype.toBrng = function(){
    return (this.toDeg() + 360) % 360;
}
function GetBearing(GLatLng1, GLatLng2){
    lat1 = GLatLng1.lat();
    lon1 = GLatLng1.lng();
    lat2 = GLatLng2.lat();
    lon2 = GLatLng2.lng();
    lat1 = lat1.toRad();
    lat2 = lat2.toRad();
    dLon = (lon2 - lon1).toRad();
    y = Math.sin(dLon) * Math.cos(lat2);
    x = Math.cos(lat1) * Math.sin(lat2) - Math.sin(lat1) * Math.cos(lat2) * Math.cos(dLon);
    return Math.atan2(y, x).toBrng();
}
function panoerror(){
    document.getElementById("pano").style.display = "none";
	document.getElementById("map_canvas").style.width = "950px";
	google.maps.event.trigger(map, 'resize');
	map.setCenter(currentLatLng);
}
var panel;
YAHOO.example.panels = function(){
	panel = new YAHOO.widget.Panel("panel",
		{
			width: "977px",
			height: "560px",
			close:true,
			visible:false,
			draggable:true,
			modal:true,
			fixedcenter: "contained",
			underlay: "none",
			zindex:4,
			effect:{effect:YAHOO.widget.ContainerEffect.FADE, duration:0.2}
		}
	);
	panel.beforeHideEvent.subscribe(function(){
		document.getElementById("map_canvas").innerHTML = '';
		document.getElementById("pano").innerHTML = '';
	}); 
	panel.render();
};
YAHOO.util.Event.addListener(window,'load',YAHOO.example.panels);

function getStreetViewImage(lat, lon, geomatch, id){
	if(zoom[geomatch] >= 15){
		var loadImg = document.createElement("img");
		loadImg.src = '/Gfx/loading.gif';
		var presImg = document.getElementById('PresentationImage'+id);
		presImg.appendChild(loadImg);
		latLng = new google.maps.LatLng(lat, lon);
		streetViewService = new google.maps.StreetViewService;
		streetViewService.getPanoramaByLocation(latLng, 55, function(result,status){
			if (status == google.maps.StreetViewStatus.OK){
				nearestLatLng = result.location.latLng;
				heading = GetBearing(nearestLatLng,latLng);
				var img = document.createElement("img");
				var width = id=='' ? 640 : 420;
				var height = id=='' ? 480 : 315;
				img.src = 'http://maps.googleapis.com/maps/api/streetview?size='+width+'x'+height+'&location='+nearestLatLng.lat()+',%20'+nearestLatLng.lng()+'&fov=90&heading='+heading+'&pitch=0&sensor=false';	
				presImg.innerHTML = '';
				document.getElementById('PresentationImage'+id).appendChild(img);
			} else {
				presImg.innerHTML = '';
			}
		});
	}
}
