
// these will support sorting them

  GPoint.prototype.html = '';
  GPoint.prototype.theta = 0;

// or sorting these if we're lucky

 GMarker.prototype.theta = 0;
 GMarker.prototype.html = '';

   var list_html = '';
    
    var map;

    var origin;

    var current_hilite = -1;

     var keyword;

     var current_bounds;

     var markerset = [];

function draw_map () {

// choose center and scale,
// get pixels size from container,
// see what we get for coverage in GBounds

// we could offset this for neighborhoods of clients

     origin = new GPoint (-71.3, 41.5);
     map = new GMap (document.getElementById ("map"));
    map.addControl (new GSmallMapControl ());
// map.addControl (new GMapTypeControl ());
    map.centerAndZoom (origin, 4);

   current_bounds =  map.getBoundsLatLng();

// add listener to keep up with changed coverage

  GEvent.addListener (map,
                     "movend",
                      function () { current_bounds =  map.getBoundsLatLng(); });

// geocoder.us puts 42 Renfrew at
// 41.492376  -71.280756 
// they SHOW it very close but wrong side of corner
// BUT google shows this a whole block South East of correct street
// does the error depend on zoom? it is the same pixels inset X & Y
// can we specify no padding?
// this helped but did not fix it  margin:0px;

  var point1 = new GPoint(-71.280756, 41.492376);

  var marker1 = new GMarker(point1);
  map.addOverlay(marker1);

// TRYING THIS OK ...

  var gmx = marker1.x;
  var gmy = marker1.y;

  var html = "Aquidneck.US  WHQ <br>(-71.280756, 41.492376) ";
  GEvent.addListener (marker1,
                     "click",
                      function () { marker.openInfoWindowHtml (html);  });

// Yahoo geocoder puts 42 Renfrew 
// 41.492243  -71.280929
// which is the correct side of the corner

  var point2 = new GPoint(-71.280929, 41.492243);

  var marker2 = new GMarker(point2);
  map.addOverlay(marker2);

  var html2 = "Aquidneck.US  WHQ <br>(-71.280929, 41.492243) ";
  GEvent.addListener (marker2,
                     "click",
                      function () { marker2.openInfoWindowHtml (html2);  });

//   - - -   geocoder.us vs yahoo
// diff lat .492376 - .492243 =  0.000133 = 50 ft yahoo is south 
// diff lng .280765 - . 280929 = -0.000164 = 50 ft yahoo is west
// seems yahoo is the correct corner of the intersection


}


// Creates a marker whose info window displays the given html
// this works but doing it inline did not
// some scope issue?

    function createMarker (point, html) {
        var marker = new GMarker (point);
      
        // Show this marker's  info window when it is clicked
// COULD WE USE rollover?
 
        GEvent.addListener (marker, "click", function() {
          marker.openInfoWindowHtml (html);
        });
      
        return marker;
    }

// request XML info with locations matching given keyword,
// draw an overlay marker for each,
// with popup info such as Title and URL

// send logged-in user_id with server request for record keeping
// or send null if not

