function Property(id, url, latitude, longitude, city, state, zipcode, phone_number, bedrooms, bathrooms, price, photo, headline) {
  this.id = id;
  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.bathrooms = bathrooms;
  this.price = price;
  this.photo = photo;
  this.headline = headline;
  
  this.setMarker = function(marker) {
    this.marker = marker;
  };
};

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

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

  pin = new GIcon(G_DEFAULT_ICON);
  yelpPin = new GIcon(G_DEFAULT_ICON);
  yelpPin.image = "/images/marker_star.png";
  yelpPin.shadow = "/images/marker_shadow.png";
  yelpPin.iconSize = new GSize(20, 29);
  yelpPin.shadowSize = new GSize(38, 29);
  yelpPin.iconAnchor = new GPoint(15, 29);
  yelpPin.infoWindowAnchor = new GPoint(15, 3);

  var firstProperty = true;
  var maxLat = 0;
  var maxLong = 0;
  var minLat = 0;
  var minLong = 0;
  $(properties).each(function() {
    var type = this.bathrooms == "yelp" ? "yelp" : "search";
    var icon = type == "yelp" ? yelpPin : pin;
    if (this.latitude && this.latitude < minLat || firstProperty)
      minLat = parseFloat(this.latitude);
    if (this.latitude && this.latitude > maxLat || firstProperty)
      maxLat = parseFloat(this.latitude);
    if (this.longitude && this.longitude < minLong || firstProperty)
      minLong = parseFloat(this.longitude);
    if (this.longitude && this.longitude > maxLong || firstProperty)
      maxLong = parseFloat(this.longitude);

    var coordinate = new GLatLng(this.latitude, this.longitude);
    var marker = new GMarker(coordinate, icon);
    var prop = this;
    this.setMarker(marker);

    GEvent.addListener(marker, "click", function() {
      if (currentMarker != null) {
          currentMarker.closeInfoWindow();
      };
      if(type == "yelp"){
        info_html = "<div class=\"yelp_map_bubble\"> \
          <div class=\"pic\"><a href=\""+prop.url+"\" target=\"_blank\"><img src=\""+prop.photo+"\" alt=\""+prop.headline+"\" /></a></div> \
          <span class=\"listing_name\">"+prop.headline+"</span> \
          <span class=\"stars\"><img src=\""+prop.phone_number+"\" /></span> \
          <span class=\"reviews\">"+prop.bedrooms+" reviews</span> \
          <span class=\"readmore\"><a href=\""+prop.url+"\" target=\"_blank\">&raquo; read more on Yelp</span></span> \
        </div>"
      } else {
        info_html = "<div class=\"search_map_bubble\"> \
          <div class=\"pic\"><a href=\"" + prop.url + "\"><img src=\"" + prop.photo + "\" alt=\"" + prop.headline + "\" width=\"70\" height=\"55\"/></a></div>\
          <span class=\"address\">" + prop.city + "," + prop.state + " " + prop.zipcode + "</span>\
          <span class=\"listingid\">#" + prop.id + "</span>\n";
        if (prop.bedrooms.length > 0){
          info_html += "<span class=\"bdbth\">" + prop.bedrooms + " bd " + prop.bathrooms + "bth</span>\n";
        };
        info_html += "<span class=\"price\">$" + prop.price + "</span>\n  <span class=\"phone\">" + prop.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 getDynamicMap() {
  $.getScript("http://maps.google.com/maps?file=api&v=2&sensor=false&async=2&callback=loadMap&client=gme-csource&channel=RH");
};

