/*
 *
 *	Global variables
 *
 */

var hs_image = new Image();
hs_image.src = '/images/gmap/hs_marker.png';

var other_image = new Image();
other_image.src = '/images/gmap/other_marker.png';

var hs_icon = new GIcon();
hs_icon.image = hs_image.src;
hs_icon.iconSize = new GSize(25, 39);
hs_icon.iconAnchor = new GPoint(13, 39);
hs_icon.infoWindowAnchor = new GPoint(5, 1);
		
var other_icon = new GIcon();
other_icon.image = other_image.src;
other_icon.iconSize = new GSize(16, 25);
other_icon.iconAnchor = new GPoint(8, 24);
other_icon.infoWindowAnchor = new GPoint(5, 1);

var map;

var loadedProperties = 0;
var totalProperties = 0;

/* 
 -- The callback that will populate the other properties 
 */
function loadProperties_callback(res) {
	totalProperties = res.value.length;
	
	loadMarkers(res.value);
}

/* 
 -- Loads the markers in chunks so IE doesn't lock up 
 */
function loadMarkers(array) {
	var len = Math.min(10, array.length);
	var loadingStatus = $('loadStatus');
	
	for(var i = 0; i < len; i++) {
		map.addOverlay(getMarkerFromProperty(array.shift()));
	}
	
	loadedProperties = loadedProperties + len;
	
	if(loadingStatus != null)
		loadingStatus.innerHTML = '<p>Loaded ' + loadedProperties + ' of ' + totalProperties + ' properties</p>';
	
	if(array.length > 0) {
		setTimeout(function() { loadMarkers(array) }, 50);
	}
	else {
		if(loadingStatus != null)
			loadingStatus.innerHTML = '';
	}
}

/* 
 -- Sets up a property marker 
 */
function getMarkerFromProperty(prop) {
	return getMarker(	
			prop.MlsNumber.toString(), 
			prop.Latitude,
			prop.Longitude, 
			String.fromCharCode(prop.RealtorType)
		);
}

/* 
 -- Sets up a property marker 
 */
function getMarker(mlsNumber, latitude, longitude, realtorType) {
	var marker = new GMarker(
			new GLatLng(latitude, longitude),
			realtorType == 'H' ? hs_icon : other_icon
		);
	
	GEvent.addListener(marker, "click", 
				function() {
					marker.openInfoWindowHtml(getInfoWindowHtml(mlsNumber));
				}
		);
	return marker;
}

/* 
 -- Gets the info window HTML 
 */
function getInfoWindowHtml(mlsNumber) {
	return HomeSale.AgentCore.Map.MapUtil.PropertyInfoWindow(mlsNumber).value;
}

/* 
 -- Get a property's information 
 */
function getProperty(mlsNumber) {
	return HomeSale.AgentCore.Map.MapUtil.ByMlsNumber(mlsNumber).value;
}
