var geocoder;
var viewClient;
var map;
var view;
var venues;
var fullControls;

function load(zoom, hasFullControls) {
	fullControls = hasFullControls;
  if (GBrowserIsCompatible()) {
    geocoder = new GClientGeocoder();
    viewClient = new GStreetviewClient(); 
    map = new GMap2(document.getElementById("map"));
    view = new GStreetviewPanorama(document.getElementById("view"));  

    if( fullControls ) {
    	map.addControl(new GLargeMapControl());	
    	map.addControl(new GMapTypeControl());
    }else{
    	map.addControl(new GSmallMapControl());	
    }    
 
 		var center = new GLatLng( 39.31058, -76.61629 );
		map.setCenter( center, zoom );
    populateMap();
  }
}

function populateMap() {
	showAddress(39.27640, -76.67130, "701 S Canton Ave, Baltimore MD", "Channel One / Club Exodus", null);
	showAddress(39.31097, -76.61769, "31 W North Ave, Baltimore MD", "Club 31", null);
	showAddress(39.31884, -76.61957, "2549 N Howard St, Baltimore MD", "Club Midnight / The Karaoke Bar", null);
	showAddress(39.30107, -76.61743, "1001 Cathedral St, Baltimore MD", "Girard's", null, true);
	showAddress(39.30933, -76.61644, "1717 N Charles St, Baltimore MD", "Godfrey's Famous Ballroom", null, true);
	showAddress(39.28937, -76.62912, "9 N Fremont Ave, Baltimore MD", "Harbor City Hall", null, true);
	showAddress(39.29457, -76.62098, "425 Eutaw St, Baltimore MD", "Jule's Loft", "the-loft");
	showAddress(39.28775, -76.64463, "1724 Frederick Ave, Baltimore MD", "The New New Loft", "the-loft");
	showAddress(39.34049, -76.60931, "4201 York Rd, Baltimore MD", "Mansion Theater", null);    	
  showAddress(39.29510, -76.62045, "306 W Franklin St, Baltimore MD", "Marble Bar", "marble-bar");
	showAddress(39.27680, -76.63398, "1433 W Hamburg St, Baltimore MD", "Memory Lane", "memory-lane");
	showAddress(39.28553, -76.59450, "1627 Eastern Ave, Baltimore MD", "Polish National Alliance Hall", null);
	showAddress(39.28645, -76.59322, "403 S Broadway, Baltimore MD", "Reptillian Records", "reptillian", true);
	showAddress(39.31025, -76.61808, "1818 Maryland Ave, Baltimore MD", "The Rev", "the-rev", true);
	showAddress(39.29154, -76.61170, "203 Davis St, Baltimore MD", "Talking Head Club", "talking-head-club", true);
	showAddress(39.29415, -76.62120, "406 Eutaw St, Baltimore MD", "Terminal 406", null, true);	
}

function showView(){
	document.getElementById("view").setAttribute("style", "visibility: visible");
}

function hideView(){
	document.getElementById("view").setAttribute("style", "visibility: hidden");
}

function showAddress(lat, long, address, label, tag, disableView) {
	var point = new GLatLng(lat, long);
	var marker = new GMarker(point);
	map.addOverlay(marker);
	GEvent.addListener(marker, "click", function() {
		if( fullControls ){
				var inner = "<b>" + label + "</b><br /><i>" + address + "</i>";
     	if( tag != null ){
     		inner += "<p><a href='http://www.deadvenues.com/blog/?tag=" + tag + "'>More Information</a></p>";
     	}
     	marker.openInfoWindowHtml( inner );
     	
     	if(disableView){
     		hideView();
     		toStatus("No Street View Available");	
     	}else{
	     	var getAngle = function( panoData ) {
	     		clearStatus();
	     		showView();
	     		showPanoData( marker, panoData );
	     	};
	     	viewClient.getNearestPanorama(marker.getLatLng(), getAngle);
     	}
		}
   });
}

function showPanoData(marker, panoData) {
	if (panoData.code != 200) {
	  	toStatus("Error Loading Street View");
	  	return;
	}
	
	var angle = computeAngle(marker.getLatLng(), panoData.location.latlng);
	view.setLocationAndPOV(panoData.location.latlng, {yaw: angle});
}
	
function computeAngle(endLatLng, startLatLng) {
	var DEGREE_PER_RADIAN = 57.2957795;
	var RADIAN_PER_DEGREE = 0.017453;
	
	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;
};

function toStatus( message ) {
  document.getElementById("status").innerHTML = message;	
}

function clearStatus( ) {
  document.getElementById("status").innerHTML = "";	
}