var map;
var bounds;
var counter = 0;
var properties;
var currentPropertyIndex = 0;

var baseIcon = new GIcon(G_DEFAULT_ICON);
baseIcon.shadow = "http://www.google.com/mapfiles/shadow50.png";
baseIcon.iconSize = new GSize(20, 34);
baseIcon.shadowSize = new GSize(37, 34);
baseIcon.iconAnchor = new GPoint(9, 34);
baseIcon.infoWindowAnchor = new GPoint(9, 2);

function mapLoad() {
	if (GBrowserIsCompatible()) {
	  map = new GMap2(document.getElementById("property_map"));
		map.addControl(new GLargeMapControl());
	}
}
$(document).ready(function() {
	mapLoad();
	bounds = new GLatLngBounds();
	map.addControl(new GMapTypeControl());

	$.getJSON(window.location.pathname + window.location.search, function(data) {
		properties = data;
		addProperty(properties[currentPropertyIndex]);
	});
});

function addProperty(property) {
	var coder = new GClientGeocoder();
	coder.getLatLng(buildPropertyAddress(property, false), function(point) {
		if(point) {			
			bounds.extend(point);
			map.setCenter(bounds.getCenter(), map.getBoundsZoomLevel(bounds));
			
			var letter = String.fromCharCode("A".charCodeAt(0) + counter);
			var letteredIcon = new GIcon(baseIcon);
			var baseDisplay = "http://www.google.com/mapfiles/";
			var imagePath = baseDisplay + "marker" + letter + ".png";
			var printPath = "http://www.eralandmark.com/mapfiles/" + "marker" + letter + ".gif";
			
			letteredIcon.image = imagePath;
			letteredIcon.printImage = letteredIcon.mozPrintImage = printPath;			
			
			$('#listing_teaser_' + property.mls).append('<div class="map_icon"><img src="' + imagePath + '" alt="" /></div>');
			// Set up our GMarkerOptions object
			markerOptions = { icon:letteredIcon };
			
			counter++;
			
			var marker = new GMarker(point, markerOptions);
			map.addOverlay(marker);
			marker.bindInfoWindowHtml(buildPropertyWindow(property));
			if(property.mls == $.query.get('mls')) {
				GEvent.trigger(marker, 'click');
			}
			map.setCenter(bounds.getCenter(), map.getBoundsZoomLevel(bounds)-1);
		} else { //can't map property. whatcha gonna do?
			$('#listing_teaser_' + property.mls).appendTo('#property_map_fail');
			$('#property_map_fail').show();
		}
		currentPropertyIndex++;
		if(currentPropertyIndex < properties.length) {
			addProperty(properties[currentPropertyIndex]);
		}
	});
}

function buildPropertyWindow(property) {
	var title = '<h3>' + buildPropertyAddress(property, false) + '</h3>';
	var image = property.photos != null ? '<img src="/files/imagecache/property_thumb/listings/' + property.photos[0].path + '" alt="Property Image" class="map_bubble_image" />' : '';
	
	var description = '';
	if(property.bedrooms) {
		description += '<strong>Beds: </strong>' + property.bedrooms + '<br />';
	}
	if(property.baths) {
		description += '<strong>Baths: </strong>' + property.baths + '<br />';
	}
	description += '<strong> Asking Price: </strong>' + property.asking_price + '<br />';
	description += '<a href="/listings/view/' + property.mls + '">View Property</a>'
	
	return '<div class="window_content">' + title + image + description + '</div>';
}

function buildPropertyAddress(property, includeZip) {
  var address = '';
  address = property.address_number;
  if(property.address_direction) {
    address += ' ' + property.address_direction;
  }
  
  address += ' ' + property.address_street + ', ' + property.city + ', ' + property.state;
  
  if(includeZip) {
    address += ' ' + property.zip;
  }
  
  return address;
}
