//<![CDATA[

//  version 2.0.1

var map;

var toMarkers = [];
var fromMarkers = [];
var captions = [];
var blurbs = [];
var polylines = [];
var pix = [];
var map_center_lat = 48.91;
var map_center_long = -123.59;
var map_center_scale = 7;

function responseOK( responseCode, dataName )
{
 if ( responseCode == 200 )
 {
  return true;
 }
 else if ( responseCode == -1 )
 {
  alert( dataName + " data request timed out!");
 }
 else
 { 
  alert( dataName + " request resulted in error!");
 }
}

function zLevelOne( marker, b )
{
 return 1;
}

function zLevelTwo( marker, b )
{
 return 2;
}

function createMarkers()
{
 var k = new Date();
 var file = "xml/markers.xml?k=" + k.getTime();
 GDownloadUrl(
  file,
  function( data, responseCode )
  {
   if ( responseOK( responseCode, "markers.xml" ) )
   {
    var fromBaseIcon = new GIcon();
    fromBaseIcon.shadow = "http://maps.google.com/mapfiles/kml/pal4/icon6s.png";
    fromBaseIcon.iconSize = new GSize( 32, 32 );
    fromBaseIcon.shadowSize = new GSize( 59, 32 );
    fromBaseIcon.iconAnchor = new GPoint( 16, 32 );
    fromBaseIcon.infoWindowAnchor = new GPoint( 16, 0 );
    var fromIcon = new GIcon( fromBaseIcon );
    fromIcon.image = "http://maps.google.com/mapfiles/kml/pal5/icon6.png";

    var toBaseIcon = new GIcon();
    toBaseIcon.shadow = "http://maps.google.com/mapfiles/kml/pal5/icon13s.png";
    toBaseIcon.iconSize = new GSize( 32, 32 );
    toBaseIcon.shadowSize = new GSize( 59, 32 );
    toBaseIcon.iconAnchor = new GPoint( 16, 32 );
    toBaseIcon.infoWindowAnchor = new GPoint( 16, 0 );
    var toIcon = new GIcon( toBaseIcon );
    toIcon.image = "http://maps.google.com/mapfiles/kml/pal5/icon13.png";

    var xml = GXml.parse( data );
    var ports = xml.documentElement.getElementsByTagName( "marker" );
    for (var i = 0; i < ports.length; i++)
    {
     var tip = ports[i].getAttribute( "tip" );
     var fromOptions = { title: tip, icon:fromIcon, zIndexProcess:zLevelTwo };
     var toOptions = { title: tip, icon:toIcon, zIndexProcess:zLevelTwo };
     var marker = new GMarker( new GLatLng( parseFloat( ports[i].getAttribute( "lat" ) ), parseFloat( ports[i].getAttribute( "lng" ) ) ), fromOptions );
     marker.bindInfoWindowHtml( GXml.value( ports[i] ) );
     fromMarkers[i] = marker;
     map.addOverlay( marker );
     fromMarkers[i].hide();
     marker = new GMarker( new GLatLng( parseFloat( ports[i].getAttribute( "lat" ) ), parseFloat( ports[i].getAttribute( "lng" ) ) ), toOptions );
     marker.bindInfoWindowHtml( GXml.value( ports[i] ) );
     toMarkers[i] = marker;
     map.addOverlay( marker );
     toMarkers[i].hide();
    }
   }
  }
             );
}

function createDays()
{
 var k = new Date();
 var file = "xml/days.xml?k=" + k.getTime();
 GDownloadUrl(
  file,
  function( data, responseCode )
  {
   if ( responseOK( responseCode, "days.xml" ) )
   {
    var xml = GXml.parse( data );
    var days = xml.documentElement.getElementsByTagName( "day" );
    for (var i = 0; i < days.length; i++)
    {
     captions[i] = days[i].getAttribute( "caption" );
     blurbs[i] = GXml.value( days[i] );
    }
    document.getElementById( "caption" ).innerHTML = captions[0];
    document.getElementById( "blurb" ).innerHTML = blurbs[0];
   }
  }
             );
}

function createRoutes()
{
 var k = new Date();
 var file = "xml/lines.xml?k=" + k.getTime();
 GDownloadUrl(
  file,
  function( data, responseCode )
  {
   if ( responseOK( responseCode, "lines.xml" ) )
   {
    var xml = GXml.parse( data );
    var lines = xml.documentElement.getElementsByTagName( "polyline" );

    for (var i = 0; i < lines.length; i++)
    {
     var color = lines[i].getAttribute( "color" );
     var width = parseFloat( lines[i].getAttribute( "width" ) );
     var opacity = parseFloat( lines[i].getAttribute( "opacity" ) );
     var points = GXml.value( lines[i] );
     var pts = points.split( '\n' );
     var latlongs = [];
     var k = 0;
     for(var j=0; j<pts.length; j++)
     {
      if ( pts[j].length > 0 )
      {
       var ll = pts[j].split( ',' );
       latlongs[k] = new GLatLng( parseFloat( ll[1] ), parseFloat( ll[0] ) );
       k++;
      }
     }
     polylines[i] = new GPolyline( latlongs, color, width, opacity );
     map.addOverlay( polylines[i] );
    }
   }
  }
             );
}

