
function trim(str)
{
    if(!str || typeof str != 'string')
        return null;
    return str.replace(/^[\s]+/,'').replace(/[\s]+$/,'').replace(/[\s]{2,}/,' ');
}

function loadGmap()
{

if(document.getElementById("map")!=null) {
	if (GBrowserIsCompatible()) {
		// create the map	
		
		var facilityLongitude = document.getElementById("facilityLongitudeId").value;
		var facilityLatitude = document.getElementById("facilityLatitudeId").value;
		var facilityTelephone = document.getElementById("facilityTelephoneId").value;
		var facilityName = document.getElementById("facilityHomeNameId").value;
	      
				      
		var facilityAddress = document.getElementById("facilityAddr1Id").value + ", "
				      + document.getElementById("facilityAddr2Id").value + ", "
				      + document.getElementById("facilityAddr3Id").value + ", "
				      + document.getElementById("facilityAddr4Id").value + ", "
				      + document.getElementById("facilityCityId").value + ", "
				      + document.getElementById("facilityCountryId").value;
		
		var AmenityLatitude = document.getElementById("amenitiesLatitude").value;
		var AmenityLongitude = document.getElementById("amenitiesLongitude").value;
		var AmenityCode = document.getElementById("amenitiesCode").value;
		var AmenityImageName = document.getElementById("amenitiesImageName").value;
		var AmenityName = document.getElementById("amenitiesName").value;
		var	AmenityType = document.getElementById("amenitiesType").value;
		
		var map = new GMap2(document.getElementById("map"));
		map.addControl(new GLargeMapControl3D());


		map.setCenter(new GLatLng(facilityLatitude,facilityLongitude), 15);
		map.setUIToDefault(); 

		// Marker for Facility
		marker1 = createFacilityMarker(new GLatLng(facilityLatitude, facilityLongitude), facilityName, facilityTelephone, facilityAddress);
		map.addOverlay(marker1);	
		
		// Marker for Amenities
		var latitude=AmenityLatitude.split(",");
		var longitude=AmenityLongitude.split(",");
		var code=AmenityCode.split(",");
		var imageName = AmenityImageName.split(",");
		var amName = AmenityName.split(",");
		var amType = AmenityType.split(",");
		var tempLongitude=0;

		while (tempLongitude < longitude.length -1)
		  {

			marker1 = createAmenityMarker(new GLatLng(latitude[tempLongitude], longitude[tempLongitude]), imageName[tempLongitude],amName[tempLongitude],amType[tempLongitude]);
			map.addOverlay(marker1);
		      	tempLongitude+=1;           
        	  }
	
	}

	else {
	  alert("Sorry, the Google Maps API is not compatible with this browser");
	}
	}

}

	
function addLoadEvent(func) 
{
  var oldonload = window.onload;
  if (typeof window.onload != 'function') 
  {
	window.onload = func;
  } else 
  {
	window.onload = function() 
	{
	  func();
	}
  }
}


function createFacilityMarker(point, name, phone, addr) {

   var bupa_pin = new GIcon(G_DEFAULT_ICON);
   bupa_pin.image = "/bupastatic/images/gmap/bupa_pin.png";
   bupa_pin.iconSize = new GSize(70, 45);
   bupa_pin.imageMap=[0,0,70,0,70,45,0,45];
   markerOptions = { icon:bupa_pin };
   
   var marker = new GMarker(point, markerOptions);
   GEvent.addListener(marker, "click", function() {
   marker.openInfoWindowHtml("<b>" +'Name : ' + "</b>" + name  + '<br />' + "<b>" + 'Address : ' + "</b>" + addr  + '<br />'+ "<b>" + 'Telephone : ' + "</b>" + phone );	
    });
    
    return marker;
  } 


function createAmenityMarker(point, imageName,amName,amType) {
   
    var blueIcon = new GIcon(G_DEFAULT_ICON);
    var imgTrimmed = trim(imageName);
		if(imgTrimmed=="busstopsMap.png")
		{
		 blueIcon.image = "/bupastatic/images/gmap/busstopsMap.png";
		}else if(imgTrimmed=="carparksMap.png")
		{
		 blueIcon.image = "/bupastatic/images/gmap/carparksMap.png";
		}else if(imgTrimmed=="hospitalMap.png")
		{
		 blueIcon.image = "/bupastatic/images/gmap/hospitalMap.png";
		}else if(imgTrimmed=="parksMap.png")
		{
		blueIcon.image = "/bupastatic/images/gmap/parksMap.png";
		}
		else if(imgTrimmed=="postMap.png")
		{
			 blueIcon.image = "/bupastatic/images/gmap/postMap.png";
		}
		else if(imgTrimmed=="shoppingMap.png")
		{
			 blueIcon.image = "/bupastatic/images/gmap/shoppingMap.png";
		}
		else if(imgTrimmed=="trainStationsMap.png")
		{
		 blueIcon.image = "/bupastatic/images/gmap/trainStationsMap.png";
		}
		else if(imgTrimmed=="interestMap.png")
		{
		 blueIcon.image = "/bupastatic/images/gmap/interestMap.png";
		}

	   blueIcon.iconSize = new GSize(25, 31);
       markerOptions = { icon:blueIcon };
       var marker = new GMarker(point, markerOptions);

	   GEvent.addListener(marker, "click", function() {
			marker.openInfoWindowHtml('<b>' +'Name : </b>' + amName + '<br/>' +  '<b> Type of Amenity : </b>'+ amType);				
	    });
  
   //var marker = new GMarker(point);
   return marker;
   
  } 
  
  function addUnLoadEvent(func) {
      var oldonunload = window.onunload;
      if (typeof window.onunload != 'function') {
        window.onunload = func;
      } else {
        window.onunload = function() {
          oldonunload();
          func();
        }
      }
  
  }
 

function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
	window.onload = func;
	} else {
		window.onload = function() {
		oldonload();
		func();
		}
	}
}
  
  addUnLoadEvent(loadGmap); 
  addLoadEvent(loadGmap);
