// set up icons
var startIcon = new GIcon();
startIcon.image = "img/markerS.png";
startIcon.shadow = "img/shadow50.png";
startIcon.iconSize = new GSize(20, 34);
startIcon.shadowSize = new GSize(37, 34);
startIcon.iconAnchor = new GPoint(9, 34);
startIcon.infoWindowAnchor = new GPoint(9, 2);
startIcon.infoShadowAnchor = new GPoint(18, 25);

var endIcon = new GIcon();
endIcon.image = "img/markerE.png";
endIcon.shadow = "img/shadow50.png";
endIcon.iconSize = new GSize(20, 34);
endIcon.shadowSize = new GSize(37, 34);
endIcon.iconAnchor = new GPoint(9, 34);
endIcon.infoWindowAnchor = new GPoint(9, 2);
endIcon.infoShadowAnchor = new GPoint(18, 25);

var redIcon = new GIcon();
redIcon.image = "img/mm_20_red.png";
redIcon.shadow = "img/mm_20_shadow.png";
redIcon.iconSize = new GSize(12, 20);
redIcon.shadowSize = new GSize(22, 20);
redIcon.iconAnchor = new GPoint(6, 20);
redIcon.infoWindowAnchor = new GPoint(5, 1);

var orangeIcon = new GIcon();
orangeIcon.image = "img/mm_20_orange.png";
orangeIcon.shadow = "img/mm_20_shadow.png";
orangeIcon.iconSize = new GSize(12, 20);
orangeIcon.shadowSize = new GSize(22, 20);
orangeIcon.iconAnchor = new GPoint(6, 20);
orangeIcon.infoWindowAnchor = new GPoint(5, 1);

var yellowIcon = new GIcon();
yellowIcon.image = "img/mm_20_yellow.png";
yellowIcon.shadow = "img/mm_20_shadow.png";
yellowIcon.iconSize = new GSize(12, 20);
yellowIcon.shadowSize = new GSize(22, 20);
yellowIcon.iconAnchor = new GPoint(6, 20);
yellowIcon.infoWindowAnchor = new GPoint(5, 1);

var greenIcon = new GIcon();
greenIcon.image = "img/mm_20_green.png";
greenIcon.shadow = "img/mm_20_shadow.png";
greenIcon.iconSize = new GSize(12, 20);
greenIcon.shadowSize = new GSize(22, 20);
greenIcon.iconAnchor = new GPoint(6, 20);
greenIcon.infoWindowAnchor = new GPoint(5, 1);

var blueIcon = new GIcon();
blueIcon.image = "img/mm_20_blue.png";
blueIcon.shadow = "img/mm_20_shadow.png";
blueIcon.iconSize = new GSize(12, 20);
blueIcon.shadowSize = new GSize(22, 20);
blueIcon.iconAnchor = new GPoint(6, 20);
blueIcon.infoWindowAnchor = new GPoint(5, 1);

var purpleIcon = new GIcon();
purpleIcon.image = "img/mm_20_purple.png";
purpleIcon.shadow = "img/mm_20_shadow.png";
purpleIcon.iconSize = new GSize(12, 20);
purpleIcon.shadowSize = new GSize(22, 20);
purpleIcon.iconAnchor = new GPoint(6, 20);
purpleIcon.infoWindowAnchor = new GPoint(5, 1);

var whiteIcon = new GIcon();
whiteIcon.image = "img/mm_20_white.png";
whiteIcon.shadow = "img/mm_20_shadow.png";
whiteIcon.iconSize = new GSize(12, 20);
whiteIcon.shadowSize = new GSize(22, 20);
whiteIcon.iconAnchor = new GPoint(6, 20);
whiteIcon.infoWindowAnchor = new GPoint(5, 1);

var click_x=0;
var click_y=0;
var typ='';

function showPoints(lat, lon, rad, typ) {
	window.frames['dataframe'].window.location.replace('datawindow.php?lon='+lon+'&lat='+lat+'&rad='+rad+'&typ='+typ);
}

function radialClickListener(overlay, point) {
	     if (point) {

		  map.clearOverlays();
		  map.addOverlay(new GMarker(point));

		  radius = parseInt(document.getElementById('rad').value);
		  zoomLevel = getZoomLevelFromRadius(radius);
		  for (i=0;i<document.forms[0].filter.length;i++)
			{
				if (document.forms[0].filter[i].checked)
				{
					typ = document.forms[0].filter[i].value;
				}
			}

		  map.centerAndZoom(point, zoomLevel);

		  showPoints(point.y, point.x, radius, typ);

		  GEvent.removeListener(RCL);

		  }
}

