var request;
var map;

function getMapInfo() {
  var url = "/map.do";
  if (window.XMLHttpRequest) {
    request = new XMLHttpRequest();
    request.onreadystatechange = createMap;
    try {
      request.open("GET", url, true);
    } catch (e) {
      alert("Unable to retrieve map info");
    }
    request.send(null);
  } else if (window.ActiveXObject) {
    request = new ActiveXObject("Microsoft.XMLHTTP");
    if (request) {
      request.onreadystatechange = createMap;
      request.open("GET", url, true);
      request.send();
    }
  }
}

function createMarker(point, tabs) {
  var marker = new GMarker(point);
  GEvent.addListener(marker, "click", function() {
    marker.openInfoWindowTabsHtml(tabs);
  });
  return marker;
}    

function createMap() {
  if (request.readyState == 4) {
    if (request.status == 200) {
      map = new GMap2(document.getElementById("map"));
      map.addControl(new GLargeMapControl());
      map.setCenter(new GLatLng(41, -108), 3);
      var mapInfo = request.responseText;
      var chgProperties = mapInfo.split("%%"); 
      for (i = 0; i < chgProperties.length; i = i + 1) {
        var propInfo = chgProperties[i].split("|");
        var point = new GLatLng(propInfo[0], propInfo[1]);
        var tabs;
        if (propInfo.length >= 10) {
          tabs = new Array(3);
        } else {
          tabs = new Array(2);
        }
        var tabTitle = "Property";
        var tabContent = "<center><a href='" + propInfo[2] + "/Home.do'><img src='" + propInfo[4] + "' border='0' width='144' height='122'></a></center>";
        tabs[0] = new GInfoWindowTab(tabTitle, tabContent);
        tabTitle = "Description";
        //alert(propInfo[5]);
        tabContent = "<div style='font-size:11px;color:#444444;font-family:tahoma;width:250px;align:center;'><b>"+propInfo[6]+", " + propInfo[7]+ "</b><br>" + propInfo[5] + "<br><center><a href='" + propInfo[2] + "/Home.do'>Visit Website</a></center></div>";
        tabs[1] = new GInfoWindowTab(tabTitle, tabContent);
        var atIndex = 8;
        var numColumns = 3;
        if (propInfo.length >= atIndex + 2) {
          tabTitle = "Amenities";
          tabContent = "<center><div style='font-size:11px;color:#444444;font-family:tahoma;width:250px;'><table style='font-size:9px;color:#444444;font-family:tahoma;' width='198' cellpadding='3' cellspacing='3' border='0'>";
          var columnIndex = 1;
          while (propInfo.length >= atIndex + 2) {
            if (columnIndex == 1)
              tabContent += "<tr>";
            tabContent += "<td width='66'><center><img src='"+propInfo[atIndex+1]+"' title='"+propInfo[atIndex]+"' border='0'><br>" + propInfo[atIndex] + "</center></td>";
            if (columnIndex == 3) {
              tabContent += "</tr>";
              columnIndex = 1;
            } else {
              columnIndex += 1;
            }
            atIndex += 2;
          }
          if (columnIndex != 1) {
            while (columnIndex < 4) {
              tabContent += "<td width='66'>&nbsp;</td>";
              columnIndex += 1;
            }
          }
          tabContent += "</table></div></center>";
          //alert(tabContent);
          tabs[2] = new GInfoWindowTab(tabTitle, tabContent);
        }
        map.addOverlay(createMarker(point, tabs));
      }
    }
  }
}
    
