var svOverlay;
var streetview;
var streetviewClient;
var svmarker;
var svcur_loc;
var mapclick_ev;
var currentYaw = 0;
var currentPitch = 0;
var timer;
function streetviewOpen(svdata){
	if ((svdata) && svdata.code == 200) {
		var zoom = map.getZoom();
		if (zoom < 15) {
			zoom = 15;
		}		
		latlng = svdata.location.latlng;
		map.setCenter(latlng,zoom);			
		svmarker.setLatLng(latlng);		
		svcur_loc = latlng;
		svmarker.show();
		infolatlng = new GLatLng((latlng.lat()+.0005),(latlng.lng()+.0003));
		map.openInfoWindowHtml(infolatlng,'<div id="svcontainer"></div>');
//			map.openInfoWindowHtml(infolatlng,\'<div id="svcontainer"></div>\',{maxContent:\'<div id="svcontainermax">testing...</div>\'});
		streetview = new GStreetviewPanorama(document.getElementById("svcontainer"));	
/*
		GEvent.addListener(map.getInfoWindow(), "maximizeclick", function() { 
//				alert(document.getElementById("svcontainermax").innerHTML);
			if (document.getElementById("svcontrols")) {
				alert("found");
			}
			streetview.setContainer(map.getInfoWindow());
		});
*/
		// realign map to streetview location
		GEvent.addListener(streetview, "initialized", function(location) { 
		//	alert(location.latlng);
			infowindow = map.getInfoWindow();
//				alert(infowindow);
			infowindow.enableMaximize();
			maplatlng = new GLatLng((location.latlng.lat()+.0045),location.latlng.lng());
			map.setCenter(maplatlng);
			infolatlng = new GLatLng((location.latlng.lat()+.0005),(location.latlng.lng()+.0003));
			infowindow.reset(infolatlng,null,new GSize(600,300));
			svmarker.setLatLng(location.latlng);
			svcur_loc = location.latlng;
		});				
		streetview.setLocationAndPOV(svdata.location.latlng);			
	} else {
		svmarker.setLatLng(svcur_loc);
		map.openInfoWindowHtml(svcur_loc,'<div id="svcontainer" style="width:auto;height:auto;">Drag me onto or click on a blue-outlined path to open streetview.</div>');
	}
}

function handleNoFlash(errorCode) {
  if (errorCode == 603) {
	alert("Error: Flash doesn't appear to be supported by your browser");
	return;
  }
} 

function createMarker(point, description) {
  var marker = new GMarker(point);
  
  GEvent.addListener(marker, 'click', function() {
	marker.openInfoWindowHtml(description);
//	map.zoomTo(3);
  });

  return marker;
}

// use icons other than the default ones
function createMarkerCustom(point, description, icon) {
  var marker = new GMarker(point,{icon:icon});
  
  GEvent.addListener(marker, 'click', function() {
	marker.openInfoWindowHtml(description);
//	map.zoomTo(3);
  });

  return marker;
}

// other functions
function svconsole() {
	var svactions = document.getElementById("svactions");
	var svcontrols = document.getElementById("svcontrols");		
	if (svactions.className == 'show') {
		svactions.className = 'hide';
		svcontrols.className = '';	
		map.removeOverlay(svOverlay);
		map.closeInfoWindow();
		svmarker.hide();
		GEvent.removeListener(mapclick_ev);
	} else {
		svactions.className = 'show';		
		svcontrols.className = 'expand';
		svOverlay = new GStreetviewOverlay();			
		map.addOverlay(svOverlay);

		var banner_img = document.getElementById('banner_image');
		var map_img_path = banner_img.src.replace(/img\/layout/i,'img/map').replace(/\/[^\/]*?.png/i,'');

		var tigericon = new GIcon();
		tigericon.image = map_img_path+'/tiger2.png';
		tigericon.shadow = "http://www.google.com/mapfiles/shadow50.png";
		tigericon.iconSize = new GSize(29, 34);
		tigericon.shadowSize = new GSize(37, 34);
		tigericon.iconAnchor = new GPoint(6, 20);
		tigericon.infoWindowAnchor = new GPoint(5, 1);

		// streetview functions
		 svmarker = new GMarker(map.getCenter(), {icon:tigericon,draggable:true,autoPan:true,dragCrossMove:true});
		 map.addOverlay(svmarker);
		//	svmarker.openInfoWindowHtml(\'<div id="svcontainer" style="width:auto;height:auto;">Drag me onto or click on a blue-outlined path to open streetview.</div>\');
		var mapcenter = map.getCenter();
		infolatlng = new GLatLng((mapcenter.lat()+.0005),(mapcenter.lng()+.0003));		
		map.openInfoWindowHtml(infolatlng,'<div id="svcontainer" style="width:auto;height:auto;">Drag me onto or click on a blue-outlined path to open streetview.</div>');
		svcur_loc = svmarker.getLatLng();	 
		streetview = new GStreetviewPanorama(document.getElementById("svcontainer"));	
		streetviewClient = new GStreetviewClient();
		
		/* events */
		GEvent.addListener(streetview, "error", handleNoFlash);
		
		// map click
		mapclick_ev = GEvent.addListener(map, "click", function(overlay,latlng) {
			streetviewClient.getNearestPanorama(latlng,streetviewOpen);
		});
		// icon drag
		GEvent.addListener(svmarker, "dragend", function() {
			latlng = svmarker.getLatLng();
			streetviewClient.getNearestPanorama(latlng,streetviewOpen);
		});			
		svcontrols.style.background = 'url(../img/map/white_trans70.png)';
		//			svcontrols.style.font = 'bold';
		//			alert(svcontrols.style.background);
		}
} // end svconsole

function animate0() {
	pov = streetview.getPOV();
	currentYaw = pov.yaw;	
	streetview.followLink(currentYaw);
}
function animatestart0() { 
	clearInterval(timer);
	timer = window.setInterval(animate0, 3000);
}
function animate180() { 
	pov = streetview.getPOV();
	currentYaw = Math.abs(pov.yaw - 180); 
	streetview.followLink(currentYaw);
}
function animatestart180() { 
	clearInterval(timer);
	timer = window.setInterval(animate180, 3000);
}
function rotate() { 
	currentYaw += .5;
	streetview.panTo({yaw:currentYaw, pitch:currentPitch});
}
function rotatestart() { 
	clearInterval(timer);
	pov = streetview.getPOV();
	currentPitch = pov.pitch;
	currentYaw = pov.yaw;
	timer = window.setInterval(rotate, 50);
}
function animatestop() { 
	clearInterval(timer);
}
function recenter(point, zoom, origin_desc) {
	map.setCenter(point, zoom);	
	map.openInfoWindowHtml(point,origin_desc);
}