var markers = new Array();
var map = null;
var pin = '';
var infoTemplate = "";
var currentMarker = null;

function loadMap() {
  map = new GMap2($("#listingMap")[0]); 
  map.addControl(new GSmallMapControl());
  map.addControl(new GMapTypeControl());

  pin = new GIcon(G_DEFAULT_ICON);

  
  var firstProperty = true;
  var maxLat = 0;
  var maxLong = 0;
  var minLat = 0;
  var minLong = 0;
  $(properties).each(function() {
    var coordinate = new GLatLng(this.latitude, this.longitude);
    if (this.latitude < minLat || firstProperty)
      minLat = parseFloat(this.latitude);
    if (this.latitude > maxLat || firstProperty)
      maxLat = parseFloat(this.latitude);
    if (this.longitude < minLong || firstProperty)
      minLong = parseFloat(this.longitude);
    if (this.latitude > maxLat || firstProperty)
      maxLong = parseFloat(this.longitude);
    var marker = new GMarker(coordinate, pin);
    var name = this.name;
    var url = this.url;
		var photo = this.photo;
		var city = this.city;
		var zipcode = this.zipcode;
		var bedrooms = this.bedrooms;
		var price = this.price;
		var phone_number = this.phone_number;
		var state = this.state;
		
    this.setMarker(marker);
     GEvent.addListener(marker, "click", function() {
      if (currentMarker != null) {
          currentMarker.closeInfoWindow();
      }
      info_html = "<div class=\"search_map_bubble\">\n <div class=\"pic\"><a href=\"" + url + "\"><img src=\"" + photo + "\" alt=\"" + name + "\" width=\"70\" height=\"55\"/></a></div>\n  <span class=\"address\">" + name + "</span>\n  <span class=\"listingid\">" + city + "," + state + " " + zipcode + "</span>\n";  
      if (bedrooms.length > 0){
        info_html += "<span class=\"bdbth\">" + bedrooms + " beds</span>\n";
      }
      info_html += "<span class=\"price\">" + price + "</span>\n  <span class=\"phone\">" + phone_number + "</span>\n  </div>";
                    
      marker.openInfoWindowHtml(info_html);
      currentMarker = marker;
    });
    
    map.addOverlay(marker);

    firstProperty = false;

  });
  
  var minLatLng = new GLatLng(minLat, minLong);
  var maxLatLng = new GLatLng(maxLat, maxLong);
  var bounds = new GLatLngBounds(minLatLng,maxLatLng);
  var center = bounds.getCenter();
  map.setCenter(center, map.getBoundsZoomLevel(bounds));
  map.panTo(map.getCenter());
}

function findPropertyById(propertyList, id) {
  for (i = 0; i < propertyList.length; i++) {
    if (propertyList[i].id == id) {
      return propertyList[i];
    }
  }
}

function getDynamicMap() {
  $.getScript("http://maps.google.com/maps?file=api&v=2&sensor=false&async=2&callback=loadMap&client=gme-csource&channel=AG");
}

function Property(name, id, url, latitude, longitude, city, state, zipcode, phone_number, bedrooms, price, photo) {
  this.name = name;
  this.url = url;
  this.latitude = latitude;
  this.longitude = longitude;
  this.city = city;
  this.state = state;
  this.zipcode = zipcode;
	this.phone_number = phone_number;
	this.bedrooms = bedrooms;
	this.price = price;
	this.photo = photo;
  
  this.setMarker = function(marker) {
    this.marker = marker;
  }
}
