   
var map = "";
var side_bar_html = "";
var gmarkers = [];
var htmls = [];
var ii = 0;
var  map_base = "";
var  img_base = "";
var  subsite  = "";
var icon = ""; 
var mapCenterZoom = "";




addLoadListener(showMap);

function showMap(){

    if (GBrowserIsCompatible()) {
  

      // A function to create the marker and set up the event window
      
       map_base = document.getElementById("mapxmlinfo").innerHTML; 
       img_base = document.getElementById("mapimginfo").innerHTML;  
       subsite  = document.getElementById("mappage").innerHTML; 
     

      // create the map
       map = new GMap2(document.getElementById("map"));
       map.addControl(new GLargeMapControl());
       map.addControl(new GMapTypeControl());
       map.setCenter(new GLatLng(25.244695951306028, 135),4);
      


      // A function to read the data

      
      // When initially loaded, use the data from "map11a.xml"
      if(subsite == "cairns"){      
       readMap("marlin.xml");
      }
      if(subsite == "tablelands"){
        readMap("cape-york.xml");
      }
      
      if(subsite == "port_douglas"){
        readMap("port-douglas.xml");
      }
      
      if(subsite == "central_australia"){
        readMap("alice.xml");
      }
      
      if(subsite == "darwin"){
        readMap("alice.xml");
      }
    }

    else {
      alert("Sorry, the Google Maps API is not compatible with this browser");
    }
    // This Javascript is based on code provided by the
    // Blackpool Community Church Javascript Team
    // http://www.commchurch.freeserve.co.uk/   
    // http://econym.googlepages.com/index.htm

}



     function createMarker(point,name,html) {
     
     
     icon = new GIcon();
      icon.image = img_base + "maps/map-icon.png";
      icon.shadow = img_base + "maps/shadow-map-icon.png";
      icon.iconSize = new GSize(26.0, 33.0);
      icon.shadowSize = new GSize(43.0, 33.0);
      icon.iconAnchor = new GPoint(13.0, 36.0);
      icon.infoWindowAnchor = new GPoint(11.0, 4.0);              
      icon.imageMap = [
        12, 36,
        0, 16,
        0, 11,
        2, 7,
        7, 2,
        10, 0,
        15, 0,
        20, 3,
        24, 7,
        25, 12,
        25, 16,
        23, 20
      ];
     
     
        var marker = new GMarker(point,icon);
        GEvent.addListener(marker, "click", function() {
          marker.openInfoWindowHtml(html);
        });
        gmarkers[ii] = marker;
        htmls[ii] = html;
        side_bar_html += '<td><a href="javascript:myclick(' + ii + ')">' + name + '</a></td>';
        ii++;
        return marker;
      }


      // This function picks up the click and opens the corresponding info window
      function myclick(i) {
        gmarkers[i].openInfoWindowHtml(htmls[i]);
      }      
      
       function readMap(url) {     
       
       
      
      
        
        
        var request = GXmlHttp.create();
        request.open("GET", map_base + url, true);
        request.onreadystatechange = function() {
          if (request.readyState == 4) {
        // alert(request.responseText);
            var xmlDoc = GXml.parse(request.responseText);
            // obtain the array of markers and loop through it 
            
               
      
          
              var markers = xmlDoc.documentElement.getElementsByTagName("marker");
            
          
            
                   
              var mapcenter = xmlDoc.getElementsByTagName("markers");
              
              mapCenterLat  =  mapcenter[0].getAttribute('mapCenterLat');
		 	        mapCenterLng  =  mapcenter[0].getAttribute('mapCenterLng');
              mapCenterZoom =  mapcenter[0].getAttribute("mapCenterZoom");
              map.setCenter(new GLatLng( mapCenterLat,mapCenterLng), parseInt(mapCenterZoom));
              
              //alert(mapCenterZoom);
          
            
            // hide the info window, otherwise it still stays open where the removed marker used to be
            
         
            
            
            map.getInfoWindow().hide();            
            map.clearOverlays(); 
              
            //map.setZoom(parseInt(mapCenterZoom));
            //  alert(mapCenterZoom);       
            
            // empty the arrays
            gmarkers = [];
            htmls = [];
            ii = 0;

            // reset the side_bar
            side_bar_html="";
            side_bar_html += '<table class="maps" cellpadding="5" cellspacing="1"><tr>';
       
            for (var i = 0; i < markers.length; i++) {
              // obtain the attribues of each marker
              var lat = parseFloat(markers[i].getAttribute("lat"));
              var lng = parseFloat(markers[i].getAttribute("lng"));
              var point = new GLatLng(lat,lng);
              var html = '<div class="map_info_desc"><img src="' + img_base +  markers[i].getAttribute("img") + '" width="104" height="104">' + markers[i].getAttribute("html") + '</div>';
              var label = markers[i].getAttribute("label");
              // create the marker
              var marker = createMarker(point,label,html);
              map.addOverlay(marker);
              
            
              
          
            }
            side_bar_html += "</tr></table>";
            
            // put the assembled side_bar_html contents into the side_bar div
            document.getElementById("side_bar").innerHTML = side_bar_html;
            
             blink();
              
//             
//                 GEvent.addListener(map, "moveend", function() {
//           var center = map.getCenter();
//           var zoom = map.getZoom();
//           document.getElementById("message").value = center.toString();
//           document.getElementById("zoom").innerHTML = zoom.toString();
//         });
        

      

          }
        }
        request.send(null);
      }
      
      
      function show()
      {
      	if (document.getElementById)
      	document.getElementById("side_bar").style.visibility = "visible";
      }
      // blink "off" state
      function hide()
      {
      	if (document.getElementById)
      	document.getElementById("side_bar").style.visibility = "hidden";
      }
      // toggle "on" and "off" states every 450 ms to achieve a blink effect
      // end after 2700 ms (less than three seconds)
      function blink(){
      for(var i=900; i < 2700; i=i+900)
      {
      	setTimeout("hide()",i);
      	setTimeout("show()",i+450);
      }
      }
      
      
      
      
      

   
    
    
    
function addLoadListener(fn)
{
  if (typeof window.addEventListener != 'undefined')
  {
    window.addEventListener('load', fn, false);
  }
  else if (typeof document.addEventListener != 'undefined')
  {
    document.addEventListener('load', fn, false);
  }
  else if (typeof window.attachEvent != 'undefined')
  {
    window.attachEvent('onload', fn);
  }
  else
  {
    var oldfn = window.onload;
    if (typeof window.onload != 'function')
    {
      window.onload = fn;
    }
    else
    {
      window.onload = function()
      {
        oldfn();
        fn();
      };
    }
  }
};

