/* SPA User Map -- By Dave Pearson <davep@davep.org> */

// Icon used to mark a member.
var memIcon              = new GIcon();
memIcon.image            = "Member.png";
memIcon.shadow           = "Shadow.png";
memIcon.iconSize         = new GSize( 12, 20 );
memIcon.shadowSize       = new GSize( 22, 20 );
memIcon.iconAnchor       = new GPoint( 6, 20 );
memIcon.infoWindowAnchor = new GPoint( 5, 1 );

// Icon used to mark a place-of-interest.
var PoIIcon              = new GIcon();
PoIIcon.image            = "PoI.png";
PoIIcon.shadow           = "Shadow.png";
PoIIcon.iconSize         = new GSize( 12, 20 );
PoIIcon.shadowSize       = new GSize( 22, 20 );
PoIIcon.iconAnchor       = new GPoint( 6, 20 );
PoIIcon.infoWindowAnchor = new GPoint( 5, 1 );

// Constructor for a BB user.
function BBUser( bbid, bbuid, lat, lon, mugshot )
{
   this.bbid    = bbid;
   this.bbuid   = bbuid;
   this.lat     = lat;
   this.lon     = lon;
   this.marker  = null;
   this.poptext =
      "<table><tr><th style=\"text-align: right\">User:</th><td style=\"text-align: left; white-space: nowrap;\"><a href=\"http://www.popastro.com/phpBB2/profile.php?mode=viewprofile&u=" + bbuid + "\" target=\"_userinfo\">"  + bbid + "</a></td></tr>" +
      "<tr><th style=\"text-align: right\">Latitude:</th><td style=\"text-align: left; white-space: nowrap;\">"  + lat + "&deg;</td></tr>" +
      "<tr><th style=\"text-align: right\">Longitude:</th><td style=\"text-align: left; white-space: nowrap;\">" + lon + "&deg;</td></tr></table>" +
      ( mugshot == "" ? "" : "<br /><img src=\"" + mugshot + "\" />" );
}

// Constructor for a PoI.
function PoI( lat, lon, title, url )
{
   this.lat     = lat;
   this.lon     = lon;
   this.title   = title;
   this.marker  = null;
   this.poptext = "<p style=\"white-space: nowrap;\"><a target=\"_blank\" href=\"" + url + "\">" + title + "</a></p>";
}

// Function for adding a user to the display.
function addUser( ulist, map, user )
{
   // Create the point and marker for the user.
   var point  = new GLatLng( user.lat, user.lon );
   var marker = new GMarker( point, memIcon );
   
   // Add a handler to pop up the information for the user.
   GEvent.addListener( marker, "click", function () {
                          marker.openInfoWindowHtml( user.poptext ) ; 
                       } );
   
   // Add the marker to the map.
   map.addOverlay( marker );
   
   // Save the marker against the user.
   user.marker = marker;

   // Add the user to the user list.
   ulist.innerHTML += "<li><a id=\"u" + user.bbuid + "\" href=\"javascript:void(0)\" title=\"Click here to find this user\">" + user.bbid + "</a></li>";
}

// Link the user name in the user list to their marker in the map.
function linkifyUser( user )
{
   document.getElementById( "u" + user.bbuid ).onclick =
      function() { user.marker.openInfoWindowHtml( user.poptext ) };
}

// Add a place of interest to the map.
function addPoI( poilist, map, id, poi )
{
   var point  = new GLatLng( poi.lat, poi.lon );
   var marker = new GMarker( point, PoIIcon );

   // Add a handler to pop up the information for the PoI.
   GEvent.addListener( marker, "click", function ()  {
                          marker.openInfoWindowHtml( poi.poptext ) ; 
                       } );

   // Add the marker to the map.
   map.addOverlay( marker );

   // Save the PoI marker to the map.
   poi.marker = marker;

   // Add the PoI to the PoI list.
   poilist.innerHTML += "<li><a id=\"poi" + id + "\" href=\"javascript:void(0)\" title=\"Click here to find this place of interest\">" + poi.title + "</a></li>";
}

// Link the PoI names in the PoI list to their markers in the map.
function linkifyPoI( id, poi )
{
   document.getElementById( "poi" + id ).onclick =
      function() { poi.marker.openInfoWindowHtml( poi.poptext ) };
}

