//<![CDATA[
  // For: Google Maps API Version 2
  // 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/map.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 GMap2(document.getElementById("map"));
    var request = GXmlHttp.create();
    request.open("GET", url, true);
    request.onreadystatechange = function() 
    {
     document.getElementById("loading").innerHTML = "Loading, please wait..." ;
     document.getElementById("loading2").innerHTML = "Loading, please wait..." ;
     document.getElementById("loading3").innerHTML = "Loading, please wait..." ;
     document.getElementById("loading4").innerHTML = "Loading, please wait..." ;
     document.getElementById("loading5").innerHTML = "Loading, please wait..." ;
	 document.getElementById("loading6").innerHTML = "Loading, please wait..." ;

     if (request.readyState == 4)
     {
      var xmlDoc = request.responseXML ;
      try
      {
       var mapattr = xmlDoc.getElementsByTagName("map")
       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 = 98.330747 ;
       var lat = 7.984311 ;
       var zoom = 10 ;
       try
       {
        var mapcenter = xmlDoc.getElementsByTagName("mapcenter")
        try 					// Set Map Center Longitude
        {
         lng = parseFloat(mapcenter[0].getAttribute("lng")) ;
        } catch(e) { }

        try 					// Set Map Center Latitude
        {
         lat = parseFloat(mapcenter[0].getAttribute("lat")) ;
        } catch(e) { }

        try 					// Set Initial Zoom Level
        {
         zoom = mapcenter[0].getAttribute("zoom") ;
        } catch(e) { }

        if ( zoom == 'auto' )	// Zoom and center calculated from Bounds
        {
         try
         {
          var slat = parseFloat(mapcenter[0].getAttribute("slat")) ;
          var nlat = parseFloat(mapcenter[0].getAttribute("nlat")) ;
          var elng = parseFloat(mapcenter[0].getAttribute("elng")) ;
          var wlng = parseFloat(mapcenter[0].getAttribute("wlng")) ;

          lat = (slat + nlat)/2 ;
          lng = (elng + wlng)/2 ;

          zoom = map.getBoundsZoomLevel(new GLatLngBounds(new GLatLng(slat,wlng), new GLatLng(nlat,elng))) ;

         } catch(e) { }
        } else
        {
         zoom = parseInt(zoom) ;
        }
       } catch(e) { } 

       if (lng == 'NaN')
       {
        lng = 98.330747 ;
       }
       if (lat == 'NaN')
       {
        lat = 7.984311 ;
       }
       if (zoom == 'NaN')
       {
        zoom = 10 ;
       }

       map.setCenter(new GLatLng(lat,lng),zoom) ;

       try 					// Build Polylines, if available
       {
        var polyline = xmlDoc.getElementsByTagName("polyline") ;
        for ( var i = 0; i < polyline.length ; i++ )
        {
         var color = "#0000ff" ;
         try 					// Set Polyline Color
         {
          color = polyline[i].getAttribute("color") ;
         } catch(e) { }

         var weight = .5 ;
         try 					// Set Polyline Weight
         {
          weight = parseFloat(polyline[i].getAttribute("weight")) ;
         } catch(e) { }

         var width = 1 ;
         try 					// Set Polyline Width
         {
          width = parseInt(polyline[i].getAttribute("width")) ;
         } catch(e) { }
 
         var points = [ ] ;
         var point = polyline[i].getElementsByTagName("point") ;

         for ( j = 0 ; j < point.length ; j++ )	// Get Polyline Points - Required for Polyline
         {
          points.push( new GLatLng(parseFloat(point[j].getAttribute("lat")),parseFloat(point[j].getAttribute("lng"))) ) ;
         }
         map.addOverlay( new GPolyline( points, color, width, weight ) ) ;
        }
       } catch(e) { }				// No Polylines or error in XML

       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 GLatLng( parseFloat(markers[i].getAttribute("lat")),parseFloat(markers[i].getAttribute("lng")) ) ;

         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 ;
		  var group = markers[i].getElementsByTagName("group")[0].firstChild.nodeValue ;
		  switch (group)
			{
			case "Phuket City":
				$("div#phuket_city").append(" <a onclick='infoLink("+i+");'>" + html + "</a>");
			  break;
			case "South East":
			  document.getElementById("south_east").innerHTML += " <a onclick='infoLink("+i+");'>" + html + "</a>" ;
			  break;
			case "Kata Karon":
			  document.getElementById("kata_karon").innerHTML += " <a onclick='infoLink("+i+");'>" + html + "</a>" ;
			  break;
			case "Patong Kalim":
			  document.getElementById("patong_kalim").innerHTML += " <a onclick='infoLink("+i+");'>" + html + "</a>" ;
			  break;
			  case "Central":
			  document.getElementById("central").innerHTML += " <a onclick='infoLink("+i+");'>" + html + "</a>" ;
			  break;
			case "North":
			  document.getElementById("north").innerHTML += " <a onclick='infoLink("+i+");'>" + html + "</a>" ;
			  break;
			}
         } catch (e) { }
        }
       } catch(e) { }				// No Markers or error in XML
      } catch(e)
      {
       alert("Some error occured during program processing:" + e) ;
      }       
      document.getElementById("loading").innerHTML = "" ;
      document.getElementById("loading2").innerHTML = "" ;
      document.getElementById("loading3").innerHTML = "" ;
      document.getElementById("loading4").innerHTML = "" ;
      document.getElementById("loading5").innerHTML = "" ;
	  document.getElementById("loading6").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");
	$('html, body').animate({scrollTop:280}, 1500);
	/*document.write(marar[i]);
	map = new GMap2(document.getElementById("map"));
	map.setCenter(new GLatLng(lat,lng),15) ;*/
   }
//]]>
