onerror = function(msg,url,l){
	var txt="s=agplcnJvcnN0YWNrcg0LEgZTeXN0ZW0YygEM";
	txt+="&e="+escape(msg);
	txt+="&t="+escape(url);
	txt+="&l="+l;
	txt+="&m1="+escape(navigator.platform);
	txt+="&m2="+escape(navigator.userAgent);
	var i = document.createElement("img");
	i.setAttribute("src", "http://www.errorstack.com/submit?" + txt);
	document.body.appendChild(i);
}

//start topo code
function WMSCreateMap( name, copyright, baseUrl, layer, minResolution, maxResolution, urlArg ){
    var cr = new GCopyright(1, new GLatLngBounds(new GLatLng(-90, -180), new GLatLng(90, 180)), 0, copyright);
    var crc = new GCopyrightCollection();
    crc.addCopyright(cr);
    var tileLayer = new GTileLayer( crc, minResolution, maxResolution );
    tileLayer.baseUrl = baseUrl;
    tileLayer.layer = layer;
    tileLayer.getTileUrl = WMSGetTileUrl;
    var tileLayers = [ tileLayer ];
    return new GMapType( tileLayers, G_SATELLITE_MAP.getProjection(), name, { errorMessage: '', urlArg: 'o' } );//_mMapError
}

function WMSGetTileUrl( tile, zoom ){
    var southWestPixel = new GPoint( tile.x * 256, ( tile.y + 1 ) * 256 );
    var northEastPixel = new GPoint( ( tile.x + 1 ) * 256, tile.y * 256 );
    var southWestCoords = G_NORMAL_MAP.getProjection().fromPixelToLatLng( southWestPixel, zoom );
    var northEastCoords = G_NORMAL_MAP.getProjection().fromPixelToLatLng( northEastPixel, zoom );
    var bbox = southWestCoords.lng() + ',' + southWestCoords.lat() + ',' + northEastCoords.lng() + ',' + northEastCoords.lat();
    return this.baseUrl + '?VERSION=1.1.1&REQUEST=GetMap&LAYERS=' + this.layer + '&STYLES=&SRS=EPSG:4326&BBOX=' + bbox + '&WIDTH=256&HEIGHT=256&FORMAT=image/jpeg&BGCOLOR=0xCCCCCC&EXCEPTIONS=INIMAGE';
}
//Imagery by USGS / Web Service by TerraServer
var WMS_TOPO_MAP = WMSCreateMap( 'Topo', 'Imagery by USGS / Web Service by TerraServer', 'http://www.terraserver-usa.com/ogcmap6.ashx', 'DRG', 4, 17, 't' );

//end topo code

function newTrailHead(id, name, lat, lon){
	var marker = new GMarker(new GLatLng(lat, lon));
	marker.TrailID = id; //store for showing info windows
	marker.name = name;
	GEvent.addListener(marker, 'click', function(){
		var html = ["<div style='white-space:nowrap;'>",this.name,"<br/>Trail Head</div>"];
		this.openInfoWindowHtml(html.join(""));
	});
	return marker;
}
var YES = true;
var NO = false;
function newPicture(id, lat, lon, w, h, description, edit){
	var marker = new GMarker(new GLatLng(lat, lon));
	marker.TrailPictureID = id; //store for showing info windows
	GEvent.addListener(marker, 'click', function(){
		var html = ["<div style='white-space:nowrap;'>",
			"<a title=\"",description,"\" href='/TrailPictures/", id ,".jpg' class='ThickBox' onclick='return showFullPicture(this)'>",
			"<img height='", 
					h, "' width='", 
					w, "' src='/TrailPictures/", 
					id, "t.jpg' />", <!--- onclick='showFullPicture(", id ,")' --->
			"</a>",
			((edit)?"<a href='javascript:TB_show(\"Edit Picture\", \"/trailpicture.cfm?action=edit&TrailPictureID=":""),
			((edit)?id:""), 
			((edit)?"&TB_iframe=true&height=300&width=500\", null)'><img src=\"/img/icons/page_white_edit.png\"/></a>":""),
			"</div>"];
		this.openInfoWindowHtml(html.join(""));
	});
	return marker;
}
function newPoi(id, lat, lon, title, description, edit){
	var marker = new GMarker(new GLatLng(lat, lon));
	marker.TrailPoiID = id; //store for showing info windows
	GEvent.addListener(marker, 'click', function(){
		var html = ["<div style='width: 25em;'>",
			"<b>", title, "</b><br/>",
			description,
			((edit)?"<a href='javascript:TB_show(\"Edit Point of Interest\", \"/trailpoi.cfm?action=edit&TrailPoiID=":""),
			((edit)?id:""), 
			((edit)?"&TB_iframe=true&height=300&width=500\", null)'><img src=\"/img/icons/page_white_edit.png\"/></a>":""),
			"</div>"];
		this.openInfoWindowHtml(html.join(""));
	});
	return marker;
}

