var map;
var bounds = new GLatLngBounds();
var bAddedMarkers=false;

function loadGoogleMap(xmlString) 
{     
      if (GBrowserIsCompatible()) {
               //var url=document.URL;
               //if (url.indexOf("MapViewServlet")>=0){
               //map div is the smaller one
               if (document.getElementById("map")!=null){
                  map = new GMap2(document.getElementById("map"));//,{ size: new GSize(300,350) } 
	       }else if (document.getElementById("left_mapview")!=null){ //mapcontainer is on the mapview.jsp much larger
                  map=new GMap2(document.getElementById("left_mapview"));
               }else
                   return;
               
                var dublin = new GLatLng(53.386522, -6.255469); 
		map.setCenter(dublin, 13);
	
		var longitude;
                var latitude;
                var name;
                var url;
                rootElement=convertStringToDom(xmlString);
                    
                var gyms = GetElementsByTagName(rootElement,'gym');
                for (var index = 0; index < gyms.length; index++) {
                    var gym = gyms[index];
                    
                    longitude=GetElementTextByTagName(gym,'longitude');
                    latitude=GetElementTextByTagName(gym,'latitude');
                    var point;
                    name=GetElementTextByTagName(gym,'name');
                    url=GetElementTextByTagName(gym,'url');
                    if (longitude==null || eval(longitude)==0 || latitude==null || eval(longitude)==0){
                        //  var address='10 market st, san francisco';
                        
                        //var address=GetElementTextByTagName(gym,'address1')+" ";
                        //address=address+GetElementTextByTagName(gym,'address2')+" ";
                        //address=address+GetElementTextByTagName(gym,'town')+" ";
                        //address=address+GetElementTextByTagName(gym,'areacode')+" ";
                        //address=address+GetElementTextByTagName(gym,'county')+" ";
                        //address=address+'Ireland';
                        //getCordinates(address,name,url);
                    }else{
                        point= new GLatLng(latitude,longitude);
                        bounds.extend(point);
                        createMarker(point, name, url);
                    }
                      
                }
                
              
                 // add the map control i.e for zooming in and out
		// adds the diiferent views i.e. map satellite, hybrid
		var mapControl = new GMapTypeControl();
		map.addControl(mapControl);
		map.addControl(new GLargeMapControl());
               
                
                zoomFit();
               
   	  }
  }
  
 function zoomFit(){
      //if there have been no markers added return, as the zoom fit will zoom out to a world wide level other wise
     if (!bAddedMarkers) return; 
     
     //http://mapki.com/wiki/APIv2 and http://au.answers.yahoo.com/question/index?qid=20080330214639AAccGou
      var newzoom = map.getBoundsZoomLevel (bounds);
      if (newzoom>=17)
          newzoom=13;
      var newcenter = bounds.getCenter();
      
      map.setCenter (newcenter,newzoom);
  } 

  function createMarker(point,gymName,url) 
  { 

      var marker = new GMarker(point); 
      
      var eventString="click";
      if (document.getElementById("mapcontainer")!=null){
        eventString="mouseover";
      }        
      
      GEvent.addListener(marker, eventString, function()
      {   
             //encodeURI handles all spaces and non alphanumeric characters, etc. browser automatically decodes 
           var myHtml =null
             
         
          // if (logo!=null)
          //    myHtml=EncodeXml(EncodeXml(EncodeXml(logo,false),false),false) + "<br/>";
          
           if (url==null || url.length==0)
            myHtml="<b>"+gymName +"</b>";
           else
            myHtml="<b>Link</b><br/><a href=" + encodeURI(url) +">"+gymName+"</a>";
           
      map.openInfoWindowHtml(point, myHtml);  
      });
      map.addOverlay(marker); 
      bAddedMarkers=true;         
}

function HighLightOnMap(longitude,latitude,name,link)  {
    /*IE executes the rollover before map the body load method if mouse is on a div 
    * So map would be still null. therefore got a JS error. check is mapp==null here fixes it
    */
    if (map==null ||longitude==null || eval(longitude)==0 || latitude==null || eval(latitude)==0)
        return;
    
     point= new GLatLng(latitude,longitude);
     var myHtml =  "<b>Link</b><br/><a href=" + link +">"+name+"</a>";
     map.openInfoWindowHtml(point, myHtml);  
}

function getCordinates(address,gymName,url) 
  { 
      
      var geocoder = new GClientGeocoder();
      geocoder.getLatLng(
            address,
            function(point) {
               if (!point)
                  alert(address + ' Not Found');
              else{
                  createMarker(point,gymName,url);
                  bounds.extend(point);
                  zoomFit();
                  }
               
            }
        );
       
               
}
	
  