function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}
function addUnLoadEvent(func) {
  var oldunonload = window.onunload;
  if (typeof window.onunload != 'function') {
    window.onunload = func;
  } else {
    window.onunload = function() {
      oldonunload();
      func();
    }
  }
}
function prepareLink(url,text) {
  if (!document.createElement) return false;
  if (!document.createTextNode) return false;
  var each_element = document.createElement("a");
  each_element.setAttribute("href", url);
  var text = document.createTextNode(text);
  each_element.appendChild(text);
  return each_element;
}
var googlemap_load = function() {
  if (typeof(window['map_markers']) == 'undefined') return false;
  if (!map_markers.length > 0) return false;
  if (typeof(window['map_div_id']) == 'undefined') return false;
  if (!document.getElementById) return false;
  if (!document.getElementById(map_div_id)) return false; 
  if (GBrowserIsCompatible()) {
    var map = new GMap2(document.getElementById(map_div_id));
    map.addControl(new GLargeMapControl());
    map.disableScrollWheelZoom();
    map.addControl(new GMapTypeControl());
    map.setCenter(new GLatLng(29.875698,-5.328200), 2);
    function createMarker(point,html) {
      var custom_icon = new GIcon(G_DEFAULT_ICON);
      custom_icon.image = '/images/RedMarker2.png';
      var marker = new GMarker(point,custom_icon);
      GEvent.addListener(marker, "click", function() {
        marker.openInfoWindowHtml(html);
      });
      return marker;
    }
    // function Rectangle(bounds, opt_weight, opt_color) {
    function Rectangle(opt_weight, opt_color) {
      // this.bounds_ = bounds;
      this.weight_ = opt_weight || 0;
      this.color_ = opt_color || "#888888";
    }
    Rectangle.prototype = new GOverlay();
    
    Rectangle.prototype.initialize = function(map) {
      var div = document.createElement("div");
      div.style.border = this.weight_ + "px solid " + this.color_;
      div.style.position = "absolute";

      // http://developer.mozilla.org/en/docs/DOM:CSS
      p = document.createElement("p");
      p.setAttribute("id", "MapLinkId");
      var successful_links = 0;
      for (var i = 0; i < about_us_links.length; i++) {
        if (about_us_links[i].length == 2) {
          if (mylink = prepareLink(about_us_links[i][0], about_us_links[i][1])) {
            p.appendChild(mylink);
            successful_links++;
          }
        }
      }
      div.appendChild(p);
    
      if (successful_links > 0) map.getPane(G_MAP_MAP_PANE).appendChild(div);
      this.map_ = map;
      this.div_ = div;
    }
    Rectangle.prototype.remove = function() {
      this.div_.parentNode.removeChild(this.div_);
    }
    Rectangle.prototype.copy = function() {
      return new Rectangle(this.weight_, this.color_,
                           this.backgroundColor_, this.opacity_);
    }
    Rectangle.prototype.redraw = function(force) {
      if (!force) return;
      this.div_.style.width = 40 + "em";
      this.div_.style.height = 20 + "em";
      this.div_.style.left = 10 + "px";
      this.div_.style.top = 0 + "px";
    }
    
    if ((typeof(window['about_us_links']) != 'undefined') && (about_us_links.length > 0)) map.addOverlay(new Rectangle());

    var open_html = '<div class="MapInfoWindow">';
    var close_html = '</div>';
    for (var count = 0; count < map_markers.length; count++) {
      if (map_markers[count].length == 4) {
        var point = new GLatLng(map_markers[count][1],map_markers[count][2]);
        var my_title = map_markers[count][0]; 
        var html = open_html + my_title + close_html
        var marker = createMarker(point, html);
        map.addOverlay(marker);
      }
    }

  }
}
var googlemap_unload = function() {
  GUnload();
}
addLoadEvent(googlemap_load);
addUnLoadEvent(googlemap_unload);
