// Mapping Software (functions)
//
// © 2009 Gary Little
//
// June 19, 2009
//


// Make a record representing a map item:
//
function makePropertyRecord( latitude, longitude, picture, name, town, rating, description ) {

	var rec = {};
	var theIcon;
	var xOffset, yOffset;
	var theTitle;

	gMarkerCount++;
	
	rec.inFoundSet = false;

	theIcon = new GIcon(G_DEFAULT_ICON, kMarkerOffURL);
	theIcon.printImage = kMarkerPrintURL;
	theIcon.mozPrintImage = kMarkerMozPrintURL;	
	theIcon.shadow = "";
	theIcon.printShadow = "";

	if ( gMarkerCount < 10 ) {

		xOffset = -2;
		
	} else if ( gMarkerCount < 100 ) {
	
		xOffset = -5;
		
	} else {
	
		xOffset = -8;
	}
	yOffset = -29;	

	theTitle = getTitle(latitude, longitude, picture, name, town, rating, description);
				
	rec.theMarker = new LabeledMarker(new GLatLng(latitude, longitude), {icon: theIcon, title: theTitle, labelText: gMarkerCount, labelOffset: new GSize(xOffset, yOffset), labelClass: kLabelClassName});
	rec.theMarker.index = gMarkerCount;

	rec.thePicture = picture;
	rec.theName = name;
	rec.theTown = town;
	rec.theRating = rating;
	rec.theDescription = description;
	
	return rec;
}


// Add all markers (and their listeners) to the map.
//
function addAllMarkers() {

	var i;
	var marker;
	
	for ( i = 0; i < propertyDB.length; i++ ) {

		marker = propertyDB[i].theMarker;

		GEvent.addListener( marker, "mouseover", function () {

			this.setImage(kMarkerOnURL);
			
			document.getElementById(kListItemID + this.index).style.backgroundColor = kListOnColor;
		} );
	
		GEvent.addListener( marker, "mouseout", function () {
			
			this.setImage(kMarkerOffURL);
			
			document.getElementById(kListItemID + this.index).style.backgroundColor = kListOffColor;
		} );
	
		GEvent.addListener( marker, "click", function () {

			var z = Math.max(gMap.getZoom(), kMinimumZoomLevel);
			var listItem = document.getElementById(kItemDetailID + this.index);
			
			gMap.setCenter(this.getPoint(), z);
		
			if ( listItem.style.display == "" ) {
			
				listItem.style.display = "none";
				
			} else {
			
				listItem.style.display = "";
			}
			
			if ( !gListClick ) {
			
				document.getElementById(kSidebarID).scrollTop = document.getElementById(kListItemID + this.index).offsetTop;
			}
		} );
	
		gMap.addOverlay(marker);
		marker.hide();
	}
}

// Set the visibility of all markers.
//
function setMarkerVisibility() {

	for ( i = 0; i < propertyDB.length; i++ ) {

		if ( propertyDB[i].inFoundSet ) {
		
			if ( propertyDB[i].theMarker.isHidden() ) {
			
				propertyDB[i].theMarker.show();
			}
			
		} else {
		
			if ( !propertyDB[i].theMarker.isHidden() ) {
			
				propertyDB[i].theMarker.hide();
			}
		}
	}
}

// Find all records which satisfy the search criteria,
// show only them on the map, and update the sidebar list.
//
function searchRecords() {

	applyFilters();
	setMarkerVisibility();
	showList();
}


// Add listeners for map events.
//
function addMapEventListeners() {

	GEvent.addListener( gMap, "maptypechanged", function () {

		switch ( this.getCurrentMapType() ) {
		
			case G_NORMAL_MAP:
			case G_PHYSICAL_MAP:
				gCopyright.style.color = "black";
				break;
				
			case G_HYBRID_MAP:
				gCopyright.style.color = "white";
				break;
				
			default:
				gCopyright.style.color = "white";
		}
	} );
}

// Show the "last updated" date.
//
function addDateDisplay( divID ) {

	if ( kShowDateDisplay ) {

		document.getElementById(divID).innerHTML = kLastUpdated;
	}
}


// Add a listener for updating the latitude and longitude display
// when the centre coordinates of the map changes.
//
function addLatLongDisplay( divID ) {

	GEvent.addListener( gMap, "move", function () {
		
		var cInfo;
		var c = gMap.getCenter();
		var latitude = c.lat();
		var longitude = c.lng();
		
		if ( kShowCoordinates ) {
		
			if ( latitude >= 0 ) {
			
				latitude = latitude.toFixed(5) + "&deg;N";
			
			} else {
			
				latitude = (-latitude).toFixed(5) + "&deg;S";
			}
			
			if ( longitude >= 0 ) {
			
				longitude = longitude.toFixed(5) + "&deg;E";
			
			} else {
			
				longitude = (-longitude).toFixed(5) + "&deg;W";
			}
			
			cInfo = latitude + " / " + longitude;
	
			document.getElementById(divID).innerHTML = cInfo;
		}
	} );
}


