//dojo initialization stuff
dojo.require("esri.tasks.identify");
dojo.require("esri.tasks.query");

//declare variables
var queryTask, query, queryTaskIntersection, queryIntersection;
var identifyTask, identifyParams;
var queryType;
var hostName = "wcgisweb.washoecounty.us:8888";

//initialization
	function Init() {
		//address query initialization.
		queryTaskAddress = new esri.tasks.QueryTask("http://" + hostName + "/ArcGIS/rest/services/BaseLayersQueryWebMercator/MapServer/0");
		queryAddress = new esri.tasks.Query();
		queryAddress.returnGeometry = true;
		queryAddress.outFields = ["OBJECTID","ADDRESS","ADDNUM","ADDNUMSUFF","STDIRPRE","STREET","STTYPE","STDIRSUF","OCCUTYPE","OCCID","STRUCTURE","FLOOR","UNIT","MUNI","COUNTY","STATE","ZIP","ZIP4","PIN","PAI","CONFIDENCE","QC","SOURCE","EDITORG","TECHNICIAN","DATEORIGIN","DATEMOD","COMMENTS","GLOBALID"];

		//intersection query initialization.
		queryTaskIntersection = new esri.tasks.QueryTask("http://" + hostName + "/ArcGIS/rest/services/BaseLayersQueryWebMercator/MapServer/1");
		queryIntersection = new esri.tasks.Query();
		queryIntersection.returnGeometry = true;
		queryIntersection.outFields = ["OBJECTID","NAME1","NAME2","NAME3"];

		//parcel number query initialization. -M. Lawton 06/02/2009
		queryTask = new esri.tasks.QueryTask("http://" + hostName + "/ArcGIS/rest/services/BaseLayersQueryWebMercator/MapServer/2");
		query = new esri.tasks.Query();
		query.returnGeometry = true;
		query.outFields = ["PIN","LINK","FIRSTNAME","LASTNAME","STREETNUM","STREETDIR","STREET","CITY","ACREAGE","BEDROOMS","BATHS","YEARBLT","ZONING","TAXDIST","taxvalue","grosstax","abate","exempt","nettax"];
	}

//formats spelled-out numeric street names in address and intersection searches so that they match the format of our data, also fixes 'saint' and 'mount' spellings
function numericStreetName(streetName) {
	streetName = streetName.replace(/twentysecond/gi,"22nd");
	streetName = streetName.replace(/twentytwo/gi,"22");
	streetName = streetName.replace(/twentyfirst/gi,"21st");
	streetName = streetName.replace(/twentyone/gi,"21");
	streetName = streetName.replace(/twentieth/gi,"20th");
	streetName = streetName.replace(/twenty/gi,"20");
	streetName = streetName.replace(/nineteenth/gi,"19th");
	streetName = streetName.replace(/nineteen/gi,"19");
	streetName = streetName.replace(/eighteenth/gi,"18th");
	streetName = streetName.replace(/eighteen/gi,"18");
	streetName = streetName.replace(/seventeenth/gi,"17th");
	streetName = streetName.replace(/seventeen/gi,"17");
	streetName = streetName.replace(/sixteenth/gi,"16th");
	streetName = streetName.replace(/sixteen/gi,"16");
	streetName = streetName.replace(/fifteenth/gi,"15th");
	streetName = streetName.replace(/fifteen/gi,"15");
	streetName = streetName.replace(/fourteenth/gi,"14th");
	streetName = streetName.replace(/fourteen/gi,"14");
	streetName = streetName.replace(/thirteenth/gi,"13th");
	streetName = streetName.replace(/thirteen/gi,"13");
	streetName = streetName.replace(/twelfth/gi,"12th");
	streetName = streetName.replace(/twelve/gi,"12");
	streetName = streetName.replace(/eleventh/gi,"11th");
	streetName = streetName.replace(/eleven/gi,"11");
	streetName = streetName.replace(/tenth/gi,"10th");
	streetName = streetName.replace(/ninth/gi,"9th");
	streetName = streetName.replace(/eighth/gi,"8th");
	streetName = streetName.replace(/seventh/gi,"7th");
	streetName = streetName.replace(/sixth/gi,"6th");
	streetName = streetName.replace(/fifth/gi,"5th");
	streetName = streetName.replace(/fourth/gi,"4th");
	streetName = streetName.replace(/third/gi,"3rd");
	streetName = streetName.replace(/second/gi,"2nd");
	streetName = streetName.replace(/first/gi,"1st");
	streetName = streetName.replace(/^mt /gi,"MOUNT ");
	streetName = streetName.replace(/^st /gi,"SAINT ");
	streetName = streetName.replace(/^mt. /gi,"MOUNT ");
	streetName = streetName.replace(/^st. /gi,"SAINT ");
	return streetName;
}

//Listens in the address search fields for an 'Enter' keypress from the user and executes address query
function addressKeyPress(charCode) {
		if (charCode == 13) {
			executeAddress(dojo.byId('addressNumber').value,dojo.byId('streetDir').value,dojo.byId('streetName').value);
		} else if (charCode == undefined) {
			uniCode = window.event.keyCode;
			if (uniCode == 13) {
				executeAddress(dojo.byId('addressNumber').value,dojo.byId('streetDir').value,dojo.byId('streetName').value);
			}
		}
}

