/**
 * @author john
 */
function resultSet(params) {
	this.lastSort = false;
	this.childItems = Array();
	
	for(var pname in params) {
		this[pname] = params[pname];
	}
	
	this.saleInfo = new SaleInfo(convertToListHtml(this.defaultContent));
	
	var setId = this.id;
	
	this.loadData = function(searchQueryString) {
		var retries = 0;
		var o = this;
		this.hasData = true;
		
		var retryDataLoad = function() {
			retries++;
			if(retries < 3) {
				requestAsyncSearch(o, searchQueryString, retryDataLoad, RM.searchToken);
			} else {
				o.setStatus("connectionFailure");
			}
		}
		
		if(this.searchScript) {
			this.setStatus("loading");
			requestAsyncSearch(this, searchQueryString, retryDataLoad, RM.searchToken);
		}
	}
	
	this.setStatus = function(status) {
		switch(status) {
			case "loading":
				this.saleInfo.set("content", convertToListHtml("<br><br><center><h2><img src='" + baseURL + imageDir + "loading_big.gif' alt='Loading...'> Loading " + this.label + "...</h2></center>"));
				this.saleInfo.set("label", this.label);
				break;
			case "searchComplete":
				//todo: display result count
				break;
			case "connectionFailure":
				RM.logError("connectionFailure", this);
				break;
		}
	}
	
	this.setItems = function(functions, items) {
		if(this.childItems.length > 0) {
			this.clearChildren();
		}
		
		this.childItems = Array();
		
		for(var idx in items) {
			var newItem = new resultItem(items[idx]);
			newItem.parent = this;
			this.childItems.push(newItem);
		}
		
		this.functions = functions;
	}
	
	this.clearChildren = function() {
		for(var childId in this.childItems) {
			if(this.geoCodeable && this.childItems[childId].marker) {
				map.removeOverlay(this.childItems[childId].marker);
			}
			
			this.childItems[childId].deleteProperties();
			delete(this.childItems[childId]);
		}
	}
	
	this.displayItems = function() {
		this.saleInfo.set("content", this.getResultListHtml());
		
		if(this.geoCodeable) {
			this.renderMapMarkers();
		}
	}
	
	this.renderMapMarkers = function() {
		for(var idx in this.childItems) {
			if(!this.childItems[idx].marker) {
				this.childItems[idx].renderMarker();
			}
		}
	}
	
	this.getResultListHtml = function() {
		var output = '';
		if(this.childItems.length > 0) {
			for(var idx in this.childItems) {
				this.childItems[idx].idx = idx;
				output += this.childItems[idx].getListHTML();
			}
		} else {
			output = '<div style="border-color: rgb(221, 221, 202);" id="grey" class="leftBox clickableRow">No ' + this.label + ' results found</div>';
		}
		
		return output;
	}
	
	this.sortSet = function(field, direction) {
		this.lastSort = {"field":field, "direction":direction};
		
		if(this.backEndSort && this.functions.totalResults > 100) {
			//get backend sort
			var sqs = RM.searchQueryString;
			
			sqs += "&sort=" + field + "&direction=" + direction;
			
			RM.setsReturned--;
			this.clearChildren();
			this.loadData(sqs);
			EPS.activePage = 1;
		} else {
			//front end sort
			if(this.sortFields[field]) {
				var thisSet = this;
				
				if(this.childItems.length > 0) {
					
					if(isNumber(this.childItems[0].properties[field])) {
						
						if(direction == 'asc') {
							var sorter = function(a, b) {
								return a.properties[field] - b.properties[field];
							};
						} else {
							var sorter = function(b, a) {
								return a.properties[field] - b.properties[field];
							};
						}
					} else {
						//string sort
						if(direction == "asc") {
							var sorter = function(a, b) {
								if(a.properties[field] > b.properties[field]) {
									return 1;
								} else if(a.properties[field] < b.properties[field]) {
									return -1;
								} else if(a.properties[field] == b.properties[field]) {
									return 0;
								}
							};
						} else {
							var sorter = function(b, a) {
								if(a.properties[field] > b.properties[field]) {
									return 1;
								} else if(a.properties[field] < b.properties[field]) {
									return -1;
								} else if(a.properties[field] == b.properties[field]) {
									return 0;
								}
							};
						}
					}
					thisSet.childItems.sort(sorter);
					this.tab.set("content", this.getResultListHtml());
				}
			}
		}
	}
}