// Mapping Software (functions)
//
// © 2007 Gary B. Little
//
// May 27, 2007
//


// Create the data element for an item on the map:
//
function makeMapItem( latitude, longitude, actionMoney, streetAddress, town, actionDate, description ) {

	var rec = {};
	var theIcon;
	var xOffset, yOffset;
	var theTitle;
	var theLabelText;

	gMarkerCount++;
	
	rec.inFoundSet = true;

	theTitle = getTitle(latitude, longitude, actionMoney, streetAddress, town, actionDate, description);

	theLabelText = getLabelText(latitude, longitude, actionMoney, streetAddress, town, actionDate, description);
				
	theIcon = new GIcon(G_DEFAULT_ICON, kMarkerOffURL);
	theIcon.printImage = kMarkerPrintURL;
	theIcon.mozPrintImage = kMarkerMozPrintURL;
	
	theIcon.shadow = "";
	theIcon.printShadow = "";

	if ( gMarkerCount < 10 ) {

		xOffset = -2;
		yOffset = -31;
		
	} else {
	
		xOffset = -6;
		yOffset = -31;	
	}

	rec.theMarker = new LabeledMarker(new GLatLng(latitude, longitude), {icon: theIcon, title: theTitle, labelText: theLabelText, labelOffset: new GSize(xOffset, yOffset), labelClass: "markerLabel"});
	rec.theMarker.index = gMarkerCount;

	rec.theActionMoney = actionMoney;
	rec.theStreetAddress = streetAddress;
	rec.theTown = town;
	rec.theActionDate = actionDate;
	rec.theDescription = description;
	
	return rec;
}


// Add markers (and listeners) to a map.
//
function addMarkers() {

	var i;
	var marker;
	
	gMap.clearOverlays();
	
	applyFilters();

	for ( i = 1; i < mapDB.length; i++ ) {
	
		if ( mapDB[i].inFoundSet ) {
	
			marker = mapDB[i].theMarker;

			GEvent.addListener( marker, "mouseover", function() {				
				
				this.setImage(kMarkerOnURL);
				
				document.getElementById("list" + this.index).style.backgroundColor=kListOnColor;
				
				if ( gScrollList ) {
				
					document.getElementById(kHotID).scrollTop = document.getElementById("list" + this.index).offsetTop;
				}
			} );
		
			GEvent.addListener( marker, "mouseout", function() {				
				
				this.setImage(kMarkerOffURL);
				
				document.getElementById("list" + this.index).style.backgroundColor=kListOffColor;
			} );
		
			GEvent.addListener( marker, "click", function() {				
				
				gMap.setCenter(mapDB[this.index].theMarker.getPoint(), gMap.getZoom(), gMap.getCurrentMapType());				
			} );
		
			gMap.addOverlay(marker);
		}
	}
	showFoundSetList();	
}


// Add listener for maptypechanged event.
//
function addMapTypeChangedListener() {

	GEvent.addListener( gMap, "maptypechanged", function() {

		if ( this.getCurrentMapType() == G_NORMAL_MAP || this.getCurrentMapType() == G_PHYSICAL_MAP ) {
			
			document.getElementById(kCopyrightID).style.color = "black";
		
		} else {

			document.getElementById(kCopyrightID).style.color = "white";
		}
	} );
}

// Show the "last updated" date.
//
function addDateDisplay( theMap, divID ) {

	if ( document.getElementById(divID) != null ) {
	
		document.getElementById(divID).innerHTML = kLastUpdated;
	}
}


// Add a listener for updating the latitude and longitude display
// when the centre point of the map changes during a pan operation.
//
function addLatLongDisplay( theMap, divID ) {

	if ( document.getElementById(divID) != null ) {
 
		GEvent.addListener( theMap, "move", function() {
			var theCoords;
			var degrees = "&deg;";
			var center = theMap.getCenter();
			var latitude = center.lat();
			var longitude = center.lng();
			
			if ( latitude >= 0 ) {
			
				latitude = Math.round(latitude*10000) / 10000 + degrees + "N";
			
			} else {
			
				latitude = -1*(Math.round(latitude*10000) / 10000) + degrees + "S";
			}
			
			if ( longitude >= 0 ) {
			
				longitude = Math.round(longitude*10000) / 10000  + degrees + "E";
			
			} else {
			
				longitude = -1*(Math.round(longitude*10000) / 10000) + degrees + "W";
			}
			
			theCoords = "Centre of map: ";
			theCoords = theCoords + latitude;
			theCoords = theCoords + " / ";
			theCoords = theCoords + longitude;
	
			document.getElementById(divID).innerHTML = theCoords;
		} );
	}
}


