// execute function when page loads
window.onload=function(){
     if(document.getElementsByTagName){
          loadmaps();
     }
}

//Core Mapping Loader
function loadmaps(){
    // get all <div> elements in the document
    divs=document.getElementsByTagName('div');
    // iterate over all <div> elements in the document
    for(var i=0;i<divs.length;i++){
    // make collection with <div> elements with class attribute 'googlemap'
    var clsname=divs[i].className;
        if (clsname != '') {
        var pos=clsname.indexOf('googlemap');
        //Check to see if the googlemap class is in the class name
            if (pos>=0) {
                if (GBrowserIsCompatible()) {
                    //Collect the current DIV
                    var div = divs[i];
                    //Check to see if this is driving directions
                    if (div.getAttribute("toaddress") != null && div.getAttribute("fromaddress") != null) { 
                        if (document.getElementById(div.getAttribute("directions")) == null) {
                            div.innerHTML = "<span style='color : red;'>Error: </span> Directions container is not specified!"
                         } else {
                            loaddrivingdirections(div, document.getElementById(div.getAttribute("directions")));
                        }
                    } else { 
                        //Load Address or Longitude and Latitude
                        loadaddress(div);
                    }
                }
            }
        }
    }
}

function loaddrivingdirections(div,route) {
  var map = new GMap2(div);
  addmapcontrols(div,map);
  var directionsPanel = route;
  var directions = new GDirections(map, directionsPanel);
  directions.load("from: " + div.getAttribute("fromaddress") + " to: " + div.getAttribute("toaddress"));
}

function loadaddress(div) {
    if (div.getAttribute("address") == null) {
        var innerHTML = div.innerHTML;
        var map = new GMap2(div);
        addmapcontrols(div,map);
        map.setCenter(new GLatLng(div.getAttribute("lat"), div.getAttribute("lon")), getzooomlevel(div));
        var point = new GLatLng(div.getAttribute("lat"), div.getAttribute("lon"));
        // Create a marker
        marker = new GMarker(point);
        // Add the marker to map
        map.addOverlay(marker);
        // Add address information to marker
        if (innerHTML != "") {
            marker.openInfoWindowHtml(innerHTML);
        }
	map.checkResize();
    } else {
        //Add the address with geocoding, passing in the target DIV
        geocodeaddress(div);
    }
}

function geocodeaddress(div) {
    // Create new geocoding object
    geocoder = new GClientGeocoder();

    // Retrieve location information, pass it to addToMap()
    geocoder.getLocations(div.getAttribute("address"), addToMap);
}

// This function adds the point to the map
function addToMap(response) {
    var div = findmapdiv(response.name);
    try
    {
        // Retrieve the object
        place = response.Placemark[0];
        //Capture the current HTML of the DIV
        var innerHTML = div.innerHTML;
        // Create new map object
        map = new GMap2(div);
        //Add map controls if applicable
        addmapcontrols(div,map);
        // Retrieve the latitude and longitude
        point = new GLatLng(place.Point.coordinates[1], place.Point.coordinates[0]);
        // Center the map on this point
        map.setCenter(point, getzooomlevel(div));
        // Create a marker
        marker = new GMarker(point);
        // Add the marker to map
        map.addOverlay(marker);
        //If we have HTML fill it in the window.
        if (innerHTML != "") {
            marker.openInfoWindowHtml(innerHTML);
        } 
    }
    catch(err)
    {
        //Handle errors here
        div.innerHTML = "<span style='color : red;'>Error: </span><b>" + response.name + "</b> was not found, please adjust address."
    }  
map.checkResize();

}

//Helper Functions
function findmapdiv(address) {
// get all <div> elements in the document
divs=document.getElementsByTagName('div');
// iterate over all <div> elements in the document
    for(var i=0;i<divs.length;i++){
    // make collection with <div> elements with class attribute 'googlemap'
        var clsname=divs[i].className;
        if (clsname != '') {
        var pos=clsname.indexOf('googlemap');
        //Check to see if the googlemap class is in the class name
            if (pos>=0) {
                if (GBrowserIsCompatible()) {
                    if (divs[i].getAttribute("address") == address) {
                        return divs[i];
                    }
                }
            }
        }
    }
}

function addmapcontrols(div, map) {

    if (div.getAttribute("scale") == "yes") {
        map.addControl(new GScaleControl());
    }
    if (div.getAttribute("type") == "yes") {
        map.addControl(new GMapTypeControl());
    }
    if (div.getAttribute("pan") == "yes") {
        map.addControl(new GSmallMapControl());
    }
}

function getzooomlevel(div) {
    var zoom = parseInt(div.getAttribute("zoom"));
        if(isNaN(zoom)) { zoom = 13; } 
    return zoom;
}