function get_locs (keyword, user_id) {

// remove old set if any

   map.clearOverlays ();

   list_html = '';
  current_hilite = -1;
   markerset = [];

  var request = GXmlHttp.create();

// MOVE THIS 'dummy_gps' to scripts/db/get_xml.php?service=directory&keyword=something
// WE COULD DO something simple to OBSCURE the GPS  e.g. times three
// NOTE the GET URL could be complete or relative

  request.open ("GET",
       "http://aquidneck.us/dummy_gps.php?keyword=" + keyword
        + "&user_id=" + user_id,
         true);

  request.onreadystatechange = function() {

   if (request.readyState == 4) {
     var xmlDoc = request.responseXML;

// these are the <marker> tags in the XML, not our page GMarkers

     var markers = xmlDoc.documentElement.getElementsByTagName("marker");

// create a GMarker for each of them

     for (var i = 0; i < markers.length; i++) {

// first the L-L

       var pointi = new GPoint(parseFloat(markers[i].getAttribute("lng")),
                              parseFloat(markers[i].getAttribute("lat")));

// other stuff will go into text

       var title = markers[i].getAttribute("title");
       var url = markers[i].getAttribute("url");
       var street = markers[i].getAttribute("street");
       var city = markers[i].getAttribute("city");

       var phone = markers[i].getAttribute("phone");
       var fax = markers[i].getAttribute("fax");
       var cell = markers[i].getAttribute("cell");
       var tollfree = markers[i].getAttribute("tollfree");
       var info = markers[i].getAttribute("info");

// link to their own site if any else just title
// (could even show thumbshot, how would that look?)

       var html;
       if ((typeof url != 'undefined') && ('' !=  url)) {
          html = "<a target='out' "
  + " href='http://aquidneck.us/redirect.php?URL=http://"
  + url
  + "' >"
  + title
  + "<br>"
  + url
  + "</a>";
       }
       else {
          html = title;
       }

       html = html + "<br>" + street + ", " + city;

      if ((typeof phone != 'undefined')  && ('' !=  phone)) { html =  html + "<br>phone: " + phone; }

      if ((typeof fax != 'undefined') && ('' !=  fax)) { html =  html + "<br>fax: " + fax; }

      if ((typeof cell != 'undefined')  && ('' !=  cell)) { html =  html + "<br>cell: " + cell; }

      if ((typeof tollfree != 'undefined')   && ('' !=  tollfree)) { html =  html + "<br>tollfree: " + tollfree; }

// hours, other phones,  or such if provided

      if ((typeof info != 'undefined') && ('' !=  info) && ('null' !=  info)) { html =  html + "<br>" + info; }

      var  markeri =  createMarker (pointi, html);

// set the extension fields

         markeri.x = pointi.x; 
         markeri.y =  pointi.y;
         markeri.theta =  Math.atan2 ((markeri.y - origin.y), (markeri.x - origin.x));

// show the LL just for debug

 //        html  =  html + " x=" + markeri.x  + " y=" + markeri.y;

         markeri.html =  html;

//  not yet sorted but OK to add them to map

         map.addOverlay(markeri);

// these will be sorted when complete

         markerset[i] = markeri; 
     }

// sort them on theta to circle clockwise from West thru North, East, South

   markerset.sort (function(p1, p2) { return p2.theta - p1.theta; });

// now sorted  add their strings to listing

      for (var i = 0; i < markerset.length; i++) {
            list_html = list_html + "<li>" + markerset[i].html + "</li>\n";
     }

// now we can show the complete listing
// "<h3>" + keyword + "</h3>" +

       document.getElementById ('showlist').innerHTML =
         "<ul>" + list_html + "</ul>";
   }
 }

 request.send(null);
}

// point could be something else with x, y fields
// e.g. our extension of GMarker

function point_in_box (point, box) {
   return ((point.x > box.minX) && (point.x < box.maxX)
  && (point.y > box.minY) && (point.y < box.maxY));
}

// requiring next hilite to be in current_bounds works,
// but choosing one to openInfoWindow shifts the view,
// which causes a wandering view problem for the next one,
// but allowing remote ones has other problems

// theta sort ignoring distance is part of the problem
// but any sort will still have the wandering effect

// could we just light up the markers instead? look in their FaQ
// FWIW even Google Local has the problem 

//  && (! point_in_box (markerset[current_hilite], current_bounds))

function move_hilite (byone) {
  var old_hilite = current_hilite;
  current_hilite += byone;
  if (current_hilite > markerset.length -1) current_hilite = 0;
  if (current_hilite < 0) current_hilite = markerset.length -1;
 /*
  while ((old_hilite != current_hilite)
           && (! point_in_box (markerset[current_hilite], current_bounds))) {
      current_hilite += byone;
      if (current_hilite > markerset.length -1) current_hilite = 0;
      if (current_hilite < 0) current_hilite = markerset.length -1;
   }
*/
   if (old_hilite != current_hilite) {
      markerset[current_hilite].openInfoWindowHtml (markerset[current_hilite].html);
  }  }


