/**
 * @author john
 */
function resultManager(rtc) {
	this.resultColumn = rtc;
	this.childSets = Array();
	this.currentSearchString = null;
	this.lastSearchTime = 0;
	this.setCount = 0;
	this.setsReturned = 0;
	this.searchToken = '';
	this.searchQueryString = '';
	this.requiredParams = '';
	this.lastReq = '';
	this.locName = '';
	this.activeTabId = null;
	
	this.addSet = function(newSet) {
		newSet.parent = this;
		this.childSets[newSet.id] = newSet;
		this.setCount++;
	}
	
	this.initiateResultSets = function() {
		for(var id in this.childSets) {
			this.resultColumn.addSaleInfo(this.childSets[id].saleInfo);
			//this.resultTabControl.addTab(this.childSets[id].tab);
		}
	}
	
	this.updateLocName = function(loc) {
		this.locName = loc;
		this.drawNavStatus();
	}
	
	this.setActiveSet = function(setId) {
		this.activeTabId = setId;
		this.drawNavStatus();
	}
	
	this.drawNavStatus = function() {
		var output = "<b>Currently Viewing:</b> " + this.locName;
		document.getElementById("nowViewing").innerHTML = output;
	}
	
	this.geoCodeAndSearch = function(popfirst) {
		var valid = true;
		
		if(this.requiredParams.length > 0) {
			for(var p in this.requiredParams) {
				if(this.requiredParams[p]) {
					if(document.getElementById(this.requiredParams[p]).value == '') {
						valid = false;
						document.getElementById(this.requiredParams[p]).focus();
						showDomError(this.requiredParams[p], "<b>" + document.getElementById(this.requiredParams[p]).title + " is a required field.</b>");
						return false;
					}
				}
			}
		}
		
		if(document.getElementById('l').value == '[ZIP]') {
			valid = false;
			return false;
		}
		
		if(valid) {
			map.closeInfoWindow();
			
			var requestUrl = baseURL + "geocode.php?action=location&" + formToUrl("sForm");
			
			if(popfirst) {
				requestUrl += "&pf=1";
			}
			
			this.lastReq = requestUrl;
			var request = YAHOO.util.Connect.asyncRequest('GET', requestUrl, {success: evaluator, failure: this.logError});
		}
	}
	
	this.logError = function(err, o) {
		var errortext = err + "\nRequest Url:\n" + this.lastReq + "\n\nargument:" + o.argument + "\n\n response:\n" + o.responseText;
		notifyError(errortext);
		throw(err);
	}
	
	this.performGlobalSearch = function(popfirst) {
		this.setsReturned = 0;
		PB.resetProgress();
		PB.setProgress(5, 1, 1);
		
		map.checkResize();
		this.clearAllResults();
		this.searchToken = Math.random();
		this.searchQueryString = formToUrl("sForm");
		
		for(var setId in this.childSets) {
			this.childSets[setId].loadData(this.searchQueryString);
		}
		var d = new Date();
		this.lastSearchTime = Math.floor(d.getTime()/1000);
		this.showSearchStatus();
		if(popfirst) {
			this.onSearchFinish = function() {
				if(tabView.getTabIndex(tabView.get("activeTab")) == 0) {
					for(var z in RM.childSets) {
						if(RM.childSets[z].childItems.length > 0) {
							RM.childSets[z].childItems[0].listClick();
						}
						break;
					}
				}
			};
		}
	}
	
	this.saveAlert = function(subscribe) {
		var requestUrl = 'mygarage/savealert.php?' + this.searchQueryString;
		this.lastReq = requestUrl;
		var request = YAHOO.util.Connect.asyncRequest('GET', requestUrl, {success: evaluator, failure: RM.logError});
	}
	
	this.showAlertSaved = function() {
		var sl = document.getElementById("generalMessageBox");
		
		sl.innerHTML = "Alert Saved!  You will now be notified when that search term appears in your area.";
	}
	
	this.showSaveSearchLink = function() {
		var sl = document.getElementById("generalMessageBox");
		
		if(sl && document.getElementById('q').value != '') {
			var l = '<a href="javascript: RM.saveAlert(\'' + escape(document.getElementById('q').value) + '\')">Let me know</a> when more &quot;';
			l += '<b>' + document.getElementById('q').value + '</b>&quot; become available';
		} else {
			l = 'Search Complete';
		}
		
		sl.innerHTML = l;
	}
	
	this.showSearchStatus = function() {
		document.getElementById('generalMessageBox').innerHTML = '<img width="16" height="16" src="' + baseURL + imageDir + 'loading_snake_top.gif"> Searching...';
	}
	
	this.performSavedSearch = function(params, popfirst) {
		setFormParams(params);
		
		this.geoCodeAndSearch(popfirst);
		
		return false;
	}
	
	this.asyncResultReturn = function(setId, functions, items) {			
		this.setsReturned++;
		
		this.childSets[setId].setItems(functions, items);
		this.childSets[setId].setStatus("searchComplete");
		
		this.childSets[setId].displayItems();
		
		PB.setProgress(Math.round((this.setsReturned/this.setCount) * 100), 1, 1);
		if(this.setCount == this.setsReturned) {
			this.onSearchComplete();
		}
	}
	
	this.clearAllResults = function() {
		for(var id in this.childSets) {
			this.childSets[id].clearChildren();
		}
		resTabColumn.resetContent();
	}
	
	this.onSearchComplete = function() {
		this.showSaveSearchLink();
		if(this.onSearchFinish) {
			this.onSearchFinish();
			delete(this.onSearchFinish);
		}
	}
	
	this.listItemClick = function(setId, itemId) {
		this.childSets[setId].childItems[itemId].listClick();
	}
	
	this.addToItinerary = function(setId, itemId, refer) {
		if(refer == undefined) {
			checkLogin('showItinerarySelection("' + setId + '", "' + itemId + '")', 'showLogin("You must login first")');
		} else {
			checkLogin('showItinerarySelection("' + setId + '", "' + itemId + '")', 'document.location = "login.php?redirect=' + refer + '"');
		}
	}
	
	this.openTab = function(setId, itemId) {
		this.childSets[setId].childItems[itemId].openTab();
	}
}