
/*
----------------------------------------------------------------------------
/////// GoogleMap script by Colin at Ecomcorp
----------------------------------------------------------------------------
*/

var marker;
var marker2;
var overlayInstance = null;
var map;
var client;
var lastMarkerLocation;
var panorama;
var mapT;
var svBoolean;
var latlng;
var guyIcon;

var map_lat;
var map_long;
var map_width;
var map_height;
var map_title;
var mouse_zoom;
var listing_name;
var logo_img;

var loc_lng;
var loc_lat;
var loc_pov;
var loc_title;
var loc_id;

var smallNode;
var window_title;


// -------------------------------------------------------
// Add Google Map
// -------------------------------------------------------
function GoogleMap(map_div, map_lat, map_long, map_width, map_height, map_title, mouse_zoom, listing_name, logo_img)
{	
	this.map_div      = map_div;
	this.map_lat      = map_lat;
	this.map_long     = map_long;
	this.map_width    = map_width;
	this.map_height   = map_height;
	this.map_title    = map_title;
	this.mouse_zoom   = mouse_zoom;
	this.listing_name = listing_name;
	this.logo_img     = logo_img;

	mapLoad();
}


// -------------------------------------------------------
// Load Google Map
// -------------------------------------------------------
function mapLoad() 
{
	// New objects
	client = new GStreetviewClient();
	latlng = new GLatLng(map_lat, map_long);
	map    = new GMap2(document.getElementById(map_div));
	
	// Setup map
	lastMarkerLocation = latlng;

	map.setCenter(lastMarkerLocation, 17);
	map.enableContinuousZoom();
	map.addControl(new GLargeMapControl3D());
	map.addControl(new GMapTypeControl()); 
	//map.addControl(new GOverviewMapControl(new GSize(100,75)));
	map.removeMapType(G_HYBRID_MAP);                              
	map.getMapTypes()[0].getMinimumResolution = function() {return 5;}            

	if(mouse_zoom)
	{
		map.enableScrollWheelZoom();
	}

	// Setup Street View icon
	guyIcon = new GIcon(G_DEFAULT_ICON);
	guyIcon.image = "http://maps.google.com/intl/en_us/mapfiles/cb/man_arrow-0.png";
	guyIcon.transparent = "http://maps.google.com/intl/en_us/mapfiles/cb/man-pick.png";
	guyIcon.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];

	guyIcon.iconSize         = new GSize(49, 52);
	guyIcon.iconAnchor       = new GPoint(25, 35); // near base of guy's feet
	guyIcon.infoWindowAnchor = new GPoint(25, 0);  // top of guy's head

	// Geo marker
	marker = new GMarker(latlng, {draggable: false});
	map.addOverlay(marker);
	GEvent.addListener(marker, "click", infoWindow);
	
	// Check to get initial Street View boolean value, then open the info window
	client.getNearestPanorama(latlng, initResponse);
}


// -------------------------------------------------------
// Open info window
// -------------------------------------------------------
function infoWindow()
{
	lastMarkerLocation = new GLatLng(map_lat, map_long);
	
	var smallNode2 = document.createElement('div');
	
	smallNode2.style.backgroundColor = '#FFF';
	smallNode2.style.fontSize = '12px';
	smallNode2.style.overflow = 'hidden';

	// Setup window content
	// -------------------------------------------------------

	// Check for logo image
	if(logo_img)
	{
		var info_logo = '<div style="display:block;"><img src="'+logo_img+'" style="max-height:40px;" border="0" alt="'+listing_name+'" /></div>';
		smallNode2.style.width  = '250px';
		smallNode2.style.height = '150px';
	}
	else
	{
		var info_logo = '';	
		smallNode2.style.width  = '200px';
		smallNode2.style.height = '110px';
	}
	
	// Setup content vars
	var info_content = info_logo;
	info_content += '<p>'+map_title+'</p>';
	
	// Check for Street View
	switch(svBoolean)
	{
		case true:
			var svthumb = '<img class="img_border" src="http://cbk0.google.com/cbk?output=thumbnail&w=90&h=68&ll='+map_lat+','+map_long+'" style="float:left;width:90px;height:68px;" border="0" />';	
			break;
		case false:
			// Maybe add in a default balnk logo here later
			var svthumb = '<img class="img_border" src="http://cbk0.google.com/cbk?output=thumbnail&w=90&h=68&ll='+map_lat+','+map_long+'" style="float:left;width:90px;height:68px;" border="0" />';	
			break;
	}

	info_content += '<div style="display:block;margin:5px 0 5px 0;"><a class="option" href="javascript:;" onclick="openStreetView();" title="Open Street View">'+svthumb+'</a><div style="float:left;"><a href="javascript:;" onclick="openStreetView();" title="Open Street View"><img style="display:block;" src="http://maps.google.com/intl/en_us/mapfiles/cb/man_arrow-5.png" border="0" /></a><div style="display:block;margin:0 0 0 10px;"><a class="option" href="javascript:;" onclick="openStreetView();" title="Open Street View">Street View</a></div></div></div>';

	smallNode2.innerHTML = info_content;
	
	marker.openInfoWindowHtml(smallNode2);
}