//Listens in the parcel search field for an 'Enter' keypress from the user and executes parcel query
function parcelKeyPress(charCode) {
		if (charCode == 13) {
			executeParcel(dojo.byId('parcelNumber').value);
		} else if (charCode == undefined) {
			uniCode = window.event.keyCode;
			if (uniCode == 13) {
				executeParcel(dojo.byId('parcelNumber').value);
			}
		}
}

//Listens in the intersection search fields for an 'Enter' keypress from the user and executes interection query
function intersectionKeyPress(charCode) {
		if (charCode == 13) {
			executeIntersection(dojo.byId('crossStreet1').value,dojo.byId('crossStreet2').value,dojo.byId('crossStreet3').value);
		} else if (charCode == undefined) {
			uniCode = window.event.keyCode;
			if (uniCode == 13) {
				executeIntersection(dojo.byId('crossStreet1').value,dojo.byId('crossStreet2').value,dojo.byId('crossStreet3').value);
			}
		}
}

//BEGIN QUERY CODE
	//executes point address query by address number, street direction, and street name
	function executeAddress(addressNumber,streetDir,streetName) {
		document.getElementById('lightbox').innerHTML = "<div class=\"grid_10 center small\">Please wait while we retrieve your data...<br /><img src=\"../images/loading.gif\" width=\"200\" height=\"20\" alt=\"Data is loading.  Please wait.\"></div>";
		var displayme = document.getElementById('lightbox').style;
		var maskme = document.getElementById('mask').style;
		maskme.display="block";
		displayme.display="block";
		a1 = addressNumber;
		//if nothing is entered for address number then it is set to blank
		if (a1 == "Enter Address#") {
			a1 = "";
		}
		a1 = a1.replace(/[^A-Za-z0-9]/g, '');
		a2 = streetDir;
		//fix numeric street names
		var m_strOut = numericStreetName(streetName);
		//if nothing is entered for street name then it is set to blank
		if (m_strOut == "Street Name") {
			m_strOut = "";
		}
		//replace symbols and spaces with wildcards
		m_strOut = m_strOut.replace(/[^A-Za-z0-9]/g, '%');
		//construct address where statement
		queryAddress.where = "ADDNUM LIKE " + "'%" + a1 + "%' AND STDIRPRE LIKE " + "'%" + a2 + "%' AND REPLACE(REPLACE(STREET,'`',''),' ','') LIKE " + "'%" + m_strOut + "%'"; //AND UNIT LIKE " + "'%" + a4 + "%'"
		//query.where = "STREETNUM LIKE " + "'%" + a1 + "%' AND STREETDIR LIKE " + "'%" + a2 + "%' AND REPLACE(REPLACE(STREET,'`',''),' ','') LIKE '%" + m_strOut + "%'";
		queryType = "address";
		//execute query and call showResults on completion
		queryTaskAddress.execute(queryAddress,showResults);
	}

	//executes an address point query by the objectid. This is necessary for addresses that may get returned as exact duplicates. The objectid is the primary key field and will allow the user to query between addresses that are otherwise identical.
	function executeAddressObjectId(objectId) {
		//construct address where statement
		queryAddress.where = "OBJECTID = " + objectId;
		queryType = "addressobjectid";
		//execute query and call showResults on completion
		queryTaskAddress.execute(queryAddress,showResults);
	}

	//if a street name cannot be matched as spelled by the user then a SOUNDEX comparison is performed to provide possible matches
	function executeStreetNameSoundex(addressNumber,streetDir,streetName) {
		//fix numeric street names
		m_strOut = numericStreetName(streetName);
		if (m_strOut == "Street Name") {
			m_strOut = "";
		}
		//remove symbols and spaces
		m_strOut = m_strOut.replace(/[^A-Za-z0-9]/g, '');
		//construct address where statement
		queryAddress.where = "SOUNDEX(STREET) = " + "SOUNDEX('" + m_strOut + "') OR SOUNDEX(REPLACE(STREET,' ','')) = " + "SOUNDEX('" + m_strOut + "')";
		queryType = "streetnamesoundex";
		//execute query and call showResults on completion
		queryTaskAddress.execute(queryAddress,showResults);
	}

	//executes parcel address query on parcel situs data
	function executeParcelAddress(addressNumber,streetDir,streetName) {
		a1 = addressNumber;
		//if nothing is entered for address number then it is set to blank
		if (a1 == "Enter Address#") {
			a1 = "";
		}
		a1 = a1.replace(/[^A-Za-z0-9]/g, '');
		a2 = streetDir;
		//fix numeric street names
		m_strOut = numericStreetName(streetName);
		//if nothing is entered for street name then it is set to blank
		if (m_strOut == "Street Name") {
			m_strOut = "";
		}
		//replace symbols and spaces with wildcards
		m_strOut = m_strOut.replace(/[^A-Za-z0-9]/g, '%');
		//construct address where statement
		query.where = "STREETNUM LIKE " + "'%" + a1 + "%' AND STREETDIR LIKE " + "'%" + a2 + "%' AND REPLACE(REPLACE(STREET,'`',''),' ','') LIKE '%" + m_strOut + "%'";
		//alert(query.where);
		queryType = "parceladdress";
		//execute query and call showResults on completion
		queryTask.execute(query,showResults);
	}

	//if a street name cannot be matched as spelled by the user then a SOUNDEX comparison is performed to provide possible matches
	function executeParcelStreetNameSoundex(addressNumber,streetDir,streetName) {
		//fix numeric street names
		m_strOut = numericStreetName(streetName);
		//if nothing is entered for street name then it is set to blank
		if (m_strOut == "Street Name") {
			m_strOut = "";
		}
		//remove symbols and spaces
		m_strOut = m_strOut.replace(/[^A-Za-z0-9]/g, '');
		//construct address where statement
		query.where = "SOUNDEX(STREET) = " + "SOUNDEX('" + m_strOut + "') OR SOUNDEX(REPLACE(STREET,' ','')) = " + "SOUNDEX('" + m_strOut + "')";
		queryType = "parcelstreetnamesoundex";
		//execute query and call showResults on completion
		queryTask.execute(query,showResults);
	}

	//executes parcel number query
	function executeParcel(pin) {
		//Strips alpha and symbol characters from entry and replaces with wildcards.
                pin = pin.replace(/[^0-9]/g, '%');
		//construct parcel number where statement
		query.where = "replace(PIN,'-','') LIKE " + "'%" + pin + "%'";
		queryType = "parcel";
		//execute query
		queryTask.execute(query,showResults);
	}

	//executes intersection query
	function executeIntersection(crossStreet1,crossStreet2,crossStreet3) {
		c1 = crossStreet1;
		c2 = crossStreet2;
		c3 = crossStreet3;
		//fix numeric street names
		c1 = numericStreetName(c1);
		c2 = numericStreetName(c2);
		c3 = numericStreetName(c3);
		//if nothing is entered for street name then it is set to blank
		if (c1 == "Cross Street 1") {
			c1 = "";
		}
		if (c2 == "Cross Street 2") {
			c2 = "";
		}
		if (c3 == "Cross Street 3") {
			c3 = "";
		}
		//replace symbols and spaces with wildcards
		c1 = c1.replace(/[^A-Za-z0-9]/g, '%');
		c2 = c2.replace(/[^A-Za-z0-9]/g, '%');
		c3 = c3.replace(/[^A-Za-z0-9]/g, '%');
		//construct intersection where statement
		queryIntersection.where = "(REPLACE(REPLACE(NAME1,'`',''),' ','') LIKE '%" + c1 + "%' OR REPLACE(REPLACE(NAME2,'`',''),' ','') LIKE '%" + c1 + "%' OR REPLACE(REPLACE(NAME3,'`',''),' ','') LIKE '%" + c1 + "%') AND (REPLACE(REPLACE(NAME1,'`',''),' ','') LIKE '%" + c2 + "%' OR REPLACE(REPLACE(NAME2,'`',''),' ','') LIKE '%" + c2 + "%' OR REPLACE(REPLACE(NAME3,'`',''),' ','') LIKE '%" + c2 + "%') AND (REPLACE(REPLACE(NAME1,'`',''),' ','') LIKE '%" + c3 + "%' OR REPLACE(REPLACE(NAME2,'`',''),' ','') LIKE '%" + c3 + "%' OR REPLACE(REPLACE(NAME3,'`',''),' ','') LIKE '%" + c3 + "%')";
		queryType = "intersection";
		//execute query and call showResults on completion
		queryTaskIntersection.execute(queryIntersection,showResults);
	}

	//if a street name cannot be matched as spelled by the user then a SOUNDEX comparison is performed to provide possible matches
	function executeIntersectionStreetNameSoundex(crossStreet1,crossStreet2,crossStreet3) {
		var c1 = crossStreet1;
		var c2 = crossStreet2;
		var c3 = crossStreet3;
		var operatorOne = '=';
		var operatorTwo = '=';
		var operatorThree = '=';
		//if nothing is entered for the street name field then a % wildcard is entered so it will not restrict the query results
		if (c1 == "Cross Street 1") {
			c1 = " '%'";
			operatorOne = 'LIKE';
		} else {
			c1 = " SOUNDEX('" + c1.replace(/[^A-Za-z0-9]/g, '') + "')";
		}
		if (c2 == "Cross Street 2") {
			c2 = " '%'";
			operatorTwo = 'LIKE';
		} else {
			c2 = " SOUNDEX('" + c2.replace(/[^A-Za-z0-9]/g, '') + "')";
		}
		if (c3 == "Cross Street 3") {
			c3 = " '%'";
			operatorThree = 'LIKE';
		} else {
			c3 = " SOUNDEX('" + c3.replace(/[^A-Za-z0-9]/g, '') + "')";
		}
		//construct address where statement
		queryIntersection.where = "(SOUNDEX(REPLACE(REPLACE(NAME1,'`',''),' ','')) " + operatorOne + c1 + " OR SOUNDEX(REPLACE(REPLACE(NAME2,'`',''),' ','')) " + operatorOne + c1 + " OR SOUNDEX(REPLACE(REPLACE(NAME3,'`',''),' ','')) " + operatorOne + c1 + ") AND (SOUNDEX(REPLACE(REPLACE(NAME1,'`',''),' ','')) " + operatorTwo + c2 + " OR SOUNDEX(REPLACE(REPLACE(NAME2,'`',''),' ','')) " + operatorTwo + c2 + " OR SOUNDEX(REPLACE(REPLACE(NAME3,'`',''),' ','')) " + operatorTwo + c2 + ") AND (SOUNDEX(REPLACE(REPLACE(NAME1,'`',''),' ','')) " + operatorThree + c3 + " OR SOUNDEX(REPLACE(REPLACE(NAME2,'`',''),' ','')) " + operatorThree + c3 + " OR SOUNDEX(REPLACE(REPLACE(NAME3,'`',''),' ','')) " + operatorThree + c3 + ")";
		//alert(queryIntersection.where);
		queryType = "intersectionstreetnamesoundex";
		//execute query and call showResults on completion
		queryTaskIntersection.execute(queryIntersection,showResults);
	}

	//executes an intersection query by the objectid. This is necessary for intersections that may get returned as exact duplicates. The objectid is the primary key field and will allow the user to query between intersections that are otherwise identical.
	function executeIntersectionObjectId(objectId) {
		//construct address where statement
		queryIntersection.where = "OBJECTID = " + objectId;
		queryType = "intersectionobjectid";
		//execute query and call showResults on completion
		queryTaskIntersection.execute(queryIntersection,showResults);
	}

	//returns results for query
	function showResults(results) {
		//clears the searchInfo div

		var s="";

		//identifies the search type that has been performed
		if (queryType == "streetnamesoundex") {
			s += "No address match found.<br/>";
			s += "<a href='#' onclick='executeParcelStreetNameSoundex(\"" + dojo.byId('addressNumber').value + "\",\"" + dojo.byId('streetDir').value + "\",\"" + dojo.byId('streetName').value + "\"); return false;'>Click here to search Parcel Situs Address records.</a></i>";
			if (results.features.length > 0) {
				s +="<br/><br/><em>Are you looking for...<br/>"
				for (var i=0, il=results.features.length; i<il; i++) {
					var featureAttributes = results.features[i].attributes;
					s += "<a href='' onclick='executeAddressObjectId(" + featureAttributes['OBJECTID'] + "); return false;'><i>" + featureAttributes['ADDRESS'] + " " + featureAttributes['MUNI'] + "<a/></em><br/>";
				}
			}
			dojo.byId("lightbox").innerHTML = s;
		} else if (queryType == "parcelstreetnamesoundex" && results.features.length > 0) {
			s += "No parcel match found.";
			s +="<br/><br/><em>Are you looking for...</em><br/>"
			for (var i=0, il=results.features.length; i<il; i++) {
				var featureAttributes = results.features[i].attributes;
				s += "<a href='' onclick='executeParcel(\"" + featureAttributes['PIN'] + "\"); return false;'><i>" + featureAttributes['STREETNUM'] + " " + featureAttributes['STREETDIR'] + " " + featureAttributes['STREET'] + " " + featureAttributes['CITY'] + "</i> (APN: " + featureAttributes['PIN'] + ")</a><br/>";
			}
			dojo.byId("lightbox").innerHTML = s;
		} else if (results.features.length == 0) { //query returns no matches.
			if (queryType == "address") {
				queryType = "";
				executeParcelAddress(dojo.byId('addressNumber').value,dojo.byId('streetDir').value,dojo.byId('streetName').value);
			} else if (queryType == "parceladdress") {
				queryType = "";
				executeStreetNameSoundex(dojo.byId('addressNumber').value,dojo.byId('streetDir').value,dojo.byId('streetName').value);
			} else if (queryType == "intersection") {
				queryType = "";
				executeIntersectionStreetNameSoundex(dojo.byId('crossStreet1').value,dojo.byId('crossStreet2').value,dojo.byId('crossStreet3').value);
			} else {
				s += "No match found. Please retry your search.";
				dojo.byId("lightbox").innerHTML = s;
			}
		} else if (results.features.length > 1 && (queryType == "parcel" || queryType == "parceladdress")) { //query returns multiple matches.
			if (results.features.length == 500) {
				s += "Search stopped at 500 possible matches found.<br />";
			} else {
				s += "<strong>" + results.features.length + "</strong> possible parcel matches found.<br />";
			}
			s += "<br/><em>Are you looking for...</em><br/>";
			for (var i=0, il=results.features.length; i<il; i++) {
				var featureAttributes = results.features[i].attributes;
				s += "<a href='#' onclick='executeParcel(\"" + featureAttributes['PIN'] + "\"); return false;'><i>" + featureAttributes['STREETNUM'] + " " + featureAttributes['STREETDIR'] + " " + featureAttributes['STREET'] + " " + featureAttributes['CITY'] + "</i> (APN: " + featureAttributes['PIN'] + ")</a><br/>";
			}
			dojo.byId("lightbox").innerHTML = s;
		} else if (results.features.length > 1 && queryType == "intersection" || queryType == "intersectionstreetnamesoundex") { //query returns multiple matches.
			if (results.features.length == 500) {
				s += "Search stopped at 500 possible matches found.<br />";
			} else {
				s += "<strong>" + results.features.length + "</strong> possible matches found.<br />";
			}
			s += "Select Item | Cross Street 1 | Cross Street 2 | Cross Street 3";
			for (var i=0, il=results.features.length; i<il; i++) {
				var featureAttributes = results.features[i].attributes;
				s += "<p><a href='#' onclick='executeIntersectionObjectId(" + featureAttributes['OBJECTID'] + "); return false;'>Click to Zoom</a> " + featureAttributes['NAME1'] + " " + featureAttributes['NAME2'] + " " + featureAttributes['NAME3'] + "</p>";
			}
			dojo.byId("lightbox").innerHTML = s;
		} else if (results.features.length > 1 && queryType == "address") { //query returns multiple matches.
			if (results.features.length == 500) {
				s += "Search stopped at 500 possible matches found.<br />";
			} else {
				s += "<strong>" + results.features.length + "</strong> possible address matches found.<br />";
			}
			s += "<a href='#' onclick='executeParcelAddress(\"" + dojo.byId('addressNumber').value + "\",\"" + dojo.byId('streetDir').value + "\",\"" + dojo.byId('streetName').value + "\"); return false;'>Click here to search Parcel Situs Address records.</a><br/><br/>";
			s += "<em>Are you looking for...</em><br/>";
			for (var i=0, il=results.features.length; i<il; i++) {
				var featureAttributes = results.features[i].attributes;
				s += "<a href='#' onclick='executeAddressObjectId(" + featureAttributes['OBJECTID'] + "); return false;'>" + featureAttributes['ADDRESS'] + " " + featureAttributes['MUNI'] + "</a><br/>";
			}
			dojo.byId("lightbox").innerHTML = s;
		} else if (results.features.length == 1) { //query returns one match.
			//sets geometry variables for selected feature
			var selFeature = results.features[0];
			if (selFeature.geometry.type == "point") {
				var selExtent = new esri.geometry.Extent(selFeature.geometry.x - 250,selFeature.geometry.y - 250,selFeature.geometry.x + 250,selFeature.geometry.y + 250);
				var selX = selFeature.geometry.x;
				var selY = selFeature.geometry.y;
			} else {
				var selExtent = selFeature.geometry.getExtent();
				var selX = selExtent.xmin + ((selExtent.xmax - selExtent.xmin)/2);
				var selY = selExtent.ymin + ((selExtent.ymax - selExtent.ymin)/2);
			}
			//sets identify variables for selected parcel in order to perform drill-down identify
			identifyTask = new esri.tasks.IdentifyTask("http://" + hostName + "/ArcGIS/rest/services/BaseLayersQueryWebMercator/MapServer");
			identifyParams = new esri.tasks.IdentifyParameters();
			identifyParams.tolerance = 1;
			identifyParams.returnGeometry = true;
			identifyParams.layerIds = [2,3,4,5,6,7,8,9,10,11,12,13,14,15,16];
			identifyParams.layerOption = esri.tasks.IdentifyParameters.LAYER_OPTION_ALL;
			//triggers identify drill-down search for center of selected parcel
			identifyParams.geometry = new esri.geometry.Point(selX, selY, new esri.SpatialReference({ wkid: 102100 }));
			identifyParams.mapExtent = selExtent;
			identifyTask.execute(identifyParams, function(idResults) { addToMap(idResults); });
		}
	}