// Show the property in the map
//
function doMouseClick( i ) {

	gListClick = true;
	GEvent.trigger(propertyDB[i].theMarker, "click");
	gListClick = false;
}

function doMouseOver( i ) {

	GEvent.trigger(propertyDB[i].theMarker, "mouseover");
}

function doMouseOut( i ) {

	GEvent.trigger(propertyDB[i].theMarker, "mouseout");
}


//
// XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
//
// The following implementations are unique to this particular solution.
//

// Show the list of selected items.
//
function showList() {
	
	var i, id;
	var idString;
	var r = "";
	var h = "";
	
	for ( i = 0; i < propertyDB.length; i++ ) {
	
		if ( propertyDB[i].inFoundSet ) {
		
			id = propertyDB[i].theMarker.index;
		
			if ( id < 10 ) {
			
				idString = "&nbsp;" + id + "&nbsp;";
				
			} else {
			
				idString = id;
			}

			r += "<tr id='" + kListItemID + id + "'>";
			
			r += "<td align=center valign=top>";
			r += "<a href='javascript:void(0)' onclick='doMouseClick(" + i + ");' onmouseover='doMouseOver(" + i + ");' onmouseout='doMouseOut(" + i + ");'>";

				r += "<span class='" + kNumberClassName + "'>";
				r += idString;
				r += "</span>";
			
			r += "</a>";
			r += "</td>";
			
			r += "<td>";
			
			r += "<a href='javascript:void(0)' onclick='doMouseClick(" + i + ");' onmouseover='doMouseOver(" + i + ");' onmouseout='doMouseOut(" + i + ");'>";
			
				r += "<span class='" + kNameClassName + "'>";
				r += propertyDB[i].theName;
				r += "</span>";
			
			r += "</a>";

			r += "<span class='" + kTownClassName + "'>";
			r += " " + propertyDB[i].theTown;
			r += "</span>";

			if ( propertyDB[i].thePicture != "" ) {

				r += " <img src='" + kCameraIconURL + "'>";
			}
			
			if ( propertyDB[i].theRating == 5 ) {

				r += " <img src='" + kStarIconURL + "'>";
			}
			
			r += "<div id='" + kItemDetailID + id + "' style='display: none;'>";

				if ( propertyDB[i].thePicture != "" ) {
				
					r += "<img src = '" + propertyDB[i].thePicture + "' width=" + kPictureWidth + ">";
				}
				
				if ( propertyDB[i].theDescription != "" ) {
				
					r += "<br>";
					r += propertyDB[i].theDescription;
				}
			
			r += "</div>";

			r += "</td>";
			
			r += "</tr>";
		}
	}
	
	h += "<div class='" + kTitleClassName + "'>";
	h += kListTitle;
	h += "</div>";
	h += "<hr>";
	
	h += "<table>";
	h += r;
	h += "</table>";
	h += "<hr>";
		
	document.getElementById(kSidebarID).innerHTML = h;

	if ( document.getElementById(kListPrintID) !== null ) {
		
		document.getElementById(kListPrintID).innerHTML = h;
	}
}

// Return the tool tip to be used.
//
function getTitle(latitude, longitude, picture, name, town, rating, description) {

	return " " + name + " / " + town + " ";
}

// Filter out unwanted records.
//
function applyFilters() {

	var i;
	var isDam

	for ( i = 0; i < propertyDB.length; i++ ) {

		propertyDB[i].inFoundSet = true;	// Assume it's a keeper
		
		if ( propertyDB[i].theMarker.getPoint().lat() == 0 && propertyDB[i].theMarker.getPoint().lng() == 0 ) {

			propertyDB[i].inFoundSet = false;
			continue;
		}
		
		isDam = (propertyDB[i].theRating == 0);
		if ( gDamFlag && !isDam || !gDamFlag && isDam ) {
		
			propertyDB[i].inFoundSet = false;
			continue;
		}

		if ( propertyDB[i].theRating < gMinimumRating ) {
		
			propertyDB[i].inFoundSet = false;
			continue;
		}

		if ( propertyDB[i].thePicture == "" && gPictureFlag ) {
		
			propertyDB[i].inFoundSet = false;
			continue;
		}
	}
}

function toggleRating( f ) {

	if ( f.elements[kRatingsBox].checked  ) {
	
		gMinimumRating = 5;
		
	} else {
	
		gMinimumRating = 0;
	}
	
	searchRecords();
}

function togglePicture( f ) {

	if ( f.elements[kPictureBox].checked  ) {
	
		gPictureFlag = true;
		
	} else {
	
		gPictureFlag = false;
	}
	
	searchRecords();
}

function toggleDam( f ) {

	if ( f.elements[kDamBox].checked  ) {
	
		gDamFlag = true;
		
	} else {
	
		gDamFlag = false;
	}
	
	searchRecords();
}
