
  
  
var map = null;
var geocoder = null;
var maps = null;
var addresses = null;
function googlemap(idName) {
    if (GBrowserIsCompatible()) {
        if(idName.type == 'load'){
            idName = "map";
        }

        maps = document.getElementById(idName);
        if(maps != null){

            map = new GMap2(maps);
            map.addControl(new GLargeMapControl());
            map.addControl(new GMapTypeControl());
            map.setCenter(new GLatLng(44.98,-93.25),12);
            window.setTimeout(setupMarkers, 2000);
        }
    }
}
function setupMarkers() {
  geocoder = new GClientGeocoder();

  var address = maps.className;
  if(address == ' || '){
    address = "Minneapolis, MN";
    showAddress(address);
  } else {
    addresses = address.split("||");
    addressLoop(0);
  }
}

function addressLoop(num) {
  //Iterative loop idea by Morgan to try and get this to work better
  if(addresses != null) {
    address = addresses[num].replace(/^\s+|\s+$/g, '') ;
    showAddress(address);
    if((num + 1) < addresses.length) {
      var t=setTimeout("addressLoop("+num +"+ 1)",300);
    }
  }
}


function showAddress(address) {
  if (geocoder) {
      geocoder.getLatLng(
              address,
              function(point) {
                  if(point){
                      var contentTxt = "";
                      var infoBalloon = document.getElementById(address);
                      if(infoBalloon != null) {
                          //Title
                          var title = infoBalloon.getElementsByTagName('h3');
                          if(title != null) {
                              contentTxt = contentTxt + "<h3 class=\"googleText\">" + title[0].firstChild.nodeValue + "</h3>";
                          }

                        //Content
                          var txt = infoBalloon.getElementsByTagName('p');
                          for(i=0; i < txt.length; i++) {
                              contentTxt = contentTxt + "<p class=\"googleText\">" + txt[i].firstChild.nodeValue + "</p>";
                          }

                        //Image
                          var img = infoBalloon.getElementsByTagName('img');
                          if(img.length > 0) {
                              var imgTitle = "";
                              if(img[0].getAttribute('title') != null)
                                  imgTitle = img[0].getAttribute('title');
                              contentTxt = contentTxt+"<img src='"+img[0].getAttribute('src')+"' title='"+imgTitle+"' />";
                          }

                          var marker = createMarker(point,contentTxt);
                          map.addOverlay(marker);
                      }
                  }
              }
              );
  }
}

function createMarker(point,html) {
    var marker = new GMarker(point);
    GEvent.addListener(marker, "click", function() {
        marker.openInfoWindowHtml(html);
    });
    return marker;
}

addEvent(window, 'load', googlemap, false);
