var trackColor = "#f52520";
var trackWeight = 3;
var trackOpacity = 1.0;
var picDiv = document.getElementById('picDiv');


var pictureIcon = new GIcon();
pictureIcon.image = "/images/red_pin.png";
pictureIcon.iconSize = new GSize(32, 28);
pictureIcon.shadowSize = new GSize(0, 0);
pictureIcon.iconAnchor = new GPoint(12, 26);
pictureIcon.infoWindowAnchor = new GPoint(9, 7);

var activePictureIcon = new GIcon();
activePictureIcon.image = "/images/red_marker_pin_highlighted.png";
activePictureIcon.iconSize = new GSize(43, 49);
activePictureIcon.shadowSize = new GSize(0, 0);
activePictureIcon.iconAnchor = new GPoint(23, 38);
activePictureIcon.infoWindowAnchor = new GPoint(20, 19);



var trackStartIcon = new GIcon();
trackStartIcon.image = "/images/trackstart.gif";
trackStartIcon.iconSize = new GSize(21, 31);
trackStartIcon.shadowSize = new GSize(21, 31);
trackStartIcon.iconAnchor = new GPoint(9, 26);
trackStartIcon.infoWindowAnchor = new GPoint(8, 12);

var trackEndIcon = new GIcon();
trackEndIcon.image = "/images/trackend.png";
trackEndIcon.iconSize = new GSize(16, 25);
trackEndIcon.shadowSize = new GSize(21, 31);
trackEndIcon.iconAnchor = new GPoint(11, 26);
trackEndIcon.infoWindowAnchor = new GPoint(8, 12);

var trackStartEndIcon = new GIcon();
trackStartEndIcon.image = "/images/trackend.png";
trackStartEndIcon.iconSize = new GSize(16, 25);
trackStartEndIcon.shadowSize = new GSize(21, 31);
trackStartEndIcon.iconAnchor = new GPoint(11, 26);
trackStartEndIcon.infoWindowAnchor = new GPoint(8, 12);


var tripIcon = new GIcon();
tripIcon.image = "/images/red_pin.png";
tripIcon.iconSize = new GSize(32, 28);
tripIcon.shadowSize = new GSize(0, 0);
tripIcon.iconAnchor = new GPoint(12, 26);
tripIcon.infoWindowAnchor = new GPoint(9, 7);


var waypointIcon = new GIcon();
waypointIcon.image = "/images/red_pin.png";
waypointIcon.iconSize = new GSize(32, 28);
waypointIcon.shadowSize = new GSize(0, 0);
waypointIcon.iconAnchor = new GPoint(12, 26);
waypointIcon.infoWindowAnchor = new GPoint(9, 7);


var campgroundIcon = new GIcon();
campgroundIcon.image = "/images/campgroundIcon.png";
campgroundIcon.iconSize = new GSize(20, 20);
campgroundIcon.shadowSize = new GSize(20, 20);
campgroundIcon.iconAnchor = new GPoint(10, 10);
campgroundIcon.infoWindowAnchor = new GPoint(10, 10);

var trailheadIcon = new GIcon();
trailheadIcon.image = "/images/hikingIcon.png";
trailheadIcon.iconSize = new GSize(20, 20);
trailheadIcon.shadowSize = new GSize(20, 20);
trailheadIcon.iconAnchor = new GPoint(10, 10);
trailheadIcon.infoWindowAnchor = new GPoint(10, 10);
  	 

function getMapFromName(name) {
   var mapType;
   if(name == "Topo") {
      mapType = USGS_TOPO_TILES;
   } else if(name == "Aerial") {
      mapType = USGS_AERIAL_TILES;
   } else if(name == "Terrain") {
      mapType = G_PHYSICAL_MAP;
   } else if(name == "Satellite") {
      mapType = G_SATELLITE_MAP;
   } else if(name == "Map") {
      mapType = G_NORMAL_MAP;
   } else if(name == "Hybrid") {
      mapType = G_HYBRID_MAP;
   } else if(name == "Earth") {
      mapType = G_SATELLITE_3D_MAP;
   } else {
      mapType = G_PHYSICAL_MAP;
   }
   return mapType;
}