function linearClickListener(overlay, point) {
			if (point) {
				if (click_x==0) {
					click_x=point.x;
					click_y=point.y;
					createMarker(point, "", startIcon);					}
				else {
					var small_x, small_y, big_x, big_y;

					if (point.x > click_x){
					  	small_x = click_x;
						big_x = point.x;
						}
					else{
						small_x = point.x;
						big_x = click_x;
			   	 	}

					if (point.y > click_y){
						small_y = click_y;
						big_y = point.y;
						}
					else{
						small_y = point.y;
						big_y = click_y;
						}

					var center_x = small_x+((big_x-small_x)/2);
					var center_y = small_y+((big_y-small_y)/2);

					zoom = getZoomLevelFromBounds(small_x, small_y, big_x, big_y);
					map.centerAndZoom(new GPoint(center_x, center_y), zoom);

					disty = (big_y - small_y);
					distx = Math.cos(small_y) * (big_x - small_x);
					radius = (Math.sqrt(disty*disty + distx*distx) * 69)/ 2;

		  		showPoints(center_y, center_x, radius, typ);

					// remove the click listener so we don't get multiple points
					GEvent.removeListener(LCL);
					click_x = 0;
					createMarker(point, "", endIcon);
					}

				}
}

// Creates one of our tiny markers at the given point
function createMarker(point,html,icon) {
			var marker = new GMarker(point, icon);
			map.addOverlay(marker);
			if(html != ""){
				GEvent.addListener(marker, "click", function() {
					marker.openInfoWindowHtml(html);
					});
				}
}

function resetView() {
		map.centerAndZoom(new GPoint(-95.0, 42.0), 14);
}

function resetMarkers() {
		click_x = 0;
		map.clearOverlays();
		GEvent.clearListeners(map, 'click');
		window.frames['dataframe'].window.location.replace('about:blank');
}

function resetMarkersLCL() {
		resetMarkers();
		LCL = GEvent.addListener(map, 'click', linearClickListener);
}

function resetMarkersRCL() {
		resetMarkers();
		RCL = GEvent.addListener(map, 'click', radialClickListener);
}

function typeSelected(type) {
	typ = type;
}

function plotData( dataset) {
		for(i=0; i<dataset.length; i++){
			var html = "";
			if (dataset[i]["Website"] != ""){
				html += "<b><a href=\""+dataset[i]["Website"]+"\" target=\"_blank\">"+dataset[i]["Name"]+"</b></a><br>";
				}
			else{
				html += "<b>"+dataset[i]["Name"]+"</b><br>";
				}

			if (dataset[i]["Exit"] != ""){
				html += dataset[i]["Exit"]+"<br>";
				}

			html += dataset[i]["Address"]+"<br>"+
				dataset[i]["City"]+", "+dataset[i]["State"]+"  "+dataset[i]["Zip"]+"<br>";

			if (dataset[i]["Phone"] != ""){
				html += dataset[i]["Phone"]+"<br>";
				}

			if (dataset[i]["Email"] != ""){
				html += dataset[i]["Email"]+"<br>";
				}

			if (dataset[i]["Review_url"] != ""){
				html += "<a href=\""+dataset[i]["Review_url"]+"\" target=\"_blank\">read our review</a><br>";
				}

			switch(dataset[i]["Type"]){
				case "campground":
					createMarker(new GPoint(dataset[i]["Lon"], dataset[i]["Lat"]),html, greenIcon);
					break;
				case "truckstop":
					createMarker(new GPoint(dataset[i]["Lon"], dataset[i]["Lat"]),html, redIcon);
					break;
				case "restaurant":
					createMarker(new GPoint(dataset[i]["Lon"], dataset[i]["Lat"]),html, blueIcon);
					break;
				case "reststop":
					createMarker(new GPoint(dataset[i]["Lon"], dataset[i]["Lat"]),html, yellowIcon);
					break;
				default:
					createMarker(new GPoint(dataset[i]["Lon"], dataset[i]["Lat"]),html, whiteIcon);
				}

			}
}

function getZoomLevelFromBounds(minX, minY, maxX, maxY) {
  var x_log = Math.log((maxX - minX) / 175.0);
  var y_log = Math.log((maxY - minY) / 117.0);
  return 15 + Math.ceil((0.1 + Math.max(x_log, y_log)) / Math.log(2));
}

function getZoomLevelFromRadius(radius) {
  if (radius <= 25 ) return 8;
  if (radius <= 50 ) return 9;
  if (radius <= 100) return 10;
  if (radius <= 250) return 12;
  /* else */         return 14;
}

/* End of file. */

