var Micromap = function(containerId) {
 var self = this;
 this.init = function() {
  this.longitude = this.longitude || -0.156019;
  this.latitude = this.latitude || 50.825891;
  this.zoom  = this.zoom || 15;
  if (GBrowserIsCompatible()) {
   this.createMap();
   this.geoHandler();
  }
 };
 this.createMap = function() {
  var mapdiv = document.createElement("div");
  mapdiv.setAttribute("id",containerId+"-map");
  var container = document.getElementById(containerId);
  container.parentNode.insertBefore(mapdiv,container);
  this.map = new GMap2(mapdiv);
  this.map.addControl(new GLargeMapControl());
  var point = new GLatLng(this.latitude,this.longitude);
  this.map.setCenter(point,this.zoom);
 };
 this.geoHandler = function() {
  var places = this.getElementsByClassName("vcard",document.getElementById(containerId));
  for (var i=0; i<places.length; i++) {
   var elems = this.getElementsByClassName("geo",places[i]);
   for (var j=0; j<elems.length; j++) {
    var degrees = elems[j].getAttribute("title").split(";");
    break;
   }
   elems = null;
   if (degrees) {
    places[i].coordlat = parseFloat(degrees[0]);
    places[i].coordlong = parseFloat(degrees[1]);
    places[i].style.cursor = "pointer";
    places[i].onclick = function() {
     self.chooseElement(this);
    };
   }
  }
  places = null;
 };
 this.chooseElement = function(element) {
  this.map.closeInfoWindow();
  if (this.marker) {
   this.map.removeOverlay(this.marker);
  }
  if (this.currentplace) {
   if (element == this.currentplace) {
    this.map.removeOverlay(this.marker);
    this.currentplace = null;
    return;
   }
  }
  this.currentplace = element;
  var point = new GLatLng(element.coordlat,element.coordlong);
  this.marker = new GMarker(point);
  GEvent.addListener(this.marker, "click", function() {
//   self.marker.openInfoWindow(self.currentplace.cloneNode(true));
   self.marker.openInfoWindowHtml(self.currentplace.innerHTML);
  });
  this.map.addOverlay(this.marker);
  this.map.panTo(point);
 };
 this.getElementsByClassName = function(name,element) {
  var results = [];
  var elems = element.getElementsByTagName("*");
  for (var i=0; i<elems.length; i++) {
   if (elems[i].className.match(new RegExp("(^|\\s)"+name+"(\\s|$)"))) {
    results[results.length] = elems[i];
   }
  }
  elems = null;
  return results;
 };
};
function sydneyMap() {
 var venue = new Micromap("sydney");
 venue.longitude = 151.206;
 venue.latitude = -33.88;
 venue.zoom = 15;
 venue.init();
}
addLoadEvent(sydneyMap);