// Show the property in the map
//
function showPropertyInMap( i ) {

	GEvent.trigger(mapDB[i].theMarker, "click");
	window.location = "#" + kMapAnchorID;
}


// Show selected items.
//
function showFoundSetList() {
	
	var i;
	var h, r;
	var iString;
	
	r = "";

	for ( i = 1; i < mapDB.length; i++ ) {
	
			if ( i < 10 ) {
			
				iString = "&nbsp;" + i + "&nbsp;";	
				
			} else {
			
				iString = "" + i + "";
			}

			r += "<tr>";
			r += "<td align=center valign=top>" + "<a href='#' onclick='showPropertyInMap(" + i + "); return false;' onmouseover='gScrollList=false; GEvent.trigger(mapDB[" + i + "].theMarker, \"mouseover\"); gScrollList=true; return false;' onmouseout='GEvent.trigger(mapDB[" + i + "].theMarker, \"mouseout\"); return false;' class=numbers>" + iString + "</a>" + "</td>";
			r += "<td valign=top id='list" + i + "' style='font-size: 9pt; background-color: " + kListOffColor + ";''><span><a href='#' onclick='showPropertyInMap(" + i + "); return false;' onmouseover='gScrollList=false; GEvent.trigger(mapDB[" + i + "].theMarker, \"mouseover\"); gScrollList=true; return false;' onmouseout='GEvent.trigger(mapDB[" + i + "].theMarker, \"mouseout\"); return false;'><b>" + mapDB[i].theStreetAddress + "</b></a></span>";

			r += "<span style='font-variant: small-caps;'>";
			r += " " + mapDB[i].theTown.toLowerCase();
			r += "</span>";
			
			r += "<br>";
			
			if ( mapDB[i].theActionMoney != "" ) {
			
				r += "<b>" + formatPrice(mapDB[i].theActionMoney) + "</b><br>";
			}
			if ( mapDB[i].theActionDate != "" ) {
			
				r += "<b>" + mapDB[i].theActionDate + "</b><br>";
			}

			r += mapDB[i].theDescription;
			r += "</td>";
			
			r += "</tr>";			
	}
	
	h  = "";
	h += "<center><b>" + kListTitle + "</b></center>";
	h += "<table>";
	h += r;
	h += "</table>";
		
	document.getElementById(kHotID).innerHTML = h;

	if ( document.getElementById(kHotID2) != null ) {
		
		document.getElementById(kHotID2).innerHTML = h;
	}
}


function formatPrice( thePrice ) {

	var numString;
	var fmtString;
	
	numString = "" + Math.round( 1000 * thePrice );
	fmtString = "";

	while ( numString.length > 3 ) {
	
		fmtString = "," + numString.substr(numString.length - 3, 3) + fmtString;
		numString = numString.substr(0, numString.length - 3);
	}

	fmtString = numString + fmtString;
	
	return "$" + fmtString;
}


//
// XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
//
// The following implementations are unique to this particular solution.
//

// Filter out unwanted markers.
//
function applyFilters() {

	var i;
	
	for ( i = 1; i < mapDB.length; i++ ) {

		mapDB[i].inFoundSet = true;	// Assume it's a keeper
	}
}

// This returns the tool tip to be used.
//
function getTitle(latitude, longitude, actionMoney, streetAddress, town, actionDate, description) {

	return actionDate;
}

// This returns the text to be shown inside the marker.
//
function getLabelText(latitude, longitude, actionMoney, streetAddress, town, actionDate, description) {

	return gMarkerCount;
}

