// google.js

var map = null;
var geocoder = null;
var homeAdd = null;
var homePoint = null;
var gmarkers = [];

function initialize() {
  if (GBrowserIsCompatible()) {
	map = new GMap2(document.getElementById("map"), { size: new GSize(298,265) } );
	map.setCenter(new GLatLng(42.239194,-83.610764), 11);

	map.addControl(new GLargeMapControl());

	var mapControl = new GMapTypeControl();
	map.addControl(mapControl);

	geocoder = new GClientGeocoder();
  }
}

function initializeHome() {
  initialize();
  showAddress(homeAdd);	  
}

//Home Icon
var homeIcon = new GIcon();
homeIcon.image = "http://maps.google.com/mapfiles/arrow.png";
homeIcon.infoWindowAnchor = new GPoint(5, 2);
homeIcon.iconAnchor = new GPoint(5, 34);	

function createHomeMarker(point) {
	var html = '<div style="width:100px"><b>Home Address:</b><br>'+homeAdd+'</div>';
	
	var marker = new GMarker(point, homeIcon);
	GEvent.addListener(marker, "click", function() {
	  marker.openInfoWindowHtml(html);
	});
	return marker;
}

function createMarker(point, distance, id, address) {
	var html = '<div style="width:100px"><b>Distance to Home:</b><br>'+distance+' Miles';
	html = html +  '<br><form action="http://maps.google.com/maps" method="get"" target="_blank">' +
				   '<INPUT value="Get Directions" TYPE="SUBMIT">' +
				   '<input type="hidden" name="daddr" value="'+ address + '"/>' +
				   '<input type="hidden" name="saddr" value="'+ homeAdd + '"/></div>';
	
	var marker = new GMarker(point);
	GEvent.addListener(marker, "click", function() {
	  marker.openInfoWindowHtml(html);
	});
	
	gmarkers[id] = marker;
	
	return marker;
}

  
  // This function picks up the click and opens the corresponding info window
function myclick(id) {
	GEvent.trigger(gmarkers[id], "click");
 }

function showAddress(address) {
  homeAdd = address;
  if (geocoder) {
	geocoder.getLatLng(
	  address,
	  function(point) {
		if (!point) {
			var newText = address + " not found";
			var oldText = document.getElementById("change");
			replaceTextAdd(oldText, newText, 'red');
			initialize();
		} else {
			homePoint = point;
			map.setCenter(homePoint, 11);
			var marker = createHomeMarker(homePoint);
			map.addOverlay(marker);
			var newText = "Address successfully validated";
			var oldText = document.getElementById("change");
			replaceTextAdd(oldText, newText, 'gr');					
		}
	  }
	);
  }
}

function showItemPins(page){
 initialize();
 var bounds = new GLatLngBounds();
 var start = page * 8;
 var end = start + 8;
 
 for (var i = start; i < end; i++) {
	if (i == start) {
	  var markerHome = new createHomeMarker(homePoint);
	  map.addOverlay(markerHome);
	  bounds.extend(homePoint);
	}
	if (sortedArray[i][6] != "N/A") {
		var point = sortedArray[i][6];
		var distance = sortedArray[i][9];
		var address = sortedArray[i][5];
		var id = sortedArray[i][4];			
		bounds.extend(point);
		var marker = new createMarker(point, distance, id, address);
		map.addOverlay(marker);					  
		map.setZoom(map.getBoundsZoomLevel(bounds));
		map.setCenter(bounds.getCenter());	
	}
 }
  request.send(null);
}