//set up the map
function showMap(map) {
    map.setCenter(new google.maps.LatLng(36.065364, -79.812639), 13);
    map.addControl(new GLargeMapControl());

    var uncgIcon = new GIcon(G_DEFAULT_ICON, 'http://web-d.uncg.edu/saf/images/uncgMapIcon.png');
    uncgIcon.iconSize = new GSize(40, 50);
    uncgIcon.iconAnchor = new GPoint(20, 50);
    var uncgIconOpts = {
	    icon:uncgIcon,
	    zIndexProcess:function() {return 2;}
    };
    var uncgMarker = new GMarker(new GLatLng(36.065364, -79.812639), uncgIconOpts);
    map.addOverlay(uncgMarker);
}
    
function changeMarker(lat, lng, html, displayNum, map) {
	var siteIcon = new GIcon(G_DEFAULT_ICON, 'https://valsaf1.uncg.edu/image.php?imagePath=https://valsaf1.uncg.edu/map_marker.gif&imageString=' + displayNum);
	var siteIconOpts = {
		icon:siteIcon,
		zIndexProcess:function() {return 2;}
	};
    var siteMarker = new GMarker(new GLatLng(lat, lng), siteIconOpts);

    map.addOverlay(siteMarker);

    var htmlInfo = "<div style='padding: 5px;'>";
    htmlInfo += html + "</div>";

    GEvent.addListener(siteMarker, "click", function() {siteMarker.openInfoWindow(htmlInfo);});
}

function changeDir(siteDir) {
  var polyline = siteDir.getPolyline();
  polylineOpts = {
      color:"#00FF00",
	  weight:3,
	  opacity:0.5
  };
  polyline.setStrokeStyle(polylineOpts);
}