function getMapFromMapName(name) {
	if(name == "G_NORMAL_MAP") return G_NORMAL_MAP;
	if(name == "G_SATELLITE_MAP") return G_SATELLITE_MAP;
	if(name == "G_HYBRID_MAP") return G_HYBRID_MAP;
	if(name == "USGS_TOPO_TILES") return USGS_TOPO_TILES;
	if(name == "USGS_AERIAL_TILES") return USGS_AERIAL_TILES;
	if(name == "G_PHYSICAL_MAP") return G_PHYSICAL_MAP;
      
	return G_HYBRID_MAP;
}



function getMapTypeId(name) {
   var mapId;
   if(name == "Topo") {
      mapId = 5;
   } else if(name == "Aerial") {
      mapId = 6;
   } else if(name == "Satellite") {
      mapId = 1;
   } else if(name == "Map") {
      mapId = 0;
   } else if(name == "Hybrid") {
      mapId = 2;
   }  else if(name == "Terrain") {
      mapId = 3;
   }  else if(name == "Earth") {
      mapId = 4;
   } else {
      mapId =3;
   }
   return mapId;
}

function changeMapType(map,type) {
	var type = getMapFromMapName(type);
	map.setMapType(type);
}

function makeTripMarker(map,latLng, title, tripId, picture) {

	var marker = new GMarker(latLng,{title:title,icon:tripIcon});

	GEvent.addListener(marker,"click",function() {
		var html = "<div class='trip-balloon'><A href=\"/view_trip.php?trip_id="+tripId+"\">"+title+"</a><br>";
         
		if(picture && picture != "") { 
			html += "<img src="+picture+" class='trip-balloon-picture'>"; 
		}
            
  		html+= "</div>";
        map.openInfoWindow(latLng,html);
	});
	return marker;
}


function getIconByName(name) {
	switch (name) {
		case "campgroundIcon":
		return campgroundIcon;
		break;
		
		case "trailheadIcon":
		return trailheadIcon;
		break;
		
		case "default":
		return G_DEFAULT_ICON;
		break;
		
		default:
		return waypointIcon;
	}
}

function loadMap(container, mapType) {
	if (GBrowserIsCompatible()) {
       
		map = new GMap2(container);
		map.addControl(new GSmallMapControl());
		map.enableDoubleClickZoom();
 		map.enableContinuousZoom();
		map.setCenter(new GLatLng(0,0),2);
		map.setMapType(mapType);
		
		if(picDiv) {
			
			GEvent.addListener(map, "move", function() {
				removeDivOnMap(map, picDiv);
			});
			
			GEvent.addListener(map, "zoomend", function() {
				removeDivOnMap(map, picDiv);
			});
			
			
		}
	}
	return map;
}


function showDivOnMap(point, offset, map, div) {
	var px1 = map.fromLatLngToDivPixel(point);
	var bounds = map.getBounds();
	var topLeft = new GLatLng(bounds.getNorthEast().lat(), bounds.getSouthWest().lng());
	var px2 = map.fromLatLngToDivPixel(topLeft);
	var x = px1.x-px2.x;
	var y = px1.y-px2.y;
	var px = new GPoint(x,y);
	

	if(map.getBounds().containsLatLng(point)) {
		div.style.visibility = "visible";
	} else {
		div.style.visibility = "hidden";
	}
	div.style.top = px.y+ offset.y + 'px';
	div.style.left = px.x+ offset.x + 'px';
	return div;
}

function removeDivOnMap(map,div) {
	if(div) div.style.visibility = "hidden";
	
	//map.getContainer().removeChild(div);
}

function showThumbnailDiv(pt, thumb, map) {
	if(picDiv) removeDivOnMap(map,picDiv);
	
	picDiv = showDivOnMap(pt, new GPoint(-3,-75), map, picDiv);
	picDiv.innerHTML = showSquareImage('<img src="'+thumb+'" class="square-photo-image-small">',50,75);//"<img class='img100w img100h' src='"+thumb+"' style='max-width:100px;max-height:100px'>";
}

function makeSquareImage(img, size, thumbMax) {
	var offset = Math.floor((thumbMax - size) / 2) - 3;
	
	return "<div style='position: absolute; width:"+thumbMax+"px;height:"+thumbMax+"px; top:-"+offset+"px; left:-"+offset+"px;'>" +
	    "<table width=100% height=100% cellpadding=0 cellspacing=0><tr><td align=center valign=center class='middle center'>" +
	       img+
		"</td></tr></table></div>";
}

function showSquareImage(img, size, thumbMax) {
	return "<div style='height:"+size+"px; width:"+size+"px;' class='square-photo-container left'>" + 
				makeSquareImage(img, size, thumbMax) + 
	             "</div>";
}