// Main body of code. This is called as the "onload" code for the
// body of the page.
function displaySPAMap( highlight )
{
   // Create the map object.    
   var map = new GMap2( document.getElementById( "map" ) );
   // Get the user list.
   var ulist = document.getElementById( "userList" );
   // Get the places-of-interest list.
   var poilist = document.getElementById( "PoIList" );

   // Configure the map.
   map.enableContinuousZoom();
   new GKeyboardHandler( map );
   
   // Add the map control.
   map.addControl( new GLargeMapControl() );
   
   // Add the map type control.
   map.addControl( new GMapTypeControl() );
   
   // Code for displaying the location of the center of the map.
   GEvent.addListener( map, "move", function() {
                          var center = map.getCenter();
                          document.getElementById( "centerLoc" ).innerHTML = 
                             "Centre at Lat: " + center.lat().toFixed( 5 ) + 
                             " Lon: " + center.lng().toFixed( 5 );
                       } );
   
   // Initial display.
   map.setCenter( new GLatLng( 51.09, 0.0 ), 5 ); 

   // Create the array of users.
   var users = new Array( new BBUser( "Paul Sutherland",        4, 51.46158,  -0.21547,  "" ),
                          new BBUser( "joe",                    9, 48.861,     2.311,    "http://img70.imageshack.us/img70/7316/mypic9lo.jpg" ),
                          new BBUser( "jeff.stevens",          13, 52.967,    -2.183,    "http://www.users.waitrose.com/~spa/Images/jeffpic.jpg" ),
                          new BBUser( "Kaustav",               17, 51.569,    -0.353,    "" ),
                          new BBUser( "mark_smith",            21, 53.613,    -0.209,    "" ),
                          new BBUser( "Norman Crompton",       39, 53.830795, -2.3388,   "http://mysite.wanadoo-members.co.uk/my-observatory/images/5-picture1.gif" ),
                          new BBUser( "Alan Harding",          41, 51.29,     -0.3,      "" ),
                          new BBUser( "astroeddie",            57, 54.701763, -1.194656, "" ),
                          new BBUser( "Phil Rice",             61, 51.39,     -0.41,     "" ),
                          new BBUser( "jeremyll33",           104, 47.883333, 11.42,     "" ),
                          new BBUser( "paulestar",            109, 52.5,       6.083,    "" ),
                          new BBUser( "Davej",                111, 53.3538,   -1.2139,   "" ),
                          new BBUser( "Hampshire Astronomer", 121, 50.994716, -1.491673, "" ),
                          new BBUser( "Eclipse",              140, 37.963,    -0.738,    "" ),
                          new BBUser( "mkp",                  154, 51.40266,  -0.19265,  "" ),
                          new BBUser( "Ian Lee",              165, 54.890,    -2.943,    "" ),
                          new BBUser( "Cumbrian",             185, 51.6650,   -0.4020,   "" ),
                          new BBUser( "Kendal Astronomer",    204, 51.5157,   -0.1215,   "" ),
                          new BBUser( "davep",                217, 52.893577, -0.34049,  "http://www.davep.org/images/davep-sq-80x80.jpg" ),
                          new BBUser( "trekker308",           225, 50.84732,  -1.79397,  "" ),
                          new BBUser( "Geoff Knight",         248, 50.9493,   -2.6268,   "" ),
                          new BBUser( "ROY",                  263, 53.569722, -1.446389, "" ),
                          new BBUser( "spodzone",             275, 56.3904,   -3.4423,   "" ), 
                          new BBUser( "john.wheeldon",        276, 51.731,     0.413,    "" ), 
                          new BBUser( "Bob",                  298, 51.24152,  -1.10303,  "" ), 
                          new BBUser( "Vega",                 329, 51.1287,   -3.0262,   "" ), 
                          new BBUser( "Chris_Barlow",         341, 53.1636,   -2.2041,   "" ),
                          new BBUser( "austin",               345, 50.9181,   -2.6177,   "" ),
                          new BBUser( "HippyChippy",          361, 51.017954, -1.470056, "" ),
                          new BBUser( "orion f6.3",           388, 51.363533,  0.6015,   "" ),
                          new BBUser( "brucewoods",           408, 51.477222,  0.0,      "" ),
                          new BBUser( "Parkins",              425, 52.9056,   -1.5155,   "" ),
                          new BBUser( "AJ",                   434, 53.323017, -3.459706, "" ),
                          new BBUser( "Mogget",               448, 54.205,    -3.07,     "" ),
                          new BBUser( "Jacqui",               484, 53.59846,  -2.21705,  "" ),
                          new BBUser( "Gordon Copestake",     534, 52.99205,  -2.21742,  "" ),
                          new BBUser( "garymcgrory",          550, 55.6094,   -4.4953,   "" ),
                          new BBUser( "RL Astro",             551, 52.480,    -0.918,    "" ),
                          new BBUser( "andy turner",          589, 51.686,    -3.382,    "" ),
                          new BBUser( "mapofthedead",         668, 52.66,      0.16,     "" ),
                          new BBUser( "Locutus",              694, 51.78808,  -4.97025,  "" ),
                          new BBUser( "met2man",              723, 51.6509,   -1.5920,   "" ),
                          new BBUser( "Paul S",               832, 44.1425,    4.448,    "" ),
                          new BBUser( "GarethD",              833, 52.988889, -1.450556, "" ),
                          new BBUser( "Michael Hezzlewood",   846, 53.793287, -2.264117, "" ),
                          new BBUser( "D-Alien",              859, 51.5221,   -0.4128 ,  "" ),
                          new BBUser( "Adee",                 889, 54.68306,  -3.46799 , "" ),
                          new BBUser( "Brian W",              916, 53.76628,  -1.85480 , "" ),
                          new BBUser( "maccers",              932, 51.01,     -3.11,     "" ),
                          new BBUser( "carlos dfc",           942, 54.625,    -1.582,    "" ),
                          new BBUser( "moonie",               965, 50.940474, -1.385307, "" ),
                          new BBUser( "capella",             1025, -31.8202, 115.7912,   "" ),
                          new BBUser( "beamer3.6m",          1113, 51.281,    -1.071,    "" ),
                          new BBUser( "storebror",           1125, 51.814,    -0.8181,   "" ),
                          new BBUser( "zx9rsteve",           1154, 51.122,    -3.0,      "" ),
                          new BBUser( "Gene",                1156, 51.1222,   -3.0001,   "" ),
                          new BBUser( "Themightyimp",        1159, 53.205647, -0.564272, "" ),
                          new BBUser( "Andrew",              1164, 51.466547, -3.216677, "" ),
                          new BBUser( "Graham",              1200, 53.73627,  -1.65312,  "" ),
                          new BBUser( "Paul",                1213, 50.8430,   -0.1320,   "" ),
                          new BBUser( "NiteGawper",          1216, 51.55750,  -1.73951,  "" ),
                          new BBUser( "chill",               1233, 53.3955,   -2.2070,   "" ),
                          new BBUser( "Zeke",                1266, 53.570944, -2.69017,  "" ),
                          new BBUser( "Malpine Walis",       1285, 41.1255,   -73.5507,  "" ),
                          new BBUser( "adlerhn",             1300, 40.459564, -3.677613, "" ),
                          new BBUser( "FrankB",              1362, 38.82444,   0.08577,  "" ),
                          new BBUser( "stewart",             1409, 58.5,      -3.5,      "" ),
                          new BBUser( "Paul Smith",          1411, 50.94623,  -2.62906,  "" ),
                          new BBUser( "martinss",            1560, 51.1498,   -0.9769,   "" ),
                          new BBUser( "puma2020",            1599, 53.04173,  -2.32331,  "" ),
                          new BBUser( "Thunderchild",        1758, 52.62336,  -1.610109, "" ),
                          new BBUser( "loruntz135",          2542, 50.799046, -1.155667, "" ),
                          new BBUser( "nas76",               2823, 51.67,     -0.4,      "" ),
                          new BBUser( "JohnH",               3007, 54.385031, -2.338414, "" ),
                          new BBUser( "cjones586",           3031, 52.25150,  -0.11494,  "" ),
                          new BBUser( "geitejager",          3608, 52.710273,  6.437216, "" ),
                          new BBUser( "mike a feist",        3778, 50.853,    -0.217,    "" ),
                          new BBUser( "antares6iron",        4118, 52.45205,  -2.08127,  "" ),
                          new BBUser( "cosmic dave",         4798, 53.308639,  0.280961, "" ),
                          new BBUser( "richardw",            5217, 53.79202,  -1.69889,  "" )
                        );
   // Create the array of places of interest.
   var poi = new Array( new PoI( 53.09,     -7.91,     "Birr Castle",                  "http://www.birrcastle.com/" ),
                        new PoI( 52.65,     -1.13,     "National Space Centre",        "http://www.spacecentre.co.uk/" ),
                        new PoI( 52.80912,  -0.62987,  "Woolsthorpe Manor",            "http://www.nationaltrust.org.uk/main/w-vh/w-visits/w-findaplace/w-woolsthorpemanor/" ),
                        new PoI( 51.47722,   0.0,      "Royal Observatory, Greenwich", "http://www.nmm.ac.uk/" ),
                        new PoI( 53.236250, -2.307139, "Jodrell Bank Observatory",     "http://www.jb.man.ac.uk/" ),
                        new PoI( 51.522296, -0.12924,  "SPA Meeting Venue",            "http://www.popastro.com/meetings.htm" ),
                        new PoI( 52.9296,    1.1509,   "Kelling Heath",                "http://www.starparty.org/" ),
                        new PoI( 55.921826, -3.177353, "Royal Observatory, Edinburgh", "http://www.roe.ac.uk/" ),
                        new PoI( 51.382361, -2.366524, "William Herschel Museum",      "http://www.bath-preservation-trust.org.uk/museums/herschel/" ),
                        new PoI( 53.712533, -2.1439,   "Astronomy Centre",             "http://www.astronomycentre.org.uk/" ),
                        new PoI( 51.550618,  0.715688, "Southend Planetarium",         "http://www.southendmuseums.co.uk/" ),
                        new PoI( 50.826738, -0.778238, "South Downs Planetarium",      "http://www.southdowns.org.uk/sdpt/" ),
                        new PoI( 52.33235,  -3.02816,  "Spaceguard Centre",            "http://www.spaceguarduk.com/centre.htm" ),
                        new PoI( 50.684869, -3.219659, "Norman Lockyer Observatory",   "http://www.projects.ex.ac.uk/nlo/" )
                      );

   // Sort the user array into alphabetical order.
   users.sort( function ( a, b ) {
                  if ( a.bbid.toLowerCase() < b.bbid.toLowerCase() )
                     return -1;
                  else if ( a.bbid.toLowerCase() > b.bbid.toLowerCase() )
                  return 1;
                  else
                     return 0;
               } );

   // Sort the PoI array into alphabetical order.
   poi.sort( function ( a, b ) {
                if ( a.title.toLowerCase() < b.title.toLowerCase() )
                   return -1;
                else if ( a.title.toLowerCase() > b.title.toLowerCase() )
                return 1;
                else
                   return 0;
             } );
   
   // Add the users to the display.
   for ( var i = 0; i < users.length; i++ )
   {
      addUser( ulist, map, users[ i ] );
   }

   // Now create the links from the list to the map.
   for ( var i = 0; i < users.length; i++ )
   {
      linkifyUser( users[ i ] );
   }

   // Add the places of interest to the display.
   for ( var i = 0; i < poi.length; i++ )
   {
      addPoI( poilist, map, i, poi[ i ] );
   }

   // Now create the links from the list to the map.
   for ( var i = 0; i < poi.length; i++ )
   {
      linkifyPoI( i, poi[ i ] );
   }

   // Finally, if we've been asked to highlight someone -- highlight them.
   if ( highlight > 0 )
   {
      var huser = null;
      
      // Find them.
      for ( var i = 0; i < users.length; i++ )
      {
         if ( users[ i ].bbuid == highlight )
         {
            huser = users[ i ];
            break;
         }
      }

      // Did we find them?
      if ( huser != null )
      {
         // We did. Highlight them.
         huser.marker.openInfoWindowHtml( huser.poptext );
      }
   }
}