//add heat data
function addHeat(map, divId) {
	//var route1 = new GDirections(map);
	/*//var heat70 = new GDirections(map);
	//var heat71 = new GDirections(map);
	//var heat72 = new GDirections(map);
	//var heat73 = new GDirections(map);
	var heat74 = new GDirections(map);
	var heat75 = new GDirections(map);
	
    //heat70.load("300 South Davie Street, Greensboro, NC to: 200 East Washington Street, Greensboro, NC to: 299 South Dudley Street, Greensboro, NC to: 1000 East Market Street, Greensboro, NC to: 3200 Burlington Road, Greensboro, NC to: 3695 East Wendover Avenue, Greensboro, NC to: 3739 Burlington Road, Greensboro, NC to: 3199 East Market Street, Greensboro, NC to: 100 South Dudley Street, Greensboro, NC to: 999 East Washington Street, Greensboro, NC to: 300 South Davie Street, Greensboro, NC");
    //heat71.load("300 South Davie Street, Greensboro, NC to: 169 East Friendly Avenue, Greensboro, NC to: 100 Tate Street, Greensboro, NC to: 1000 Spring Garden Street, Greensboro, NC to: 518 Stirling Street, Greensboro, NC to: 1300 Walker Avenue, Greensboro, NC to: 499 South Aycock Street, Greensboro, NC to: 124 North Aycock Street, Greensboro, NC to: 1799 Fairmont Street, Greensboro, NC to: 399 Hillcrest Drive, Greensboro, NC to: 1500 West Friendly Avenue, Greensboro, NC to: 600 Friendly Center Road, Greensboro, NC to: 2700 Grandview Avenue, Greensboro, NC to: 699 Pembroke Road, Greensboro, NC to: 3200 West Friendly Avenue, Greensboro, NC to: 800 New Garden Road, Greensboro, NC to: 5800 Arcadia Drive, Greensboro, NC to: 800 New Garden Road, Greensboro, NC to: 5704 West Friendly Avenue, Greensboro, NC to: 500 Pembroke Road, Greensboro, NC to: 2799 Grandview Avenue, Greensboro, NC ");
    //heat72.load("300 Echols Street, Greensboro, NC to: 232 East Washington Street, Greensboro, NC to: 299 South Dudley Street, Greensboro, NC to: 1000 East Market Street, Greensboro, NC to: 100 North Benbow Street, Greensboro, NC to: 1703 Bluford Street, Greensboro, NC to: 199 Obermeyer Street, Greensboro, NC to: 100 Daniel Street, Greensboro, NC to: 1600 Bluford Street , Greensboro, NC");
    //heat73.load("2399 Spring Garden Street, Greensboro, NC to: 900 Northridge Street, Greensboro, NC to: 2300 Oakland Avenue, Greensboro, NC to: 1299 Park Terrace, Greensboro, NC to: 2899 Spring Garden Street, Greensboro, NC to: 499 South Tate Street, Greensboro, NC to: 1100 West Friendly Avenue, Greensboro, NC to: 1213 West Market Street, Greensboro, NC to: 100 South Aycock Street, Greensboro, NC to: 1599 Walker Avenue, Greensboro, NC to: 600 South Elam Avenue, Greensboro, NC to: 2200 Spring Garden Street, Greensboro, NC to: 1100 Park Terrace, Greensboro, NC to: 2799 Oakland Avenue, Greensboro, NC to: 999 Northridge Street, Greensboro, NC to: 2399 Spring Garden Street, Greensboro, NC ");
    //heat74.load("300 South Davie Street, Greensboro, NC to: 300 East Washington Street, Greensboro, NC to: 604 West Lee Street, Greensboro, NC to: 5006 High Point Road, Greensboro, NC to: 162 Montgomery Circle, Greensboro, NC to: 5006 High Point Road, Greensboro, NC to: 604 West Lee Street, Greensboro, NC to: 403 South Edgeworth Street, Greensboro, NC to: 300 East Washington Street, Greensboro, NC to: 300 South Davie Street, Greensboro, NC ");
    heat74.load("500 South Benbow Road, Greensboro, NC to: 1400 McConnell Road, Greensboro, NC to: 700 South Booker Street, Greensboro, NC to: 1400 Perkins Street, Greensboro, NC to: 199 North Benbow Road, Greensboro, NC to: 1703 Bluford Street, Greensboro, NC to: 199 Obermeyer Street, Greensboro, NC to: 100 Daniel Street, Greensboro, NC to: 1600 Bluford Street, Greensboro, NC to: 200 North Benbow Road, Greensboro, NC to: 299 North Booker Street, Greensboro, NC to: 400 North Benbow Road, Greensboro, NC to: 1499 Sullivan Street, Greensboro, NC to: 800 East Lindsay Street, Greensboro, NC to: 1205 East Bessemer Avenue, Greensboro, NC to: 999 Park Avenue, Greensboro, NC to: 900 Dewey Street, Greensboro, NC to: 699 Boyd Street, Greensboro, NC");
    heat75.load("700 East Lindsay Street, Greensboro, NC to: 1400 Sullivan Street, Greensboro, NC to: 489 North Benbow Road, Greensboro, NC to: 500 South Benbow Road, Greensboro, NC");

    //GEvent.addListener(heat70, "load", function() {changeHeat(heat70, "heatBusRoute70")});
    //GEvent.addListener(heat71, "load", function() {changeHeat(heat71, "heatBusRoute71")});
    //GEvent.addListener(heat72, "load", function() {changeHeat(heat72, "heatBusRoute72")});
    //GEvent.addListener(heat73, "load", function() {changeHeat(heat73, "heatBusRoute73")});
    GEvent.addListener(heat74, "load", function() {changeHeat(heat74, "heatBusRoute74")});
    GEvent.addListener(heat75, "load", function() {changeHeat(heat75, "heatBusRoute75")});*/

    
    //create the control panel for toggling heat routes
    var heatRouteHeader = document.createElement('h4');
    heatRouteHeader.innerHTML = 'Heat Routes';
    var heat1div = document.createElement('div');
    heat1div.innerHTML = '<input type="checkbox" checked="checked" id="heatBusRoute1" /> - Heat 1';
    var heat6div = document.createElement('div');
    heat6div = '<input type="checkbox" checked="checked" id="heatBusRoute6" /> - Heat 6';
    var heat70div = document.createElement('div');
    heat70div.innerHTML = '<input type="checkbox" checked="checked" id="heatBusRoute70" /> - Heat 70';
    var heat71div = document.createElement('div');
    heat71div.innerHTML = '<input type="checkbox" checked="checked" id="heatBusRoute71" /> - Heat 71';
    var heat72div = document.createElement('div');
    heat72div.innerHTML = '<input type="checkbox" checked="checked" id="heatBusRoute72" /> - Heat 72';
    var heat73div = document.createElement('div');
    heat73div.innerHTML = '<input type="checkbox" checked="checked" id="heatBusRoute73" /> - Heat 73';
    var heat74div = document.createElement('div');
    heat74div.innerHTML = '<input type="checkbox" checked="checked" id="heatBusRoute74" /> - Heat 74';
    var heat75div = document.createElement('div');
    heat75div.innerHTML = '<input type="checkbox" checked="checked" id="heatBusRoute75" /> - Heat 75';
    
    document.getElementById(divId).appendChild(heatRouteHeader);
    document.getElementById(divId).appendChild(heat1div);
    document.getElementById(divId).appendChild(heat70div);
    document.getElementById(divId).appendChild(heat71div);
    document.getElementById(divId).appendChild(heat72div);
    document.getElementById(divId).appendChild(heat73div);
    document.getElementById(divId).appendChild(heat74div);
    document.getElementById(divId).appendChild(heat75div);
	
	//add polyline using kml data
	var heat1Xml = new GGeoXml("http://web-d.uncg.edu/stn/service/xml/heat1.xml");
	map.addOverlay(heat1Xml);
	document.getElementById("heatBusRoute1").onclick = function() {heat1Xml.isHidden() ? heat1Xml.show() : heat1Xml.hide()};
	
	var heat6Xml = new GGeoXml("http://web-d.uncg.edu/stn/service/xml/heat6.xml");
	map.addOverlay(heat6Xml);
	document.getElementById("heatBusRoute6").onclick = function() {heat6Xml.isHidden() ? heat6Xml.show() : heat6Xml.hide()};
	
	var heat70Xml = new GGeoXml("http://web-d.uncg.edu/stn/service/xml/heat70.xml");
	map.addOverlay(heat70Xml);
	document.getElementById("heatBusRoute70").onclick = function() {heat70Xml.isHidden() ? heat70Xml.show() : heat70Xml.hide()};
	
	var heat71Xml = new GGeoXml("http://web-d.uncg.edu/stn/service/xml/heat71.xml");
	map.addOverlay(heat71Xml);
	document.getElementById("heatBusRoute71").onclick = function() {heat71Xml.isHidden() ? heat71Xml.show() : heat71Xml.hide()};
	
	var heat72Xml = new GGeoXml("http://web-d.uncg.edu/stn/service/xml/heat72.xml");
	map.addOverlay(heat72Xml);
	document.getElementById("heatBusRoute72").onclick = function() {heat72Xml.isHidden() ? heat72Xml.show() : heat72Xml.hide()};
	
	var heat73Xml = new GGeoXml("http://web-d.uncg.edu/stn/service/xml/heat73.xml");
	map.addOverlay(heat73Xml);
	document.getElementById("heatBusRoute73").onclick = function() {heat73Xml.isHidden() ? heat73Xml.show() : heat73Xml.hide()};
	
	var heat74Xml = new GGeoXml("http://web-d.uncg.edu/stn/service/xml/heat74.xml");
	map.addOverlay(heat74Xml);
	document.getElementById("heatBusRoute74").onclick = function() {heat74Xml.isHidden() ? heat74Xml.show() : heat74Xml.hide()};
	
	var heat75Xml = new GGeoXml("http://web-d.uncg.edu/stn/service/xml/heat75.xml");
	map.addOverlay(heat75Xml);
	document.getElementById("heatBusRoute75").onclick = function() {heat75Xml.isHidden() ? heat75Xml.show() : heat75Xml.hide()};
}

