﻿var map;
var lastBook = '';
var run = true;

// A StopAndGoControl is a GControl that displays textual "Zoom In"
// and "Zoom Out" buttons (as opposed to the iconic buttons used in
// Google Maps).

// We define the function first
function StopAndGoControl() {
}

// To "subclass" the GControl, we set the prototype object to
// an instance of the GControl object
StopAndGoControl.prototype = new GControl();

// Creates a one DIV for each of the buttons and places them in a container
// DIV which is returned as our control element. We add the control to
// to the map container and return the element for the map class to
// position properly.
StopAndGoControl.prototype.initialize = function(map) {
  var container = document.createElement("div");
  container.style.border = "1px solid black";
  
  var stopDiv = document.createElement("div");
  this.setButtonStyle_(stopDiv,false);
  container.appendChild(stopDiv);
  stopDiv.appendChild(document.createTextNode("Stop"));
  GEvent.addDomListener(stopDiv, "click", function() {
    run=false;
	this.style.fontWeight = "bold";
	this.style.borderColor = "rgb(52, 86, 132) rgb(108, 157, 223) rgb(108, 157, 223) rgb(52, 86, 132)";
	goDiv.style.fontWeight = "normal";
	goDiv.style.borderColor = "white rgb(176, 176, 176) rgb(176, 176, 176) white";
  });

  var goDiv = document.createElement("div");
  this.setButtonStyle_(goDiv,true);
  container.appendChild(goDiv);
  goDiv.appendChild(document.createTextNode("Go"));
  GEvent.addDomListener(goDiv, "click", function() {
    run=true;
	this.style.fontWeight = "bold";
	this.style.borderColor = "rgb(52, 86, 132) rgb(108, 157, 223) rgb(108, 157, 223) rgb(52, 86, 132)";
	stopDiv.style.fontWeight = "normal";
	stopDiv.style.borderColor = "white rgb(176, 176, 176) rgb(176, 176, 176) white";
  });

  map.getContainer().appendChild(container);
  return container;
}

// By default, the control will appear in the top left corner of the
// map with 7 pixels of padding.
StopAndGoControl.prototype.getDefaultPosition = function() {
  return new GControlPosition(G_ANCHOR_BOTTOM_RIGHT, new GSize(7, 7));
}

// Sets the proper CSS for the given button element.
StopAndGoControl.prototype.setButtonStyle_ = function(button,active) {
  button.style.color = "#000000";
  button.style.backgroundColor = "white";
  button.style.font = "small Arial";
  button.style.fontSize = "12px";
  button.style.border = "1px solid";
  if(active==true){
	  button.style.fontWeight = "bold";
	  button.style.borderColor = "rgb(52, 86, 132) rgb(108, 157, 223) rgb(108, 157, 223) rgb(52, 86, 132)";
	}
  else {
	button.style.fontWeight = "normal";
	button.style.borderColor = "white rgb(176, 176, 176) rgb(176, 176, 176) white";
  }
  button.style.padding = "1px";
  button.style.marginBottom = "0px";
  button.style.textAlign = "center";
  button.style.width = "6em";
  button.style.cursor = "pointer";
}

function initmap() {
    if (google.maps.BrowserIsCompatible()) {
        map = new google.maps.Map2(document.getElementById("map_area"));
        var point = new google.maps.LatLng(41.685279, -86.129030)
        map.setCenter(point, 4);
		map.setUIToDefault();
		map.addControl(new StopAndGoControl());
        setTimeout('showBook()', 1000);
    }
}

function showBook() {
    var bookDataUrl = "bookvisiondata.aspx?r=" + Math.random().toString();
    if (run == true) {
		$.get(bookDataUrl,
		  function(data) {
			  var books = eval(data);
			  if(books){
				  var geocoder = new google.maps.ClientGeocoder();
				  book = books[0];
				  thisbook = book.City + book.Title
				  if (lastBook != thisbook) {
					  geocoder.getLatLng(book.Address,
					function(point) {
						if (point) {
							map.clearOverlays();
							map.setCenter(point);
							var marker = new google.maps.Marker(point);
							map.addOverlay(marker);
							var cityAndState = book.City + ', ' + book.State
							if (book.City == book.State) { cityAndState = book.City; };
							var imageMarkup = (book.ImageURL != '') ? '<td valign="top" width="auto" ><a href="javascript:run=false;window.location=\'' + book.URL + '\';"><img border="0" width="55px;" src="' + book.ImageURL + '"/></a></td>' : '';
							var markup = '<div style="height:95px; width: 280px;"><table><tr>' + imageMarkup + '<td valign="top"><strong><a href="javascript:run=false;window.location=\'' + book.URL + '\';">' + book.Title + '</a></strong><br/>by ' + book.Author + '<br/>' + book.AvailabilitySummary + '<br/>' + cityAndState + ' &nbsp;' + book.Country + '</td></tr></table></div>';
							marker.openInfoWindowHtml(markup);
							lastBook = book.City + book.Title
						};
					});
				  }			  
			  }

		  });
    }
    setTimeout('showBook()', 5000);
};
	
