var currentDiv = null;
function getDetailsFor(id, div, callback) {
	var date = new Date();
	var page = "details.php";
	var getstr = "?id="+id+"&x="+date.getTime();
	currentDiv = div;
	makeRequest(page, getstr, callback);
}

function handleResponseInfoWindow() {
	if (http_request.readyState == 4) {
		if (http_request.status == 200) {
			result = http_request.responseText;
			currentDiv.innerHTML = result;
		} else {
			result = "There was a problem while getting details.";
		}
		
		servicingLoc.marker.openInfoWindowHtml("<div align=left>"+parseDetails(result)+"</div>");
		servicingID = -1;		
	}
}

function handleResponse() {
	if (http_request.readyState == 4) {
		if (http_request.status == 200) {
			result = http_request.responseText;
		} else {
			result = "There was a problem while getting details.";
		}
		
		currentDiv.innerHTML = parseDetails(result);
		currentDiv = null;
		servicingID = -1;
	}
}

var minheight = 4000;
function checkMapSize() {
	return (map.getSize().height > minheight);	
}

var servicingID = -1;
var servicingLoc = null;
function showDetailsCheckHeight(id) {
	if(servicingID == -1) {
		servicingID = id;
		servicingLoc = getLocationWithID(id);
		if(checkMapSize()) {
			getDetailsFor(id, getDetailsDiv(), handleResponseInfoWindow);
		} else {
			showDetails(id);
		}
	} else {
		alert("Still loading more information for another property.");
	}
}

function showDetails(id) {
	//hideAllDivs();
	var detailsDiv = getDetailsDiv();
	detailsDiv.innerHTML = "<font color=red>Loading details for location, please wait.&nbsp;&nbsp;&nbsp;</font>";
	getDetailsFor(id, detailsDiv, handleResponse);
	showDiv(getMainDetailsDiv());
	loadPopup("detailsMain");
}

function emptyDetailsDiv() {
	var detailsDiv = getDetailsDiv();
	detailsDiv.innerHTML = "";
}

function parseDetails(details) {
	var srch = "::::LINKS::::";
	var i = details.indexOf(srch);
	
	if(i != -1) {
		var loc = getLocationWithID(servicingID);
		if(loc != null) {
			details = details.replace(srch, loc.getLinkHTML(1));
		}
	} else {
		details = details.replace(srch, "");
	}
	
	return details;
}