function changeHeat(heatRoute, id) {
  var polyline = heatRoute.getPolyline();
  polylineOpts = {
      color:"#FF0000",
	  weight:3,
	  opacity:1
  };
  polyline.setStrokeStyle(polylineOpts);

  //var routeInput = document.getElementById(id);
  //routeInput.onclick = function() {toggleHeat(polyline, heatRoute)};
  
  dDiv = document.getElementById('polylineData');
  //output marker data
  dDiv.innerHTML += "<h4>Marker Data for: " + id + "</h4>";
  for (var i =0; i < heatRoute.getNumGeocodes(); i++) {
	  dDiv.innerHTML += heatRoute.getMarker(i).getLatLng().lng() + "," + heatRoute.getMarker(i).getLatLng().lat() + ",0<br />";
  }
  
  //output vertex data
  dDiv.innerHTML += "<h4>Polyline Data for: " + id + "</h4>";
  for (var i =0; i < polyline.getVertexCount(); i++) {
	  dDiv.innerHTML += polyline.getVertex(i).lng() + "," + polyline.getVertex(i).lat() + ",0<br />";
  }
}

function toggleHeat(aGeoXml) {
  //aGeoXml.isHidden() ? aGeoXml.show() : aGeoXml.hide();
}

//draw a circle on the map
function drawCircle(center, radius, color, thickness, opacity, map) {
	//Function created by Chris Haas
	var circleQuality = 20;			//1 is best but more points, 5 looks pretty good, too
	var M = Math.PI / 180;			//Create Radian conversion constant
	var L = map.getBounds();		//Holds copy of map bounds for use below
	var sw = L.getSouthWest();
	var ne = L.getNorthEast();

	//The map is not completely square so this calculates the lat/lon ratio
	// this works because we create a square map
	//var circleSquish = (ne.lng() - sw.lng()) / (ne.lat() - sw.lat());
	var circleSquish = 1;

	var points = [];							//Init Point Array
	//Loop through all degrees from 0 to 360
	for(var i=0; i<360; i+=circleQuality){
		var P = new GLatLng(
			center.lat() + (radius * Math.sin(i * M)),
			center.lng() + (radius * Math.cos(i * M)) * circleSquish
			);
		points.push(P);
	}
	points.push(points[0]);	// close the circle
	var p = new GPolyline(points, color, thickness, opacity)
	map.addOverlay(p);
}