var map;
var bounds;
var locations = new Array();
var maxZoomLevel = 10;
var mapPadFactor = .000001;
var MapControl = 0;
var executeOnLoad = "";
var gguy_marker = null; 
var overlayInstance = null; 
var client; 
var last_gguy_marker_location;
var panorama;
var guyStartListener;
var centerLat = 0;
var centerLong = 0;
var mapCenter = null;

function clearMap(){
	bounds = new GLatLngBounds();
	map.clearOverlays();
	
	/*
	if(MapControl == 1){
		map.removeControl(MapControl);
	} */
}
function createSimpleGMap(id){
	var map = new GMap2(document.getElementById(id));

	//map.disableDragging();
	//map.disableDoubleClickZoom();
	//map.disableScrollWheelZoom();
	map.addControl(new GSmallMapControl());
	map.addControl(new GMapTypeControl());
	
	return map;
}
function createMapLocation(lat,long,addy1){
	//alert(lat[1]+"--"+long[1]);
	//var lat = 25.774846;
	//var long = -80.141909;
	var point = new GLatLng(lat,long);
	
	
	mapCenter = new GLatLng(lat,long);
	
	
	
		var marker = new GMarker(point);
	//var infoHTML = "aa";
	var infoHTML = "<font size='2' color='#333333'><br>" + addy1 + "<br>";
	//if(linkto && linkto != ""){
	//  infoHTML += "<br><a href='" + linkto + "'><font color='#AB0000'>Click to view more information</font></a></font>";
	//}
	  
	//GEvent.addListener(marker, "click", function() {
	//	marker.openInfoWindowHtml(infoHTML);
	//});
	
	GEvent.addListener(marker,"mouseover", function() {
		
		
		marker.openInfoWindowHtml(infoHTML);
  });
	
	//if(addToBounds){
	//	bounds.extend(marker.getLatLng());
	//}
//alert(marker);
	map.addOverlay(marker);
	
	if(navigator.appName == "Microsoft Internet Explorer")
	{
		var zoom = 7;
	}
	if(navigator.appName == "Netscape")
	{
		var zoom = 7;
	}
	
	if(mapCenter != null) map.setCenter(mapCenter, zoom);
	else	map.setCenter(bounds.getCenter(), zoom);

	if(bounds.length>10){
		alert("so many");
	}
}

/*function makeLocationMarkers(){
	clearMap();
	
}*/

function loadGoogleMap () { 
	if (GBrowserIsCompatible()) {
		
		map = createSimpleGMap("map");
		//client = new GStreetviewClient();
		map.setCenter(new GLatLng(25.6910530, -80.2713780),7);
		bounds = new GLatLngBounds();
		//alert(locations.length);
		
		if(locations.length > 0){
			for(var i=0;i<locations.length;i++){
		createMapLocation(locations[i].latitude,locations[i].longitude,locations[i].address1);
	}
		}else if(executeOnLoad != ""){
			eval(executeOnLoad);
		}
	}
}

function makeRegionLocationMarkers(region){
	locations = regionLocations[region];
	/*if(pointLocations){
		locations = locations.concat(pointLocations);
	}*/
	makeLocationMarkers();
	//centerMapOnBounds();
}
function get_center() {
	var bounds = map.getBounds();
	var min = bounds.getSouthWest();
	var max = bounds.getNorthEast();
	return new GLatLng((max.lat() + min.lat()) / 2 , (max.lng() + min.lng()) / 2);
}

function toggleStreetsView() {
  if (!overlayInstance) {
    overlayInstance = new GStreetviewOverlay();
    map.addOverlay(overlayInstance);
	guyStartListener = GEvent.addListener(map, "click", startGuy);
  } else {
    map.removeOverlay(overlayInstance);
    overlayInstance = null;
    if(gguy_marker){	
    	map.removeOverlay(gguy_marker);
    	gguy_marker = null;
    }
    GEvent.removeListener(guyStartListener);
  }
}

function startGuy(overlay,  latlng){
	client.getNearestPanorama(latlng, onClientResponseStartGuy);
	
}

function onGuyDrop() {
	var latlng = gguy_marker.getLatLng();
	if (panorama) {
    	client.getNearestPanorama(latlng, onClientResponse);
  	}
}

function onClientResponse(response) {
	if (response.code != 200) {
		gguy_marker.setLatLng(last_gguy_marker_location);
	} else {
		var latlng = new GLatLng(response.Location.lat, response.Location.lng);
		gguy_marker.setLatLng(latlng);
		last_gguy_marker_location = latlng;
		openPanoramaBubble();
  }
}

function onClientResponseStartGuy(response){
	if(response.code == 200){
		var latlng = new GLatLng(response.Location.lat, response.Location.lng);
		gguy_marker = get_gguy(latlng);
    	map.addOverlay(gguy_marker);
		GEvent.addListener(gguy_marker, "dragend", onGuyDrop);
		GEvent.addListener(gguy_marker, "click", openPanoramaBubble);		
		openPanoramaBubble();
		GEvent.removeListener(guyStartListener);
		
	}
	else {
		alert("It appears that you did not click on a actual street.  Only streets marked with a blue line are available for street view.  If you need help click the \"How to use street View\" button.");
	}
}

function openPanoramaBubble() {
	var contentNode = document.createElement('div');
	contentNode.style.textAlign = 'center';
	contentNode.style.width = '500px';
	contentNode.style.height = '300px';
	contentNode.innerHTML = 'Loading panorama';
	gguy_marker.openInfoWindow("<div id='pano' style='width:200px;height:200px;'></div>", {maxContent: contentNode, maxTitle: "Full screen"});

	panorama = new GStreetviewPanorama(document.getElementById("pano"));
	panorama.setLocationAndPOV(gguy_marker.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);
	});
}

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";
	gguy_marker.setImage(guyImageUrl);
}

function onNewLocation(lat, lng) {
	var latlng = new GLatLng(lat, lng);
	gguy_marker.setLatLng(latlng);
}

function setup_streets(center) {
	map.addOverlay(gguy_marker);
	enableOverlay();
}

function get_gguy(center) {
	var 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, 5);  // top of guy's head
	
	gguy = new GMarker(center, {icon: guyIcon, draggable: true});
	return gguy;
}


