<html>
	<head>
		<title>Hack Spots</title>
		<script type="text/javascript" src="js/ICanHaz.js"></script> 
		<script src='http://api.tiles.mapbox.com/mapbox.js/v1.0.0/mapbox.js'></script>
		<script src='js/jquery.js '></script>
		<script type="text/javascript" src='js/d3.js'></script>
		<script type="text/javascript" src='js/sheetsee.js'></script>
		<link rel="shortcut icon" href="https://raw.github.com/jllord/sheetsee-cache/master/favicon-p.png"/>


    <link href='http://api.tiles.mapbox.com/mapbox.js/v1.0.0/mapbox.css' rel='stylesheet' />
		<link media="screen" rel="stylesheet" type="text/css" href="/css/style.css"> 
		<link media="screen" rel="stylesheet" type="text/css" href="/css/site.css"> 
	</head>
<body>
	<div id="wrapper">
		<h1>Hack Spots</h1>
		<div id="container">
			
			<div id="latestSpot"></div>
			<div id="selectedSpot"></div>
			<div id="map"></div>
		</div>
		<div id="hackSpotsTable"></div>
		

	</div><!-- end wrapper -->

	<script id="hackSpotsTable" type="text/html">
		<table>
		<tr><th class="tHeader">Name</th><th class="tHeader">Address</th><th class="tHeader">City</th><th class="tHeader">State</th><th class="tHeader">Country</th></tr>
		  {{#rows}}
		  	<tr id="{{rowNumber}}" class="spotRow"><td>{{name}}</td><td>{{address}}</td><td>{{city}}</td><td>{{state}}</td><td>{{country}}</td><td><a class="button" href="https://maps.google.com/maps?q={{address}},{{city}},{{state}}" target="_blank">Map it!</a></td></tr>
		  	<tr class="hideRow"><td>wifi: {{wifipassword}}</td></tr>
		  {{/rows}}
	  </table>
	</script>

	<script id="latestSpot" type="text/html">
			{{#rows}}
				<h2>Recently Added</h2>
				<h3>{{name}}</h3>
				<p>{{address}}, {{city}}, {{state}}
				<ul>
			  	<li>Wifi: {{wifipassword}}</li>
			  	<li>Outlets: {{outlets}}</li>
			  	<li>Couch: {{couch}}</li>
			  	<li>Large Table: {{largetable}}</li>
			  	<li>Outdoor Seating: {{outdoorseating}}</li>
			  	<li>Brewing: {{brewing}}</li>
		  	</ul>
		  {{/rows}}
	</script>

	<script id="selectedSpot" type="text/html">
			{{#rows}}
				<h2>Selected Spot</h2>
				<h3>{{name}}</h3>
				<p>{{address}}, {{city}}, {{state}}
				<ul>
			  	<li>Wifi: {{wifipassword}}</li>
			  	<li>Outlets: {{outlets}}</li>
			  	<li>Couch: {{couch}}</li>
			  	<li>Large Table: {{largetable}}</li>
			  	<li>Outdoor Seating: {{outdoorseating}}</li>
			  	<li>Brewing: {{brewing}}</li>
		  	</ul>
		  {{/rows}}
	</script>

	<script type="text/javascript">
  document.addEventListener('DOMContentLoaded', function() {
  	Sheetsee.makeTable(gData, "#hackSpotsTable")

  	$('.spotRow').click(function(event) {
 			var rowNumber = $(this).closest("tr").attr("id")
 			console.log(rowNumber)
 			var dataElement = Sheetsee.getMatches(gData, rowNumber, "rowNumber")
 			console.log(dataElement)
 			var selectedSpot = ich.selectedSpot({
			  rows: dataElement
		  })
		  $('#latestSpot').css("display", "none")
		  $('#selectedSpot').html(selectedSpot).css("display", "inline")
		  var selectedCoords = [dataElement[0].lat, dataElement[0].long]
		  console.log(selectedCoords)
		  map.setView(selectedCoords, 17)
 		})


  	var theLatestSpot = gData.reverse()[0]
	  var latestSpot = ich.latestSpot({
		   rows: theLatestSpot
	  })
	  $('#latestSpot').html(latestSpot)

   }) 

	  var optionsJSON = ["name", "address", "city", "rownumber"]
    var geoJSON = Sheetsee.createGeoJSON(gData, optionsJSON)
		var map = Sheetsee.loadMap("map")
		Sheetsee.addTileLayer(map, 'examples.map-20v6611k')
		var markerLayer = Sheetsee.addMarkerLayer(geoJSON, map, 11)
		addPopups(map, markerLayer)

		function addPopups(map, markerLayer) {
		  markerLayer.on('click', function(e) {
		    var feature = e.layer.feature
		    var popupContent = '<h2>' + feature.opts.name + '</h2>' +
		                        '<h3>' + feature.opts.address + ', ' + feature.opts.city + '</h3>'
		    e.layer.bindPopup(popupContent,{closeButton: false,})
		  })
		}


	</script>

	<script type="text/javascript">
		console.log(gData)
	</script>
</body>
</html>