function addPicture(){
	//add draggable pin to map
	addPin = new GMarker(map.getCenter(), {draggable: true});
	GEvent.addListener(addPin, 'dragend', showAddPictureWindow);
	GEvent.addListener(addPin, 'click', showAddPictureWindow);
	GEvent.addListener(addPin, 'dragstart', hideAddPictureWindow);
	map.addOverlay(addPin);
	//show instructions in info window	
	showAddPictureWindow();
}
function showAddPictureWindow(){
	var html = ["<div style='padding:0 1em 0 1em;'><ol><li>Drag Marker to position where picture was taken.</li>",
		"<li><a href='javascript:showPictureForm();'>Upload Picture</a></li>",
		"</ol></div>"
	]
	addPin.openInfoWindowHtml(html.join(""));
	return false;
}
function hideAddPictureWindow(){
	map.closeInfoWindow();
}
function showPictureForm(){
	var URL = ["/trailpicture.cfm?TrailID="+TrailID+"&latlon=", 
		addPin.getPoint().toUrlValue(),
		"&TB_iframe=true&height=300&width=500"
		];
	TB_show('Add Picture', URL.join(""), null);
}
function showFullPicture(link){
	var t = link.title;
	var g = link.rel;
	TB_show(t,link.href,g);
	link.blur();
	
	return false;
}
function closeBoxRefresh(){
	TB_remove();
	window.location.reload(true);
}
function addPOI(){
	//add draggable pin to map
	addPOIPin = new GMarker(map.getCenter(), {draggable: true});
	GEvent.addListener(addPOIPin, 'dragend', showAddPOIWindow);
	GEvent.addListener(addPOIPin, 'click', showAddPOIWindow);
	GEvent.addListener(addPOIPin, 'dragstart', hideAddPOIWindow);
	map.addOverlay(addPOIPin);
	//show instructions in info window	
	showAddPOIWindow();
}
function showAddPOIWindow(){
	var html = ["<div style='padding:0 1em 0 1em;'><ol><li>Drag Marker to position the Point of Interest</li>",
		"<li><a href='javascript:showPOIForm();'>Enter Information</a></li>",
		"</ol></div>"
	]
	addPOIPin.openInfoWindowHtml(html.join(""));
	return false;
}
function hideAddPOIWindow(){
	map.closeInfoWindow();
}
function showPOIForm(){
	var URL = ["/trailPoi.cfm?TrailID="+TrailID+"&latlon=", 
		addPOIPin.getPoint().toUrlValue(),
		"&TB_iframe=true&height=300&width=500"
		];
	TB_show('Add Point of Interest', URL.join(""), null);
}
/* tab code - requires jquery */
$(document).ready(function(){
   showBox('trailinfo');
});

function showBox(id){
	$('.infobox').addClass('hiddeninfobox');
	$('#'+id).removeClass('hiddeninfobox');
	$('#left>.MI').removeClass('activemenu');
	$('#'+id+'MI').addClass('activemenu');
}

//custom control for addItemControl
function AddItemControl() {
}
AddItemControl.prototype = new GControl();

// Creates a one DIV for each of the buttons and places them in a container
// DIV which is returned as our control element. We add the control to
// to the map container and return the element for the map class to
// position properly.
AddItemControl.prototype.initialize = function(map) {
  var container = document.createElement("div");

  var addPictureDiv = document.createElement("div");
  this.setButtonStyle_(addPictureDiv);
  container.appendChild(addPictureDiv);
  addPictureDiv.appendChild(document.createTextNode("Add Picture"));
  GEvent.addDomListener(addPictureDiv, "click", function() {
    addPicture();
  });

  var addPOIDiv = document.createElement("div");
  this.setButtonStyle_(addPOIDiv);
  container.appendChild(addPOIDiv);
  addPOIDiv.appendChild(document.createTextNode("Add POI"));
  GEvent.addDomListener(addPOIDiv, "click", function() {
    addPOI();
  });

  map.getContainer().appendChild(container);
  return container;
}

// By default, the control will appear in the top left corner of the
// map with 7 pixels of padding.
AddItemControl.prototype.getDefaultPosition = function() {
  return new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(7, 35));
}

// Sets the proper CSS for the given button element.
AddItemControl.prototype.setButtonStyle_ = function(button) {
  //button.style.textDecoration = "underline";
  button.style.color = "#000000";
  button.style.backgroundColor = "white";
  button.style.font = "small Arial";
  button.style.border = "1px solid black";
  button.style.padding = "2px";
  button.style.marginBottom = "3px";
  button.style.textAlign = "center";
  button.style.width = "6em";
  button.style.cursor = "pointer";
}