var _maxLat = null, _minLat = null, _maxLon = null, _minLon = null, ajaxMapLoaded = null;
function mapLoadCallback() {
	$("body").trigger("mapLoadComplete");
	ajaxMapLoaded = true;
	removeVEBubble()
}
function ajaxLoadMap(a) {
	ajaxMapLoaded === null && $.get(a, function(b) {
		b = new Envelope(b);
		$("#resultsMapContainer").html(b.payload);
		ajaxMapLoaded = false;
		loadMapIfReady(0)
	}, "text")
}
function loadMapIfReady(a) {
	if(window.loadMap && window.VEMap) {
		$("#loadingMap").hide();
		$("#resultsMapContainer").is(":visible") && setTimeout(function() {
			loadMap()
		}, 250)
	}else if(a < 100) {
		a++;
		setTimeout(function() {
			loadMapIfReady(a)
		}, 250)
	}
}
function setCollectionPushpin(a, b) {
	if(map) {
		clearMap();
		var c = new VEShapeLayer;
		for(i = 0;i < b.length;i++)if(b[i].lat != 0 && b[i].lon != 0) {
			var d = createPushpin(b[i]);
			c.AddShape(d)
		}map.AddShapeLayer(c);
		b = $("#" + a).width();
		a = $("#" + a).height();
		a = calculateMapBestView(points, b, a);
		map.SetCenterAndZoom(a.centerPoint, a.zoomLevel)
	}
}
function createPushpin(a) {
	var b = [];
	b.push('<div id="mapBubble-' + a.propertyid + '" class="mapBubble">');
	b.push('<div class="bubbleContent">');
	b.push('<div class="t"></div>');
	b.push('<div class="buttons"><a href="javascript:closeBubble();" class="xbutton"></a></div>');
	b.push('<div><div class="photo">');
	b.push('<a href="/admin/listingdetails/mlsid/' + a.mlslistid + "/propertyid/" + a.propertyid + '/">');
	b.push('<img src="' + a.imageurl + '" height="56" width="77"></a>');
	b.push('</div><div class="info">');
	b.push('<ul><li class="price">');
	b.push('<a href="/admin/listingdetails/mlsid/' + a.mlslistid + "/propertyid/" + a.propertyid + '/">');
	b.push("<h3>" + a.price + "</h3></a></li>");
	b.push('<li class="address"><span>' + a.address + "</span></li>");
	b.push('<li class="csz"><span>' + a.csz + "</span></li>");
	b.push('<li class="beds"><span>' + a.beds + "</span><label> beds</label></li>");
	b.push('<li class="baths"><span>' + a.baths + "</span><label> baths</label></li>");
	b.push('<li class="remarks"><span>' + a.remarks + "</span></li>");
	b.push("</ul></div>");
	b.push('<div class="clearLeft"></div>');
	b.push("</div></div>");
	b.push('<div class="b"><div></div></div>');
	b.push("</div>");
	b = b.join("");
	a = new VELatLong(a.lat, a.lon);
	var c = new VEShape(VEShapeType.Pushpin, a);
	c.SetTitle("");
	c.SetDescription(b);
	c.SetCustomIcon('<div class="mapPoint"><span class="digit1 pushpin"></span></div>');
	points[counter] = a;
	markers[counter] = c;
	counter++;
	return c
}
function toObj(a) {
	if(typeof a == "object")return a;
	else {
		a = $.trim(a + "");
		if(a.charAt(0) != "{" && a.slice(-1) != "}")a = "{" + a + "}";
		return window.JSON && window.JSON.parse ? window.JSON.parse(a) : (new Function("return " + a))()
	}
}
function setPushpin(a) {
	rowData = toObj(a);
	if(!(!rowData || !rowData.lat || !rowData.lon)) {
		a = createPushpin(rowData);
		map.AddShape(a);
		adjustCenterCoords(rowData.lat, rowData.lon)
	}
}
function deletePushpin(a) {
	rowData = toObj(a);
	if(!(!rowData || !rowData.lat || !rowData.lon))for(i = 0;i < points.length;i++)if(points[i] != null) {
		a = points[i];
		if(a.Latitude == rowData.lat && a.Longitude == rowData.lon) {
			map.DeleteShape(markers[i]);
			points[i] = null;
			return
		}
	}
}
function adjustCenterCoords(a, b) {
	b = parseFloat(b);
	a = parseFloat(a);
	if(b && a) {
		_maxLon = _maxLon != null ? Math.max(b, _maxLon) : b;
		_minLon = _minLon != null ? Math.min(b, _minLon) : b;
		_maxLat = _maxLat != null ? Math.max(a, _maxLat) : a;
		_minLat = _minLat != null ? Math.min(a, _minLat) : a;
		a = (_minLon + _maxLon) / 2;
		b = (_minLat + _maxLat) / 2;
		map.SetCenterAndZoom(new VELatLong(b, a), 9)
	}
}
function calculateMapBestView(a, b, c) {
	var d = -90, e = 90, f = -180, g = 180, j = [78.27152, 39.13576, 19.56788, 9.78394, 4.89197, 2.44598, 1.22299, 0.6115, 0.30575, 0.15287, 0.07644, 0.03822, 0.01911, 0.00955, 0.00478, 0.00239, 0.00119, 6.0E-4, 3.0E-4];
	for(i = 0;i < a.length;i++) {
		if(a[i].Latitude > d)d = a[i].Latitude;
		if(a[i].Latitude < e)e = a[i].Latitude;
		if(a[i].Longitude > f)f = a[i].Longitude;
		if(a[i].Longitude < g)g = a[i].Longitude
	}a = (d + e) / 2;
	var h = (f + g) / 2;
	f = HaversineDistance(a, g, a, f);
	d = HaversineDistance(d, h, e, h) * 2;
	b = f / (b || 100);
	c = d / (c || 100);
	c = b > c ? b : c;
	for(i = b = 1;i < 19;i++)if(c > j[i]) {
		b = i;
		break
	}b = Math.min(14, b);
	return{centerPoint:new VELatLong(a, h), zoomLevel:b}
}
function HaversineDistance(a, b, c, d) {
	var e = 6371, f = Math.PI / 180, g = (c - a) * f;
	b = (d - b) * f;
	a = Math.sin(g / 2) * Math.sin(g / 2) + Math.cos(a * f) * Math.cos(c * f) * Math.sin(b / 2) * Math.sin(b / 2);
	a = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
	return e * a
}
function redrawOpenBubble() {
}
function redrawWait() {
	if(openBubble && map)if(busyRedraw) {
		busyRedraw = false;
		setTimeout(redrawWait, 250)
	}else setTimeout(function() {
		var a = openBubble.GetID();
		a = a + "_1" + a.slice(-4);
		var b = {elementID:a};
		shape_onclickEvent(b);
		setTimeout(function() {
			if(openBubble) {
				map.ShowInfoBox(openBubble);
				fixBubble(a, 0)
			}
		}, 750)
	}, 250)
}
function selectOnMap(a) {
	if(map) {
		map.HideInfoBox();
		map.SetZoomLevel(12);
		map.SetCenter(points[a]);
		openBubble = markers[a];
		busyRedraw = false;
		setTimeout(redrawWait, 250)
	}
}
function clearMap() {
	_minLon = _maxLon = _minLat = _maxLat = null;
	points = [];
	markers = [];
	counter = 0;
	map && map.Clear()
}
function removeVEBubble() {
	$(".customInfoBox-shadow").removeClass("customInfoBox-shadow").addClass("customInfoBox-shadow-hide");
	$(".customInfoBox-body").removeClass("customInfoBox-body").addClass("customInfoBox-body-hide");
	$("div.MSVE_MapContainer").pngFix()
}
function fixBubble(a, b) {
	var c = $("#" + a), d = $("div.customInfoBox-with-leftBeak, div.customInfoBox-with-rightBeak");
	if(c.length && d.height() < 390 && b < 3) {
		d.find(".firstChild").children("br,p").remove().end().find("div.b").click(closeBubble);
		var e = parseInt(c.offset().top + 0.5);
		c = d.offset();
		e = e - parseInt(d.height() + 0.5);
		Math.abs(c.top - e) > 15 && d.offset({top:e, left:c.left})
	}else {
		b++;
		setTimeout(function() {
			fixBubble(a, b)
		}, b * 50)
	}
}
$(window).load(function() {
	setTimeout(function() {
		if(window.loadMap && $("#map").is(":visible")) {
			map || loadMap();
			removeVEBubble();
			if(points.length > 1) {
				var a = $("#map").width(), b = $("#map").height();
				a = calculateMapBestView(points, a, b);
				map.SetCenterAndZoom(a.centerPoint, a.zoomLevel)
			}
		}
	}, 50)
});