function createPix()
{
 var k = new Date();
 var file = "xml/pix.xml?k=" + k.getTime();
 GDownloadUrl(
  file,
  function( data, responseCode )
  {
   if ( responseOK( responseCode, "pix.xml" ) )
   {
    var xml = GXml.parse( data );

    var baseIcon = new GIcon();
    baseIcon.shadow = "http://maps.google.com/mapfiles/kml/pal4/icon46s.png";
    baseIcon.iconSize = new GSize( 32, 32 );
    baseIcon.shadowSize = new GSize( 59, 32 );
    baseIcon.iconAnchor = new GPoint( 16, 32 );
    baseIcon.infoWindowAnchor = new GPoint( 16, 0 );
    var cameraIcon = new GIcon( baseIcon );
    cameraIcon.image = "http://maps.google.com/mapfiles/kml/pal4/icon46.png";

    var pics = xml.documentElement.getElementsByTagName( "marker" );
    for (var i = 0; i < pics.length; i++)
    {
     var tip = pics[i].getAttribute( "tip" );
     var options = { title: tip, icon:cameraIcon, zIndexProcess:zLevelOne };
     var marker = new GMarker( new GLatLng( parseFloat( pics[i].getAttribute( "lat" ) ), parseFloat( pics[i].getAttribute( "lng" ) ) ), options );
     marker.bindInfoWindowHtml( GXml.value( pics[i] ) );
     pix[i] = marker;
     map.addOverlay( marker );
    }
   }
  }
             );
}

function load()
{
 if ( !GBrowserIsCompatible() )
 {
  alert( "The Google Maps API is not compatible with this browser!" );
 }
 else
 {
  map = new GMap2( document.getElementById( "map" ) );

  document.getElementById("map").style.backgroundImage = "url(images/map-back.jpg)";

  map.addControl( new GLargeMapControl() );
  map.addControl( new GScaleControl() );
  map.addControl( new GMapTypeControl() );
  map.setCenter( new GLatLng( map_center_lat, map_center_long ), map_center_scale );
  map.setMapType( G_SATELLITE_MAP );

  createDays();
  createRoutes();
  createPix();
  createMarkers();
 }
}


function hideRoutes()
{
 for (var i = 0; i < polylines.length; i++)
 {
  polylines[i].hide();
 }
}

function showRoutes()
{
 for (var i = 0; i < polylines.length; i++)
 {
  polylines[i].show();
 }
}

function hideMarkers()
{
 for (var i = 0; i < fromMarkers.length; i++)
 {
  fromMarkers[i].hide();
  toMarkers[i].hide();
 }
}

function showDay( day )
{
 hideMarkers();
 if ( day > 0 )
 {
  hideRoutes();
  polylines[day-1].show();
 }
 else
 {
  showRoutes();
 }

 document.getElementById( "caption" ).innerHTML = captions[day];
 document.getElementById( "blurb" ).innerHTML = blurbs[day];

 if ( day >= 1 && day <= 24 )
 {
  fromMarkers[day-1].show();
  toMarkers[day].show();
  switch ( day )
  {
   case 1 :
    map.setCenter( new GLatLng( 47.78, -122.54 ), 10 );
    break;
   case 2 :
    map.setCenter( new GLatLng( 48.23, -122.83 ), 9 );
    break;
   case 3 :
    map.setCenter( new GLatLng( 48.54, -122.99 ), 12 );
    break;
   case 4 :
    map.setCenter( new GLatLng( 48.58, -123.01 ), 12 );
    break;
   case 5 :
    map.setCenter( new GLatLng( 48.71, -123.16 ), 10 );
    break;
   case 6 :
    map.setCenter( new GLatLng( 48.89, -123.49 ), 10 );
    break;
   case 7 :
    map.setCenter( new GLatLng( 49.06, -123.68 ), 10 );
    break;
   case 8 :
    map.setCenter( new GLatLng( 49.33, -123.83 ), 9 );
    break;
   case 9 :
    map.setCenter( new GLatLng( 49.57, -124.00 ), 10 );
    break;
   case 10 :
    map.setCenter( new GLatLng( 49.72, -124.28 ), 10 );
    break;
   case 11 :
    map.setCenter( new GLatLng( 49.94, -124.65 ), 10 );
    break;
   case 12 :
    map.setCenter( new GLatLng( 50.10, -124.85 ), 10 );
    break;
   case 13 :
    map.setCenter( new GLatLng( 49.88, -124.48 ), 9 );
    break;
   case 14 :
    map.setCenter( new GLatLng( 49.39, -123.87 ), 9 );
    break;
   case 15 :
    map.setCenter( new GLatLng( 49.02, -123.55 ), 10 );
    break;
   case 16 :
    map.setCenter( new GLatLng( 48.76, -123.15 ), 10 );
    break;
   case 17 :
    map.setCenter( new GLatLng( 48.76, -123.15 ), 10 );
    break;
   case 18 :
    map.setCenter( new GLatLng( 48.76, -123.15 ), 10 );
    break;
   case 19 :
    map.setCenter( new GLatLng( 48.76, -123.15 ), 10 );
    break;
   case 20 :
    map.setCenter( new GLatLng( 48.76, -123.15 ), 10 );
    break;
   case 21 :
    map.setCenter( new GLatLng( 48.76, -123.15 ), 10 );
    break;
   case 22 :
    map.setCenter( new GLatLng( 48.58, -122.93 ), 10 );
    break;
   case 23 :
    map.setCenter( new GLatLng( 48.41, -122.75 ), 10 );
    break;
   case 24 :
    map.setCenter( new GLatLng( 47.97, -122.52 ), 9 );
    break;
   default :
    map.setCenter( new GLatLng( map_center_lat, map_center_long ), map_center_scale );
    break;
  }
 }
 else
 {
  map.setCenter( new GLatLng( map_center_lat, map_center_long ), map_center_scale );
 }
}
//]]>