//END QUERY CODE
//**NOTE: THE IDENTIFY FUNCTIONALITY AND QUERY FUNCTIONALITY PERFORM VERY SIMILAR FUNCTIONS. THIS CODE COULD BE STREAMLINED AND IMPROVED IN THE FUTURE, BUT FOR THE SAKE OF TIME AND EFFORT SOME REDUNDANCIES EXIST IN THE CODE. -M. LAWTON 07/16/2009

//BEGIN IDENTIFY CODE -- this performs the 'drill-down' function once the coordinate location has been set by the address/parcel/intersection query
	//returns results for identify
	function addToMap(idResults) {
		var s = ""; //clears result set that populates searchInfo DIV
		layerresults2 = {displayFieldName:null,features:[]}; //Landrec Parcels result set
		layerresults3 = {displayFieldName:null,features:[]}; //Subdivision result set
		layerresults4 = {displayFieldName:null,features:[]}; //Planning Area result set
		//layerresults5 = {displayFieldName:null,features:[]}; //Planned Land Use result set -- not used anymore
		layerresults6 = {displayFieldName:null,features:[]}; //Citizen Advisory Board result set
		layerresults7 = {displayFieldName:null,features:[]}; //Elementary School result set
		layerresults8 = {displayFieldName:null,features:[]}; //Middle School result set
		layerresults9 = {displayFieldName:null,features:[]}; //High School result set
		layerresults10 = {displayFieldName:null,features:[]}; //Fire District result set
		layerresults11 = {displayFieldName:null,features:[]}; //Voter Precinct result set
		layerresults12 = {displayFieldName:null,features:[]}; //Zip Code result set
		layerresults13 = {displayFieldName:null,features:[]}; //Census Block result set
		layerresults14 = {displayFieldName:null,features:[]}; //Census Tract result set
		layerresults15 = {displayFieldName:null,features:[]}; //Public Land Survey result set
		layerresults16 = {displayFieldName:null,features:[]}; //Business License Area result set
		for (var i=0, il=idResults.length; i<il; i++) {
			var idResult = idResults[i];
			if (idResult.layerId == [2]) {
				layerresults2.features.push(idResult.feature); //pushes query for Landrec Parcels results
			}
			else if (idResult.layerId == [3]) {
				layerresults3.features.push(idResult.feature); //pushes query for Subdivision results
			}
			else if (idResult.layerId == [4]) {
				layerresults4.features.push(idResult.feature); //pushes query for Planning Area results
			}
			//Planned Land Use not used anymore
			//else if (idResult.layerId == [5]) {
			//	layerresults5.features.push(idResult.feature); //pushes query for Planned Land Use results
			//}
			else if (idResult.layerId == [6]) {
				layerresults6.features.push(idResult.feature); //pushes query for Citizen Advisory Board results
			}
			else if (idResult.layerId == [7]) {
				layerresults7.features.push(idResult.feature); //pushes query for Elementary School results
			}
			else if (idResult.layerId == [8]) {
				layerresults8.features.push(idResult.feature); //pushes query for Middle School results
			}
			else if (idResult.layerId == [9]) {
				layerresults9.features.push(idResult.feature); //pushes query for High School results
			}
			else if (idResult.layerId == [10]) {
				layerresults10.features.push(idResult.feature); //pushes query for Fire District results
			}
			else if (idResult.layerId == [11]) {
				layerresults11.features.push(idResult.feature); //pushes query for Voter Precinct results
			}
			else if (idResult.layerId == [12]) {
				layerresults12.features.push(idResult.feature); //pushes query for Zip Code results
			}
			else if (idResult.layerId == [13]) {
				layerresults13.features.push(idResult.feature); //pushes query for Census Block results
			}
			else if (idResult.layerId == [14]) {
				layerresults14.features.push(idResult.feature); //pushes query for Census Tract results
			}
			else if (idResult.layerId == [15]) {
				layerresults15.features.push(idResult.feature); //pushes query for Public Land Survey results
			}
			else if (idResult.layerId == [16]) {
				layerresults16.features.push(idResult.feature); //pushes query for Business License Area results
			}
		}
		if (layerresults2.features.length > 1) {
			s += layerTabContent(layerresults2,"layerresults2"); //If more than one parcel is found, do not perform full drill-down. Instead show multiple parcel list for user selection.
		} else {
			//If a single parcel is selected then perform full drill-down through all defined layers.
			s += '<h1 style="font-size: 2em; font-weight: normal; color: #486c8e;">Property Information</h1>';
			s += "<p>Click one of the links below to display information related to that section. Click on the heading again to close or another heading to view that sections' information.</p>";
			s += '<div class="prop_button">General Info</div>';
			s += '<div id="apn_general" class="prop_content" style="padding-top: 1em; padding-bottom: 1em;"><p>';
			s += layerTabContent(layerresults2,"layerresults2");
			s += layerTabContent(layerresults3,"layerresults3");
			s += layerTabContent(layerresults4,"layerresults4");
			s += layerTabContent(layerresults12,"layerresults12");
			s += layerTabContent(layerresults16,"layerresults16");
			//Planned Land Use not used anymore
			//s += layerTabContent(layerresults5,"layerresults5");
			s += layerTabContent(layerresults6,"layerresults6");
			s += "</p></div>";

			s += '<div class="prop_button">School Info</div>';
			s += '<div id="apn_schools" class="prop_content" style="padding-top: 1em; padding-bottom: 1em;"><p>';
			s += layerTabContent(layerresults7,"layerresults7");
			s += layerTabContent(layerresults8,"layerresults8");
			s += layerTabContent(layerresults9,"layerresults9");
			s += '</p></div>';

			s += '<div class="prop_button">District Info</div>';
			s += '<div id="apn_districts" class="prop_content" style="padding-top: 1em; padding-bottom: 1em;"><p>';
			s += layerTabContent(layerresults10,"layerresults10");
			s += layerTabContent(layerresults11,"layerresults11");
			if (layerTabContent(layerresults11,"layerresults11") == "") { //adds the message below if the location falls in between multiple voter precincts
				s += "<br/><strong>NOTE</strong>: THIS LOCATION MAY BORDER MULTIPLE VOTER PRECINCTS. REFINE YOUR SEARCH LOCATION TO DISPLAY VOTER PRECINCT INFORMATION."
			}
			s += '</p></div>';

		}
		s = '<div class="closer"><a href="#" class="closer_link" onclick="showPopup(\'\', \'\'); return false;"><img src="/images/close.jpg" alt="close this window" height="20" width="20" border="0" /></a></div><div class="grid_10">' + s + "</div>";

		jq("#lightbox").html(s);

		jq(".prop_button").click(function() {
		    jq(".prop_button").removeClass("on");

		    jq(".prop_content").slideUp();

		    if (jq(this).next().is(":hidden") == true) {
			jq(this).addClass("on");
			jq(this).next().slideDown();
		    }
		});

		jq(".prop_button").mouseover(function() {
		    jq(this).addClass("over");
		});

		jq(".prop_button").mouseout(function() {
		    jq(this).removeClass("over");
		});

		//dojo.byId("lightbox").innerHTML = s; //populates searchInfo DIV
	}

