  //<![CDATA[

  // Copyright (C) 2006 John D. Coryat
  // This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
  // This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  // You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

   var marar = [ ] ;					// Marker Array
   var map ;
   var iconaa = [ ] ;					// Icon Associative Array

   function LoadMap()
   {
    if (GBrowserIsCompatible()) 			// Do Map if Compatible Browser only
    {
     if ( location.search.length > 1 )
     {
      genericmap( location.search.substring(1,location.search.length) ) ;
     } else
     {
      genericmap( "xml/fleenordistributorlocations.xml" ) ; ;
     }
    } else
    {
     document.getElementById("map").innerHTML = "<h1>Browser not compatible with Google Maps. Sorry...</h1>" ;
    }
   }

// Build Icons and Draw Polylines and Markers

   function genericmap( url )
   {
    map = new GMap(document.getElementById("map"));
    var request = GXmlHttp.create();
    request.open("GET", url, true);
    request.onreadystatechange = function() 
    {
     document.getElementById("loading").innerHTML = "Loading, please wait..." ;

     if (request.readyState == 4)
     {
      var xmlDoc = request.responseXML ;
      try
      {
       mapattr = xmlDoc.getElementsByTagName("map")
       map.addControl(new GSmallMapControl());
       //map.addControl(new GLargeMapControl());
       //map.addControl(new GMapTypeControl());
       //map.addControl(new GScaleControl()) ;

       try 					// Set Title
       {
        document.title = mapattr[0].getAttribute("title") ;
       } catch(e) { }

       var lng = -113.421217627648 ;
       try 					// Set Map Center Longitude
       {
        lng = parseFloat(mapattr[0].getAttribute("lng")) ;
       } catch(e) { }

       var lat = 47.7685850162875 ;
       try 					// Set Map Center Latitude
       {
        lat = parseFloat(mapattr[0].getAttribute("lat")) ;
       } catch(e) { }

       var zoom = 14 ;
       try 					// Set Initial Zoom Level
       {
        zoom = parseInt(mapattr[0].getAttribute("zoom")) ;
       } catch(e) { }

       map.centerAndZoom(new GPoint( lng,lat ), zoom);

       try 					// Build Icons, if available
       {
        var icons = xmlDoc.getElementsByTagName("icon") ;
        for ( var i = 0; i < icons.length ; i++ )
        {
         var name = icons[i].getAttribute("name") ;		// Required: icon name

         var x = "" ;				// Test Variable
         var y = "" ;				// Test Variable

         try 					// Check for an icon copy
         {
          x = icons[i].getAttribute("copy") ;
         } catch(e) { }

         if ( x && iconaa[x] )			// Make sure icon to copy has been built
         {
          var icon = new GIcon(iconaa[x]) ;
         } else
         {
          var icon = new GIcon( ) ;
         }

         icon.image = icons[i].getElementsByTagName("image")[0].getAttribute("src") ;	// Required: Icon Image

         x = "" ;
         y = "" ;
         try 					// Check for an icon anchor
         {
          x = icons[i].getAttribute("xanchor") ;
          y = icons[i].getAttribute("yanchor") ;
         } catch(e) { }

         if ( x != null && y!= null && x && y )
         {
          icon.iconAnchor = new GPoint( parseInt(x),parseInt(y) ) ;
         }

         x = "" ;
         y = "" ;

         try 					// Check for an icon infowindow anchor
         {
          x = icons[i].getAttribute("xiwanchor") ;
          y = icons[i].getAttribute("yiwanchor") ;
         } catch(e) { }

         if ( x != null && y!= null && x && y )
         {
          icon.infoWindowAnchor = new GPoint( parseInt(x),parseInt(y) ) ;
         }

         x = "" ;
         y = "" ;

         try 					// Check for an icon infoshadow anchor
         {
          x = icons[i].getAttribute("xisanchor") ;
          y = icons[i].getAttribute("yisanchor") ;
         } catch(e) { }

         if ( x != null && y!= null && x && y )
         {
          icon.infoShadowAnchor = new GPoint( parseInt(x),parseInt(y) ) ;
         }

         x = "" ;
         y = "" ;
         try 					// Check for icon image height and width
         {
          x = icons[i].getElementsByTagName("image")[0].getAttribute("width") ;
          y = icons[i].getElementsByTagName("image")[0].getAttribute("height") ;
         } catch(e) { }

         if ( x != null && y!= null && x && y )
         {
          icon.iconSize = new GSize( parseInt(x),parseInt(y) ) ;
         }

         x = "" ;
         try 					// Check for an shadow image
         {
          x = icons[i].getElementsByTagName("shadow")[0].getAttribute("src") ;
         } catch(e) { }

         if ( x != null && x )
         {
          icon.shadow = x ;
         }

         x = "" ;
         y = "" ;
         try 					// Check for an icon shadow dimension
         {
          x = icons[i].getElementsByTagName("shadow")[0].getAttribute("width") ;
          y = icons[i].getElementsByTagName("shadow")[0].getAttribute("height") ;
         } catch(e) { }

         if ( x != null && y!= null && x && y )
         {
          icon.shadowSize = new GSize( parseInt(x),parseInt(y) ) ;
         }
         
         iconaa[name] = icon ;
        }

       } catch (e) { }

       try 					// Build Markers, if available
       {
        var markers = xmlDoc.getElementsByTagName("marker") ;
        for ( var i = 0; i < markers.length ; i++ )
        {
         var point = new GPoint( parseFloat(markers[i].getAttribute("lng")),parseFloat(markers[i].getAttribute("lat")) ) ;

         var iconname ="" ;
         try 					// Look for Icon Name
         {
          iconname = markers[i].getAttribute("iconname") ;
         } catch(e) { }
 
         var html = "" ;
         try 					// Set Polyline Weight
         {
          html = markers[i].getElementsByTagName("infowindow")[0].firstChild.nodeValue ;
         } catch(e) { }

         var marker = createMarker( point, html, iconname ) ;
         marar.push( marker ) ;
         map.addOverlay(marker) ;
         try					// Set link
         {
          html = markers[i].getElementsByTagName("link")[0].firstChild.nodeValue ;
         document.getElementById("sidebar").innerHTML += "<a href='javascript:infoLink(" + i + ");'>" + html + "</a><br>" ;
         } catch (e) { }
        } 
       } catch(e) { }				// No Markers or error in XML

      } catch(e)
      {
       alert("Some error occured during program processing:" + e) ;
      }       
      document.getElementById("loading").innerHTML = "" ;
     }
    }
    request.send(null);

// Create a marker at a point with an infowindow...

    function createMarker(point, html, iconname) 
    {
     if ( iconname && iconaa[iconname] )
     {
      var marker = new GMarker(point, iconaa[iconname]) ;
     } else
     {
      var marker = new GMarker(point) ;
     }
     GEvent.addListener(marker, "click", function()
     {
      marker.openInfoWindowHtml(html);
     });
     return marker;
    }
   }

// Open an Infowindow when the link is clicked in the message div...
        
   function infoLink(i)
   {
    GEvent.trigger(marar[i], "click");
   }

  //]]>