// stardust.js written by Ed Hall
// copyright 2006 by Ed Hall.

// Create a "tiny" marker icon with the given color
function CreateSmallIcon( color )
{
  var icon = new GIcon();
  icon.image = "http://www.brillig.com/stardust/images/"+color+".png";
  icon.shadow = "http://www.brillig.com/stardust/images/shadow.png";
  icon.iconSize = new GSize(12, 20);
  icon.shadowSize = new GSize(22, 20);
  icon.iconAnchor = new GPoint(6, 20);
  icon.infoWindowAnchor = new GPoint(5, 1);
  return icon;
}

var blueIcon = CreateSmallIcon("blue");
var greenIcon = CreateSmallIcon("green");
var orangeIcon = CreateSmallIcon("orange");
var redIcon = CreateSmallIcon("red");
var whiteIcon = CreateSmallIcon("white");
var numObservers = 0;

// Create a marker whose info window displays the given number.
function CreateObserverMarker(status, lat, lon, name, note, url)
{
  ++numObservers;

  var icon;
  var statusStr;

  if ( status == "D" ) {
    icon = blueIcon;
    statusStr = "has data (photo, video, etc.)";
  } else if ( status == "V" ) {
    icon = greenIcon;
    statusStr = "visual observation only";
  } else if ( status == "W" ) {
    icon = orangeIcon;
    statusStr = "weather prevented observation";
  } else if ( status == "N" ) {
    icon = redIcon;
    statusStr = "no observation";
  } else {
    icon = whiteIcon;
    statusStr = "need follow-up to confirm";
  }
  var point = new GPoint(lon,lat);
  var marker = new GMarker(point,icon);
  // Show this marker's index in the info window when it is clicked.
  var str = "<b>Observer:</b> " + name;
  str += "<br /><i>" + statusStr + "</i>";
  if ( note != null ) {
    str += "<br />&#8220;" + note + "&#8221;";
  }
  if ( url != null ) {
    str += "<br />(<a href=\"" + url + "\" target=\"_blank\">more info</a>)";
  }
  GEvent.addListener(marker, 'click', function() {
    var element = document.getElementById("balloon");
    element = element.cloneNode(true);
    element.style.display = "";
    element.innerHTML = str;
	  marker.openInfoWindow(element);
  });

  return marker;
}


function onLoad() {
  if (GBrowserIsCompatible()) {
    var map = new GMap(document.getElementById("map"));
    map.addControl(new GLargeMapControl());
    map.addControl(new GMapTypeControl());
    map.centerAndZoom(new GPoint(-117.0, 40.600), 12);

    // Download the data in observers.xml and load it on the map. The format we
    // expect is:
    // <observers>
    //   <observer stat="D" lat="40.7273" lng="-114.0419" who="Bruce Fischer" />
    //   <observer stat="D" lat="43.0000" lng="-85.7000" who="James H. Van Prooyen" />
    // </observers>
    // where stat means the following:
    //    D   has data (photo, video, etc.)
    //    V   visual observation only
    //    W   weather prevented observation
    //    N   no observation (but not due to weather)
    //    -   need follow up with observer to confirm
    var request = GXmlHttp.create();
    request.open("GET", "observers.xml", true);
    request.onreadystatechange = function() {
      if (request.readyState == 4) {
        var xmlDoc = request.responseXML;
        var observers = xmlDoc.documentElement.getElementsByTagName("observer");
        for (var i = 0; i < observers.length; i++) {
          var latitude  = parseFloat(observers[i].getAttribute("lat"));
          var longitude = parseFloat(observers[i].getAttribute("lng"));
          var status = observers[i].getAttribute("stat");
          var who = observers[i].getAttribute("who");
          var note = observers[i].getAttribute("note");
          var url = observers[i].getAttribute("url");
          var marker = new CreateObserverMarker( status, latitude, longitude, who, note, url );
          map.addOverlay(marker);
        }
      }
    }
    request.send(null);

    // Stardust spacecraft's re-entry path
    var points = [];
    points.push(new GPoint(236.07873-360.0,41.75427));
    points.push(new GPoint(237.23156-360.0,41.64620));
    points.push(new GPoint(238.67353-360.0,41.49323));
    points.push(new GPoint(240.11424-360.0,41.32047));
    points.push(new GPoint(241.54484-360.0,41.12901));
    points.push(new GPoint(242.92304-360.0,40.92562));
    points.push(new GPoint(244.11491-360.0,40.73460));
    points.push(new GPoint(244.97006-360.0,40.58885));
    points.push(new GPoint(245.50835-360.0,40.49337));
    points.push(new GPoint(245.83808-360.0,40.43343));
    points.push(new GPoint(246.04575-360.0,40.39498));
    points.push(new GPoint(246.18104-360.0,40.36961));
    points.push(new GPoint(246.27057-360.0,40.35260));
    points.push(new GPoint(246.33025-360.0,40.34105));
    points.push(new GPoint(246.36973-360.0,40.33318));
    points.push(new GPoint(246.39371-360.0,40.32816));
    points.push(new GPoint(246.40862-360.0,40.32482));
    points.push(new GPoint(246.41747-360.0,40.32262));
    points.push(new GPoint(246.42248-360.0,40.32119));
    points.push(new GPoint(246.42529-360.0,40.32023));
    points.push(new GPoint(246.42698-360.0,40.31953));
    map.addOverlay(new GPolyline(points,"#ff6347",8,0.5));
  }
}
