/**
 * ERMapItem is a "class" that holds the information for a single listing or POI
 *
 * @param	data			Object containing the data for this listing/poi
 * @param	searchFields	List of properties that can be searched
 * @param	onByDefault		Boolean value indicating whether listing/poi is visible when it is loaded
 * @param	resultBox		Pointer to object that handles search results
 */
function ERMapItem(params) {
	// Add data to this object
	jQuery.extend(this, params.data);
	
	
	// Create a Map Marker for this point
	var icon   = new google.maps.Icon(gm_baseIcon);
	icon.image = this.mapIconUrl;
	var point  = new google.maps.LatLng(this.lat, this.lon);
	var opts   = {'icon':icon, 'title':this.desc};
	this.gm_marker = new google.maps.Marker(point, opts);
	
	google.maps.Event.addListener(this.gm_marker,"click", function() { params.onClickIcon(params.data.id) });
	
	// Set the the initial searchResult values
	this.searchResults = {};
	var that = this;
	this.isVisible = !params.onByDefault;
	jQuery.each(params.searchFields, function(i, n) {
		that.searchResults[n] = true;
	});
	
	// Add the result line to the correct result box
	this.resultBox = params.resultBox;
	this.resultBox.add(this);
	
	// Set inititial visibility
	this.checkVisibility();
}
/**
 * Decides whether or not this item should be visible
 */
ERMapItem.prototype.doSearch = function(query) {
	var obj = this;
	for (var field in query) {
		var vals = query[field].split("\t");
		var type = vals.shift();
		if (type == 'in') {
			// Test member of set
			var foundInList = false;
			jQuery.each(vals, function(i, n) { if (n == obj[field]) foundInList = true; });
			obj.searchResults[field] = foundInList;
		} else if (type == 'btwn') {
			// Test min/max
			this.searchResults[field] = (this[field] >= vals[0]  &&  this[field] <= vals[1]);
		} else if (type == 'eq') {
			this.searchResults[field] = (this[field] == vals[0]);
		} else if (type == 'nq') {
			this.searchResults[field] = (this[field] != vals[0]);
		}
	}
	this.checkVisibility();
}
/**
 * Adjusts this item's visibility if necessary
 */
ERMapItem.prototype.checkVisibility = function() {
	var visible = true;
	for (var n in this.searchResults) {
		visible = this.searchResults[n];
		if (!visible) break;
	}
	if (visible  &&  !this.isVisible) {
		ERMap.gm_map.addOverlay(this.gm_marker);
		this.resultBox.show(this);
		this.isVisible = true;
	} else if (!visible  &&  this.isVisible) {
		ERMap.gm_map.removeOverlay(this.gm_marker);
		this.resultBox.hide(this);
		this.isVisible = false;
	}
}

