var map;
var bounds = new GLatLngBounds();
var mappoints = new Array();



    var diamondIcon = new GIcon();
      diamondIcon.image = "images/diamond.png";
//      diamondIcon.shadow = "shadow.png";
	diamondIcon.iconSize = new GSize(12, 12);
	//diamondIcon.shadowSize = new GSize(37, 34);
	diamondIcon.iconAnchor = new GPoint(6, 6);
	diamondIcon.infoWindowAnchor = new GPoint(9, 2);
	//diamondIcon.infoShadowAnchor = new GPoint(18, 25);

    var waterIcon = new GIcon();
      waterIcon.image = "images/water.png";
	waterIcon.iconSize = new GSize(16, 27);
	waterIcon.iconAnchor = new GPoint(6, 6);
	waterIcon.infoWindowAnchor = new GPoint(9, 2);

    var flagIcon = new GIcon();
      flagIcon.image = "images/flag.png";
	flagIcon.iconSize = new GSize(16, 27);
	flagIcon.iconAnchor = new GPoint(6, 6);
	flagIcon.infoWindowAnchor = new GPoint(9, 2);


function initmap(){

		if(GBrowserIsCompatible()){
			map = new GMap2(document.getElementById("map"));
			var loc = new GLatLng(38.568837, -121.7577838);
			map.setCenter(loc, 14);
			map.addControl(new GLargeMapControl());		
			map.addControl(new GMapTypeControl());	
		}
		

		
		

		populateTrailsList();
		drawLegend();
		
		

 
		
}


window.onload = initmap;
window.onunload = GUnload;




function populateTrailsList(){

	var t = document.getElementById("radioTrails");
	t.innerHTML = "";
	for(id in trailsArray){
		t.innerHTML += '<input type="radio" onclick="checkTrail(\''+id+'\')" name="trails" id="'+ id +'" value="'+id+'" />'+trailsArray[id].name+'<br />';
	
	}

}

function checkTrail(trail){
	var chk = document.getElementById("radioTrails");
	var chktr = eval("chk."+trail+".checked");
	var chkcount = 0;
	
	trail = trailsArray[trail];
	

	clearTrails();
	
	
	
	removeTrailInfo(trail);
	//highlightTrail(trail);
	if(chktr==true){
		displayTrailInfo(trail);
	}

	
	
	for(var i=0; i < chk.length; i++){
	
		if (chk[i].checked == true) {
			chkcount++;
			
			if(chkcount==1){
				delete bounds;
				bounds = new GLatLngBounds();
			}
			trail=trailsArray[chk[i].value];
			drawTrail(trail);
			loadSpecialPoints(trail)
		}
	}
	

	

	centerAndZoomOnBounds(bounds);
		
		
	
}

function clearTrails(){
	map.clearOverlays();

}

function displayTrailInfo(trail){ 
	document.getElementById("trailname").innerHTML = trail.name;
	document.getElementById("traildistance").innerHTML = Math.round((trail.distance/1609)*100)/100  + " mi. (" + (trail.distance/1000) + " km.)";
	document.getElementById("trailsurface").innerHTML = trail.surface
	document.getElementById("trailsummary").innerHTML = trail.summary;
	
	document.getElementById("trailkeywords").innerHTML = "";
	for(var i=0; i < trail.keywords.length; i++){
		document.getElementById("trailkeywords").innerHTML += trail.keywords[i];
		
		if(i < trail.keywords.length-1)
			document.getElementById("trailkeywords").innerHTML+=", ";
	
	}
	document.getElementById("trailtips").innerHTML = trail.tips;

}

function removeTrailInfo(trail){
	document.getElementById("trailname").innerHTML = "";
	document.getElementById("traildistance").innerHTML = "";
	document.getElementById("trailsurface").innerHTML = "";
	document.getElementById("trailsummary").innerHTML = "";
	document.getElementById("trailkeywords").innerHTML = "";
	document.getElementById("trailtips").innerHTML = "";
}

function highlightTrail(trail){

	for(id in trailsArray){
			
		trailsArray[id].coordinates.color = "#"+trailsArray[id].linecolor;
	
	}
	trail.coordinates.color="#000000";
}

function drawTrail(trail){

	map.addOverlay(trail.coordinates);
}

function drawLegend(){

	var txt = "<table><tr>";
	txt+="<td><img src='images/diamond.png' /></td><td> - Mileage marker </td>";
	txt+="<td><img src='images/water.png'  /></td><td> - Drinking fountain </td>";
	txt+="<td><img src='images/flag.png'  /></td><td> - Access to levee</td>";
	
	txt +="</tr></table>";
	document.getElementById("legend").innerHTML = txt;
	

}

function removeTrail(trail){

	map.removeOverlay(trail.coordinates);
}



function d2h(d) {return d.toString(16);}
function h2d(h) {return parseInt(h,16);}


function loadSpecialPoints(trail){
//trail is a reference to the actual object in trailsArray

	if(trail.specialpoints != null || trail.specialpoints != undefined){
		for(var i=0; i < trail.specialpoints.length; i++){
			loadPoint( trail.specialpoints[i][0], trail.specialpoints[i][1], trail.specialpoints[i][2], trail );
		}
	}
}

function loadPoint(latlng, txt, type, trail){


	var w = /waterWater/;
	if(txt.match('water') || txt.match("Water") ){
		var marker = new GMarker(latlng, waterIcon);
	
	}else if(type=="flag" ){
	var marker = new GMarker(latlng, flagIcon);
	
	}
	
	else{
		var marker = new GMarker(latlng, diamondIcon);
	}
	

	function gotoPoint(){
		//highlightTrail(trail);
		marker.openInfoWindowHtml(txt);
		displayTrailInfo(trail);

	}
	
	
	GEvent.addListener(marker, 'click', gotoPoint);	
	map.addOverlay(marker);	
	bounds.extend(latlng);
	
	

	
}
/*


function gotoMapPoint(tid){

	var tpt = mappoints[tid];
	map.panTo(new GLatLng(tpt.point.latitude, tpt.point.longitude));
	tpt.openInfoWindowHtml(tpt.point.address);

}









function loadIcon(tPoint){
//tPoint should have the class attributes needed to decide which icon to load

	var id = tPoint.id;
	
	return icons[tPoint.id];


}


function zoom(i){

	map.setZoom(i);
}
*/


function centerAndZoomOnBounds(bounds) {
var center = bounds.getCenter();
var newZoom = map.getBoundsZoomLevel(bounds);
  if (map.getZoom() != newZoom) {
		
		map.setCenter(center, newZoom);
  } else {
		map.panTo(center);
  }
} 

function trace(txt){

document.getElementById("debug").innerHTML += txt+"<br/>";
}