// -------------------------------------------------------
// Handle Flash check error
// -------------------------------------------------------
function handleNoFlash(errorCode) 
{
	if (errorCode == 603) 
	{
		smallNode.style.width  = '300px';
		smallNode.style.height = '100px';
		smallNode.innerHTML    = '<h4>Flash player upgrade.</h4><p>To use street view, you need Adobe Flash Player version 9 or newer. Click <a href="http://get.adobe.com/flashplayer/" target="_blank">here</a> to get the latest Flash Player from Adobe.com.</p>';
		window_title = 'Flash player upgrade required';
		
		marker2.openInfoWindow(smallNode, {pixelOffset: "0,0", maxContent: contentNode, maxTitle: window_title});
	}
}


// -------------------------------------------------------
// Open Panorama bubble
// -------------------------------------------------------
function openPanoramaBubble() 
{
	var contentNode = document.createElement('div');
	
	contentNode.style.textAlign = 'center';
	contentNode.style.width     = (map_width - 45) + 'px';
	contentNode.style.height    = (map_height - 50) + 'px';
	contentNode.style.overflow  = 'hidden';
	contentNode.innerHTML       = '';
	
	smallNode = document.createElement('div');
	
	panorama = new GStreetviewPanorama(smallNode);
	
	smallNode.style.overflow = 'hidden';
	smallNode.id             = 'pano';
	
	smallNode.innerHTML = '';
	
	// Check for correct Flash version
	GEvent.addListener(panorama, "error", handleNoFlash);

	switch(svBoolean)
	{
		case true:	
		default:
			smallNode.style.width  = '300px';
			smallNode.style.height = '200px';
			smallNode.innerHTML    = '';
			window_title = '';
			break;
		case false:
			smallNode.style.width  = '300px';
			smallNode.style.height = '120px';
			smallNode.innerHTML    = '<h4>No street view available</h4><p>Drag the orange person icon to the nearest street with a street view.</p><p>A street that has a street view enabled will show a blue outline while the icon is being dragged.</p>';
			window_title = 'No Street View';
			break;
	}
	
	marker2.openInfoWindow(smallNode, {pixelOffset: "0,0", maxContent: contentNode, maxTitle: window_title});
	
	panorama.setLocationAndPOV(marker2.getLatLng(), null);
	
	GEvent.addListener(panorama, "newpano", onNewLocation);
	GEvent.addListener(panorama, "yawchanged", onYawChange); 
	
	var iw = map.getInfoWindow();
	
	GEvent.addListener(iw, "maximizeend", function() 
	{
		panorama.setContainer(contentNode);  
		window.setTimeout("panorama.checkResize()", 5);
	});
}


// -------------------------------------------------------
// Open new Street View icon
// -------------------------------------------------------
function openStreetView()
{
	// Remove the marker if it already exists
	if(marker2)
	{
		map.removeOverlay(marker2);
	}
	
	// Add new marker instance
	marker2 = new GMarker(lastMarkerLocation, {icon: guyIcon, draggable: true, bouncy: true, zIndexProcess: zIndexSwap, dragCrossMove: true});
	map.addOverlay(marker2);
	GEvent.addListener(marker2, "dragend", onDragEnd);
	GEvent.addListener(marker2, "click", openStreetView);
	GEvent.addListener(marker2, "dragstart", showOverlay);
	
	client.getNearestPanorama(lastMarkerLocation, svResponse);
}


