

function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}

addLoadEvent(checkgoogleMaps);



function checkgoogleMaps() {
                     
 // To get all a elements in the document with a “info-links” class.
  var gmapsDivs =  getElementsByClassName(document, "div", "gmaps");
  

    
    if(gmapsDivs.length>0){    
       
        var i = new Image();
        i.onload = doConnectFunction;
        i.onerror = doNoConnectFunction;
        i.src = 'http://www.aapress.com.au/img/71f.gif?d=' + escape(Date());
        
      }
    
   
    }


    
function doConnectFunction() {  
   
}

function doNoConnectFunction() {  
 

   
    var gmapsDivs =  getElementsByClassName(document, "div", "gmaps");
    
     for (var i = 0; i < gmapsDivs.length; i++) {       

          var gDiv=gmapsDivs[i];
          
           gDiv.style.display = "none";
           var newOfflineDiv=document.createElement("div")           
           // newOfflineDiv.setAttribute("id", "divUnavailable")           
            newOfflineDiv.innerHTML="<b>Internet connection Not available</b>"
            newOfflineDiv.style.width = "300px";
            newOfflineDiv.style.height =  "300px";

          
            parentnode=gDiv.parentNode.insertBefore(newOfflineDiv, gDiv)
            
                 
            
        }
    }


function getElementsByClassName(oElm, strTagName, oClassNames){
	var arrElements = (strTagName == "*" && oElm.all)? oElm.all : oElm.getElementsByTagName(strTagName);
	var arrReturnElements = new Array();
	var arrRegExpClassNames = new Array();
	if(typeof oClassNames == "object"){
		for(var i=0; i<oClassNames.length; i++){
			arrRegExpClassNames.push(new RegExp("(^|\\s)" + oClassNames[i].replace(/\-/g, "\\-") + "(\\s|$)"));
		}
	}
	else{
		arrRegExpClassNames.push(new RegExp("(^|\\s)" + oClassNames.replace(/\-/g, "\\-") + "(\\s|$)"));
	}
	var oElement;
	var bMatchesAll;
	for(var j=0; j<arrElements.length; j++){
		oElement = arrElements[j];
		bMatchesAll = true;
		for(var k=0; k<arrRegExpClassNames.length; k++){
			if(!arrRegExpClassNames[k].test(oElement.className)){
				bMatchesAll = false;
				break;
			}
		}
		if(bMatchesAll){
			arrReturnElements.push(oElement);
		}
	}
	return (arrReturnElements)
}


/*    for Canvas element       */
var map;
var place;
var geocoder;
var addressToFind;
var myLatitude, myLongitude;
var mgr;
function initialize() {
    if (GBrowserIsCompatible()) {
        map = new GMap2(document.getElementById("map_canvas"));
        map.setCenter(new GLatLng(-23.70036, 133.8809), 7);
        map.addControl(new GSmallMapControl());

        var customUI = map.getDefaultUI();
        customUI.controls.menumaptypecontrol = true;
        customUI.controls.smallzoomcontrol3d = false;
        map.setUI(customUI);

        GEvent.addListener(map, "click", function (overlay, latlng) {
            if (latlng) {
                DrawMarkerOnMap(latlng);
            }
        });
        geocoder = new GClientGeocoder();
    }
}

function DrawMarkerOnMap(latlng) {
    SaveAddressAndMarkOnMap(latlng, 1);
}

function SaveAddressAndMarkOnMap(point, option) {

    var currentMapType = null;
    var currentzoom = null;
    document.title = "";
    llpoint = new String(point);
    var data = llpoint.replace("(", "").replace(")", "")
    var arr = data.split(",");

    myLatitude = parseFloat(new String(arr[0]));
    myLongitude = parseFloat(new String(arr[1]));

    if (myLatitude != null && myLongitude != null) {
        map.setCenter(new GLatLng(myLatitude, myLongitude));
    }




    if (arr[2] != null) {
        currentzoom = parseInt(new String(arr[2]));
    }
    else {
        currentzoom = parseInt(new String("7"));
    }

    if (arr[3] != null) {
        currentMapType = arr[3];

    }
    else {
        currentMapType = GetMapType('m');

    }

    if (currentzoom != null) {
        map.setZoom(eval(currentzoom));


    }
    if (currentMapType != null) {
        map.setMapType(eval(currentMapType));
    }




    point = new GLatLng(myLatitude, myLongitude);
    marker = new GMarker(point);
    if (option == 1) {
        map.openInfoWindow(point, "Latitude: " + myLatitude + '<br>Longitude: ' + myLongitude);
        document.title = myLongitude + "," + myLatitude + "," + map.getZoom();
        document.getElementById("hdLongitude").value = myLongitude;
        document.getElementById("hdLatitude").value = myLatitude;
    } else {
        document.title = "";
        map.addOverlay(marker);
        marker.openInfoWindowHtml(place.address + '<br>' + '<b>Country code:<\/b> ' + place.AddressDetails.Country.CountryNameCode);
    }
}

function GetMapType(mType) {


    if (mType == 'k')
        return "G_SATELLITE_MAP";
    if (mType == 'h')
        return "G_HYBRID_MAP";
    if (mType == 'p')
        return "G_PHYSICAL_MAP";

    return "G_NORMAL_MAP";

}

function addAddressToMap(response) {
    map.clearOverlays();
    if (!response || response.Status.code != 200) {
        alert("Sorry, we were unable to geocode that address");
    } else {
        place = response.Placemark[0];
        myLatitude = place.Point.coordinates[1];
        myLongitude = place.Point.coordinates[0];
        point = new GLatLng(myLatitude, myLongitude);
        SaveAddressAndMarkOnMap(point, 0);
    }
}
function showLocation() {
    geocoder.getLocations(addressToFind, addAddressToMap);
}
function findLocation(address) {
    document.getElementById("hdLongitude").value = "";
    document.getElementById("hdLatitude").value = "";
    addressToFind = address;
    showLocation();
}

function getMapParams() {
    var center = map.getCenter();
    return center.lat() + ";" + center.lng() + ";" + map.getZoom() + ";" + map.getCurrentMapType().getName();
}

/*    for Canvas element     end   */