var map;
var bounds;
var counter = 0;
var property;
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);

$(document).ready(function() {
	if(mapLoad()) {
	  bounds = new GLatLngBounds();
  	map.addControl(new GMapTypeControl());

  	$.getJSON(window.location.pathname, function(data) {
  		addProperty(data);
  	});
	}
});

function mapLoad() {
	if (GBrowserIsCompatible() && $('#property_map').length > 0) {
	  map = new GMap2($('#property_map')[0],{size: new GSize(Number($('#property_map').css('width').replace('px','')), Number($('#property_map').css('height').replace('px','')))});
		map.addControl(new GLargeMapControl());
		return true;
	} else {
	  return false;
	}
}

function addProperty(property) {
	var coder = new GClientGeocoder();
	coder.getLatLng(buildPropertyAddress(property, true), 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))
			//GEvent.trigger(marker, 'click');
			map.setCenter(bounds.getCenter(), map.getBoundsZoomLevel(bounds)-3);
			$('#map-button').show();			
		} else {
			$('#property_map').hide();
		}
	});
}

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;
}