// -------------------------------------------------------
// Swap Icon depth
// -------------------------------------------------------
function zIndexSwap()
{
	return GOverlay.getZIndex(map_lat)+10000;
}


// -------------------------------------------------------
// Toggle the blue Street View overlay
// -------------------------------------------------------
function toggleOverlay() 
{
	if (!overlayInstance) 
	{
		overlayInstance = new GStreetviewOverlay();
		map.addOverlay(overlayInstance);
	} 
	else 
	{
		map.removeOverlay(overlayInstance);
		overlayInstance = null;
	}
}


// -------------------------------------------------------
// Show the blue Street View overlay
// -------------------------------------------------------
function showOverlay() 
{
	if (!overlayInstance) 
	{
		overlayInstance = new GStreetviewOverlay();
		map.addOverlay(overlayInstance);
	}
}


// -------------------------------------------------------
// Set the change in yaw
// -------------------------------------------------------
function onYawChange(newYaw) 
{
	var GUY_NUM_ICONS = 16;
	var GUY_ANGULAR_RES = 360/GUY_NUM_ICONS;
	if (newYaw < 0) 
	{
		newYaw += 360;
	}
	guyImageNum = Math.round(newYaw/GUY_ANGULAR_RES) % GUY_NUM_ICONS;
	guyImageUrl = "http://maps.google.com/intl/en_us/mapfiles/cb/man_arrow-" + guyImageNum + ".png";
	marker2.setImage(guyImageUrl);
}


// -------------------------------------------------------
// Set new coords for marker
// -------------------------------------------------------
function onNewLocation(lat, lng) 
{
	latlng = new GLatLng(lat, lng);
	marker2.setLatLng(latlng);
}


// -------------------------------------------------------
// On Drag completion
// -------------------------------------------------------
function onDragEnd() 
{
	latlng = marker2.getLatLng();
	map.removeOverlay(overlayInstance);
	overlayInstance = null;
	
	if(!client.getNearestPanorama(latlng, dragResponse))
	{
		// If no nearest panorama found
		dragResponse();
	}
}


// -------------------------------------------------------
// On Init response
// -------------------------------------------------------
function initResponse(response) 
{
	if(response != null)
	{
		// If failure(no Street View available)...
		if (response.code != 200)
		{
			//...set Street View to false
			svBoolean = false;
		} 
		else 
		{
			svBoolean = true;
		}
	}
	else
	{
		//...set Street View to false
		svBoolean = false;	
	}
	
	// Open the info window
	infoWindow();
}


// -------------------------------------------------------
// On Init response
// -------------------------------------------------------
function svResponse(response) 
{
	if(response != null)
	{
		// If failure(no Street View available)...
		if (response.code != 200)
		{
			//...set Street View to false
			svBoolean = false;
			openPanoramaBubble();
		} 
		else 
		{
			//...otherwise set new marker location data
			loc_lng   = response.location.lng;
			loc_lat   = response.location.lat;
			loc_pov   = response.location.pov;
			loc_title = response.location.description;
			loc_id    = response.location.panoId;

			latlng = new GLatLng(loc_lat, loc_lng);
			marker2.setLatLng(latlng);
			lastMarkerLocation = latlng;
			svBoolean = true;
			openPanoramaBubble();
		}
	}
	else
	{
		//...set Street View to false
		svBoolean = false;	
	}
}


// -------------------------------------------------------
// On Drag response
// -------------------------------------------------------
function dragResponse(response) 
{
	if(response != null)
	{
		// If failure(no Street View available)...
		if (response.code != 200)
		{
			//...go back to last marker location
			marker2.setLatLng(lastMarkerLocation);
		} 
		else 
		{
			//...otherwise set new marker location data
			loc_lng   = response.location.lng;
			loc_lat   = response.location.lat;
			loc_pov   = response.location.pov;
			loc_title = response.location.description;
			loc_id    = response.location.panoId;
			
			latlng = new GLatLng(loc_lat, loc_lng);
			marker2.setLatLng(latlng);
			lastMarkerLocation = latlng;
			svBoolean = true;
			openPanoramaBubble();
		}
	}
	else
	{
		//...set Street View to false
		svBoolean = false;	
	}
}