//formats display of content on the searchInfo DIV when performing an identify. -M. Lawton 06/01/2009
	function layerTabContent(layerResults, layerName) {
		var content = "";
		if (layerResults.features.length > 1 && layerName == "layerresults2") {
			if (layerResults.features.length == 500) {
				content += "Search stopped at 500 possible matches found.<br />";
			} else {
				content += layerResults.features.length + " possible matches found.<br />";
			}
			content += "<br/><i>Are you looking for...</><br/>";
			for (var i=0, il=layerResults.features.length; i<il; i++) {
				var featureAttributes = layerResults.features[i].attributes;
				content += "<a href='#' onclick='executeParcel(\"" + featureAttributes['PIN'] + "\"); return false;'><i>" + featureAttributes['STREETNUM'] + " " + featureAttributes['STREETDIR'] + " " + featureAttributes['STREET'] + " " + featureAttributes['CITY'] + "</i> (APN: " + featureAttributes['PIN'] + ")</a><i>?</i><br/>";
			}
		} else if (layerResults.features.length > 1 && layerName != "layerresults2") {
			content = "";
		} else if (layerResults.features.length == 1) {
			var featureAttributes = layerResults.features[0].attributes;
			//parcel layer fields to display on identify. -M. Lawton 06/01/2009
			if (layerName == "layerresults2") {
				var apnNoDashes = featureAttributes['PIN'].replace(/-/g,"");
				var dataString = '<div style="padding: 10px !important; margin-bottom: 5px !important; text-align: center !important;"><a href="' + layerResults.features[0].attributes['LINK'] + '">Parcel Map</a>&nbsp;|&nbsp;<a href="http://www.washoecounty.us/assessor/cama/search.php~parid=' + featureAttributes['PIN'] + '&Card=1&disclaimer=yes">Assessment</a>&nbsp;|&nbsp;<a href="https://etaxes.washoecounty.us/Tabs/TaxSearch.aspx?i=' + apnNoDashes + '">Tax Report</a><br/><a href="http://bldgpermits.washoecounty.us/index.php?searchType=parcel&terms=' + apnNoDashes + '"">Building Permits</a>&nbsp;|&nbsp;<a href="http://wcgisweb.washoecounty.us:8888/QuickMap/Index.htm?' + featureAttributes['PIN'] + '">Quick Map</a></div>';
				content+= dataString;
				content+="<p><strong>APN</strong>: "+layerResults.features[0].attributes['PIN']+"&nbsp;<a style='font-size: 10px;' href='http://wcgisweb.washoecounty.us:8888/QuickMap/Index.htm?" + featureAttributes['APN'] + "'>Click to zoom</a><br />";
				content+="<strong>First Name</strong>: "+layerResults.features[0].attributes['FIRSTNAME']+"<br />";
				content+="<strong>Last Name</strong>: "+layerResults.features[0].attributes['LASTNAME']+"<br />";
				content+="<strong>Address</strong>: "+layerResults.features[0].attributes['STREETNUM']+"<br />";
				content+="<strong>Dir</strong>: "+layerResults.features[0].attributes['STREETDIR']+"<br />";
				content+="<strong>Street</strong>: "+layerResults.features[0].attributes['STREET']+"<br />";
				content+="<strong>Area</strong>: "+layerResults.features[0].attributes['CITY']+"<br />";
				content+="<strong>Acres</strong>: "+layerResults.features[0].attributes['ACREAGE']+"<br />";
				content+="<strong>Bedrooms</strong>: "+layerResults.features[0].attributes['BEDROOMS']+"<br />";
				content+="<strong>Baths</strong>: "+layerResults.features[0].attributes['BATHS']+"<br />";
				content+="<strong>Year Built</strong>: "+layerResults.features[0].attributes['YEARBLT']+"<br />";
				content+="<strong>Zoning</strong>: "+layerResults.features[0].attributes['ZONING']+"<br />";
				content+="<strong>Tax District</strong>: "+layerResults.features[0].attributes['TAXDIST']+"<br />";
				content+="<strong>Taxable Value 2010/2011</strong>: "+layerResults.features[0].attributes['TOTALAPR']+"<br />";
				content+="<strong>Gross Tax Subject to Abatement</strong>: "+layerResults.features[0].attributes['grosstax']+"<br />";
				content+="<strong>Abated Tax</strong>: "+layerResults.features[0].attributes['abate']+"<br />";
				content+="<strong>Exempt Tax</strong>: "+layerResults.features[0].attributes['exempt']+"<br />";
				content+="<strong>Final Tax</strong>: "+layerResults.features[0].attributes['nettax']+"<br />";
			}
			if (layerName == "layerresults3") {
				content+="<strong>Subdivision</strong>: " +layerResults.features[0].attributes['NAME']+ " <a style='font-size: 10px;' href='"+layerResults.features[0].attributes['MAP_LINK']+"'>Tract Map</a><br />";
				content+="<strong>Location</strong>: "+layerResults.features[0].attributes['LOCATION']+"<br />";
			}
			if (layerName == "layerresults4") {
				content+="<strong>Planning Area</strong>: "+layerResults.features[0].attributes['PLANAREA_N']+"<br />";
			}
			//if (layerName == "layerresults5") {
			//	content+="<p>Planned Land Use: "+layerResults.features[0].attributes['PLUDESCRIPTION']+"</p>";
			//}
			if (layerName == "layerresults6") {
				content+="<strong>Citizen Advisory Board</strong>: "+layerResults.features[0].attributes['CAB_NAME']+"<br />";
			}
			if (layerName == "layerresults7") {
				content+="<strong>Elementary School</strong>: "+layerResults.features[0].attributes['NAME']+"<br />";
			}
			if (layerName == "layerresults8") {
				content+="<strong>Middle School</strong>: "+layerResults.features[0].attributes['NAME']+"<br />";
			}
			if (layerName == "layerresults9") {
				content+="<strong>High School</strong>: "+layerResults.features[0].attributes['NAME']+"<br />";
			}
			if (layerName == "layerresults10") {
				content+="<strong>Fire District</strong>: "+layerResults.features[0].attributes['District']+"<br />";
			}
			if (layerName == "layerresults11") {
				var dataStringPrecinct = '<a style="font-size: 10px;" href="ftp://wcftp.washoecounty.us/outtoworld/voters/Precincts/prec_' + layerResults.features[0].attributes['PRECINCT'] + '.pdf">Precinct Map</a>';
				content+="<strong>Voter Precinct</strong>: "+layerResults.features[0].attributes['PRECINCT']+" " + dataStringPrecinct + "<br />";
				content+="<strong>Senate District</strong>: "+layerResults.features[0].attributes['SENATE']+"<br />";
				content+="<strong>Assembly District</strong>: "+layerResults.features[0].attributes['ASSEMBLY']+"<br />";
				content+="<strong>Board of Education</strong>: "+layerResults.features[0].attributes['BOARDOFEDU']+"<br />";
				content+="<strong>University Regents</strong>: "+layerResults.features[0].attributes['REGENTS']+"<br />";
				content+="<strong>County Commission District</strong>: "+layerResults.features[0].attributes['COMMISSION']+"<br />";
				content+="<strong>Reno Ward</strong>: "+layerResults.features[0].attributes['RWARDS']+"<br />";
				content+="<strong>Sparks Ward</strong>: "+layerResults.features[0].attributes['SWARDS']+"<br />";
				content+="<strong>Standard Trustee</strong>: "+layerResults.features[0].attributes['STDTRUST']+"<br />";
				content+="<strong>General Trustee</strong>: "+layerResults.features[0].attributes['GENTRUST']+"<br />";
				content+="<strong>General Improvement District</strong>: "+layerResults.features[0].attributes['GID']+"<br />";
				content+="<strong>Township</strong>: "+layerResults.features[0].attributes['TOWNSHIP']+"<br />";
			}
			if (layerName == "layerresults12") {
				content+="<strong>Zip Code</strong>: "+layerResults.features[0].attributes['ZIPCODE']+"<br />";
			}
			if (layerName == "layerresults13") {
				content+="<strong>Census Block</strong>: "+layerResults.features[0].attributes['BLOCK']+"<br />";
			}
			if (layerName == "layerresults14") {
				var dataStringCensus = '<a style="font-size: 10px;" href="ftp://wcftp.co.washoe.nv.us/outtoworld/census/Tracts/Tract%20' + layerResults.features[0].attributes['TRACT'] + '.pdf">Census Map</a><br />';
				content+="<strong>Census Tract</strong>: "+layerResults.features[0].attributes['TRACT']+" " + dataStringCensus + "<br />";
			}
			if (layerName == "layerresults15") {
				content+="<strong>Public Land Survey</strong>: "+layerResults.features[0].attributes['TR']+"<br />";
			}
			if (layerName == "layerresults16") {
				content+="<strong>Business License Area</strong>: "+layerResults.features[0].attributes['LICAREA_NA']+"<br />";
			}
		}
		return content;
	}
//END IDENTIFY CODE

function preload() {
	document.getElementById('lightbox').innerHTML = "<div class=\"grid_10 center small\">Please wait while we retrieve your data...<br /><img src=\"../images/loading.gif\" width=\"200\" height=\"20\" alt=\"Data is loading.  Please wait.\"></div>";
	var displayme = document.getElementById('lightbox').style;
	var maskme = document.getElementById('mask').style;
	maskme.display="block";
	displayme.display="block";
}

Init();
