






function expQueryDetails(evt){
	map.setMapCursor("wait");
	map.infoWindow.hide();
	foundResults = false;
	var identifyTask = new esri.tasks.IdentifyTask(ip + "/ArcGIS/rest/services/HarrisCounty/Harris_Exploration/MapServer");
	
	var identifyParams = new esri.tasks.IdentifyParameters();
	identifyParams.tolerance = 2;
	identifyParams.returnGeometry = false;
	identifyParams.layerIds = expVisible;
	
	identifyParams.layerOption = esri.tasks.IdentifyParameters.LAYER_OPTION_ALL;
	
	map.infoWindow.setTitle("Results");
	
	var symbol = new esri.symbol.SimpleFillSymbol(esri.symbol.SimpleFillSymbol.STYLE_SOLID, new esri.symbol.SimpleLineSymbol(esri.symbol.SimpleLineSymbol.STYLE_SOLID, new dojo.Color([255, 0, 0]), 2), new dojo.Color([255, 255, 0, 0.5]));
	
	identifyParams.geometry = evt.mapPoint;
	identifyParams.mapExtent = map.extent;
	
	expStartTable();
	
		identifyTask.execute(identifyParams, function(idResults){
			
			expAddToMap(idResults, evt);
			
			expQueryPipe(evt);
			
			
		}, function(){
			expQueryPipe(evt);
			
		});

	
		
	
}

function expQueryPipe(evt){
	if (expPipe == true) {
		var expidentifyPipeTask = new esri.tasks.IdentifyTask(ip + "/ArcGIS/rest/services/HarrisCounty/Harris_ExpPipelines/MapServer");
		
		var expidentifyPipeParams = new esri.tasks.IdentifyParameters();
		
		expidentifyPipeParams.tolerance = 1;
		
		expidentifyPipeParams.returnGeometry = false;
		
		expidentifyPipeParams.layerIds = expPipeVisible;
		
		var pipesymbol = new esri.symbol.SimpleFillSymbol(esri.symbol.SimpleFillSymbol.STYLE_SOLID, new esri.symbol.SimpleLineSymbol(esri.symbol.SimpleLineSymbol.STYLE_SOLID, new dojo.Color([255, 0, 0]), 2), new dojo.Color([255, 255, 0, 0.5]));
		
		expidentifyPipeParams.geometry = evt.mapPoint;
		expidentifyPipeParams.mapExtent = map.extent;
		expidentifyPipeTask.execute(expidentifyPipeParams, function(idResults){
			expAddToMapPipe(idResults, evt);
			expEndTable(evt);
			
		});
	} else {
		expEndTable(evt);
	}
}

function expStartTable(){
			expInfoHTML = "";
			dijit.byId('infoWindow').setContent('');
			map.infoWindow.setContent('');
			
			expInfoHTML = "<table><tr><td><strong>Name</strong></td><td><strong>Type</strong></td><td></td></tr>";
			
	
}
function expEndTable(evt){
			
			expInfoHTML += "</table>";
			dijit.byId('infoWindow').setContent(expInfoHTML);
			map.infoWindow.setContent(dijit.byId('infoWindow').domNode);
			map.setMapCursor("default");
			if (foundResults == true) {
				
				map.infoWindow.resize(300, 200);
				map.infoWindow.show(evt.screenPoint, map.getInfoWindowAnchor(evt.screenPoint));
				
			}
	
}
function expAddToMap(idResults, evt) {	
		
		if (idResults.length > 0) {
			foundResults = true;
			var layer1results = {
				displayFieldName: null,
				features: []
			};
			var layer2results = {
				displayFieldName: null,
				features: []
			};
			var layer3results = {
				displayFieldName: null,
				features: []
			};
			
			
			
			for (var i = 0, il = idResults.length; i < il; i++) {
				var idResult = idResults[i];
				if (idResult.layerId === 0) {  //Wells
				
						expInfoHTML += "<tr><td>" + idResult.feature.attributes['OPERATOR'] + "</td><td>Well</td><td>";
						
						
						
						
						var wellDetails = '<script type=\"dojo/method\" event=\"onClick\"> dojo.byId(\"expDetail\").innerHTML = \'<br><strong>' + idResult.feature.attributes['OPERATOR'] + '</strong><br><br>';
						wellDetails += '<br><br><div dojoType=\"dijit.TitlePane\" title = \"Info\" selected=\"true\">Operator: ' + idResult.feature.attributes['OPERATOR'] + '<br>Lease: ' + idResult.feature.attributes['LEASE'] + '<br>Field: ' + idResult.feature.attributes['FIELD'] + '</div><div dojoType=\"dijit.TitlePane\" title = \"Well Detail\">Type: '; 
						switch (idResult.feature.attributes['WELLCLAS']) {
							case 'D&A':
								wellDetails += 'Dry and Abandoned';
								break;
							case 'OIL':
								wellDetails += 'Oil Well';
								break;
							case 'GAS':
								wellDetails += 'Gas Well';
								break;
							case 'AGW':
								wellDetails += 'Abandoned Gas Well';
								break;
							case 'AOW':
								wellDetails += 'Abandoned Oil Well';
								break;
							case 'INJ':
								wellDetails += 'Injection Well';
								break;
							case 'J&A':
								wellDetails += 'Junked and Abandoned';
								break;
							case 'LOC':
								wellDetails += 'Permit';
								break;
							case 'M-G':
								wellDetails += 'Multiple Gas Wells';
								break;
							case 'M-O':
								wellDetails += 'Multiple Oil Wells';
								break;
							case 'MOG':
								wellDetails += 'Multiple Oil and Gass Wells';
								break;
							case 'P&A':
								wellDetails += 'Plugged and Abandoned';
								break;
							case 'SER':
								wellDetails += 'Service Well';
								break;
							case 'SUS':
								wellDetails += 'Suspended Well';
								break;
							case 'T&A':
								wellDetails += 'Temporarily Abandoned';
								break;
							default:
								wellDetails += 'Unknown - ' + idResult.feature.attributes['WELLCLAS'];
														
						}
						wellDetails += '<br>Well Number: '+ idResult.feature.attributes['WELLNO'] + '<br>Total Depth: ' + idResult.feature.attributes['TOTDEPTH'] + '</div>';
						wellDetails += '\';dijit.byId("Exploration").selectChild(dijit.byId("expDetail")); dojo.parser.parse(dojo.byId("expDetail"));;</script>';
						expInfoHTML += "<button type='button' dojoType='dijit.form.Button' align='right' >Details " + wellDetails + " </button>";
						expInfoHTML +="</td></tr>";
					
				}
				else 
					if (idResult.layerId === 1) {  //Refinery
						
						expInfoHTML += "<tr><td>" + idResult.feature.attributes['Name'] + "</td><td>Refinery</td><td>";
							var refDetails = '<script type=\"dojo/method\" event=\"onClick\"> dojo.byId(\"expDetail\").innerHTML = \'<br><strong>' + idResult.feature.attributes['Name'] + '</strong><br><br>';
								
							if (idResult.feature.attributes['Misc'] != "") {
								refDetails += '<a href=\"' + idResult.feature.attributes['Misc'] + '\" target=\"_blank\">Refinery Website</a>';
							}
							refDetails += '<br><br><div dojoType=\"dijit.TitlePane\" title = \"Address\" selected=\"true\">' + idResult.feature.attributes['Name'] + '<br>' + idResult.feature.attributes['Address'] + '<br>' + idResult.feature.attributes['City'] + ', ' + idResult.feature.attributes['State'] + ' ' + idResult.feature.attributes['Zipcode'] + '</div><div dojoType=\"dijit.TitlePane\" title = \"Capacity\">'; 
							refDetails += idResult.feature.attributes['Capacity'] + ' barrels per day</div>';
							refDetails += '\';dijit.byId("Exploration").selectChild(dijit.byId("expDetail")); dojo.parser.parse(dojo.byId("expDetail"));;</script>';
							expInfoHTML += "<button type='button' dojoType='dijit.form.Button' align='right' >Details " + refDetails + " </button>";
							expInfoHTML +="</td></tr>";
							
					}
					else 
						if (idResult.layerId === 2) {  //Oil Fields
							expInfoHTML += "<tr><td>" + idResult.feature.attributes['FEATURE_ST'] + "</td><td>Oil Field</td><td>";
							var fieldDetails = '<script type=\"dojo/method\" event=\"onClick\"> dojo.byId(\"expDetail\").innerHTML = \'<br><strong>' + idResult.feature.attributes['FEATURE_ST'] + '</strong><br><br>';
								
							fieldDetails += '<br><br><div dojoType=\"dijit.TitlePane\" title = \"Details\" selected=\"true\">Name:' + idResult.feature.attributes['FEATURE_ST'] + '</div>'; 
							
							fieldDetails += '\';dijit.byId("Exploration").selectChild(dijit.byId("expDetail")); dojo.parser.parse(dojo.byId("expDetail"));;</script>';
							expInfoHTML += "<button type='button' dojoType='dijit.form.Button' align='right' >Details " + fieldDetails + " </button>";
							expInfoHTML +="</td></tr>";
						}
						
			}
			
	}
	
}

function expAddToMapPipe(pipeIdResults, evt) {	
		
		if (pipeIdResults.length >0) {
			foundResults = true;
			var pipeName = [];
			var pipeSys = [];
				for (var i = 0, il = pipeIdResults.length; i < il; i++) {
					if (!pipelineContained(pipeName,pipeSys,pipeIdResults[i].feature.attributes['OWNER'],pipeIdResults[i].feature.attributes['SYS_NM'])) {
						expInfoHTML += "<tr><td>" + pipeIdResults[i].feature.attributes['OWNER'] + "</td><td>Pipeline</td><td>";
						var pipeDetails = '<script type=\"dojo/method\" event=\"onClick\"> dojo.byId(\"expDetail\").innerHTML = \'<br><strong>' + pipeIdResults[i].feature.attributes['OWNER'] + '</strong><br><br>';
						
						pipeDetails += '<br><br><div dojoType=\"dijit.TitlePane\" title = \"Info\" selected=\"true\">Owner: ' + pipeIdResults[i].feature.attributes['OWNER'] + '<br>Operator: ' + pipeIdResults[i].feature.attributes['OPR_NM'] + '<br>System Name: ' + pipeIdResults[i].feature.attributes['SYS_NM'] + '<br>Subsystem Name: ' + pipeIdResults[i].feature.attributes['SUBSYSNM'] + '</div><div dojoType=\"dijit.TitlePane\" title = \"Pipeline Detail\">Commodity: '; 
						switch (pipeIdResults[i].feature.attributes['CMDTY']) {
							case 'HG':
								pipeDetails += 'Hydrogen Gas';
								break;
							case 'CRD':
								pipeDetails += 'Crude Oil';
								break;
							case 'LPG':
								pipeDetails += 'Liquified Petroleum Gas';
								break;
							case 'NG':
								pipeDetails += 'Natural Gas';
								break;
							case 'PRD':
								pipeDetails += 'Product';
								break;
							case 'AA':
								pipeDetails += 'Anhydrous Ammonia';
								break;
							case 'CO2':
								pipeDetails += 'Carbon Dioxide';
								break;
							case 'NGL':
								pipeDetails += 'Natural Gas Liquids';
								break;
							case 'HVL':
								pipeDetails += 'Hihgly Volatile Liquid';
								break;
							case 'EMT':
								pipeDetails += 'Empty';
								break;
							case 'Refine':
								pipeDetails += 'Product';
								break;
							default:
								pipeDetails += 'Unknown';
														
						}
						
						pipeDetails += '<br>Pipeline System Type: ';
						
						switch(pipeIdResults[i].feature.attributes['SYSTYPE']){
							case 'K':
								pipeDetails+= 'Carbon Dioxide Line';
								break;
							case 'L':
								pipeDetails+= 'Crude Gathering Line';
								break;
							case 'O':
								pipeDetails+= 'Crude Transmission Line';
								break;
							case 'P':
								pipeDetails+= 'Product Line (Not Highly Volatile)';
								break;
							case 'Q':
								pipeDetails+= 'Other Product Line (Highly Volatile)';
								break;
							case 'T':
								pipeDetails+= 'Gas Transmission Line';
								break;
							case 'G':
								pipeDetails+= 'Gas Gathering Line';
								break;
							default:
								pipeDetails += 'Unknown';
							
						}
						pipeDetails += '<br>Pipeline Diameter (inches): '+ pipeIdResults[i].feature.attributes['DIA_IN'] + '</div>';
						pipeDetails += '\';dijit.byId("Exploration").selectChild(dijit.byId("expDetail")); dojo.parser.parse(dojo.byId("expDetail"));;</script>';
						expInfoHTML += "<button type='button' dojoType='dijit.form.Button' align='right' >Details " + pipeDetails + " </button>";
						expInfoHTML +="</td></tr>";
						
						
						pipeName[i] = pipeIdResults[i].feature.attributes['OWNER'];
						pipeSys[i] = pipeIdResults[i].feature.attributes['SYS_NM'];
					} else {
						pipeName[i] = "";
						pipeSys[i] = "";
					}
					
				}
			
			
		}
	
}

function pipelineContained (pipeName,pipeSys,newpipename,newpipesys){
	for (var i = 0;i<pipeName.length;i++){
		if ((pipeName[i] == newpipename) && (pipeSys[i] == newpipesys)) {
			return true;
		}
	}
	return false;
	
}

function expRefineryTable(featureSet) {
	
						var html = "";
						for(var h=0; h<featureSet.features.length; h++) {
							var reffeature = featureSet.features[h];
							var refDetails = '<script type=\"dojo/method\" event=\"onClick\"> dojo.byId(\"expDetail\").innerHTML = \'<br><strong>' + reffeature.attributes.Name + '</strong><br><br>';
								
							if (reffeature.attributes.Misc != "") {
								refDetails += '<a href=\"' + reffeature.attributes.Misc + '\" target=\"_blank\">Refinery Website</a>';
							}
							refDetails += '<br><br><div dojoType=\"dijit.TitlePane\" title = \"Address\" selected=\"true\">' + reffeature.attributes.Name + '<br>' + reffeature.attributes.Address + '<br>' + reffeature.attributes.City + ', ' + reffeature.attributes.State + ' ' + reffeature.attributes.Zipcode + '</div><div dojoType=\"dijit.TitlePane\" title = \"Capacity\">'; 
							refDetails += reffeature.attributes.Capacity + ' barrels per day</div>';
							refDetails += '\';dijit.byId("Exploration").selectChild(dijit.byId("expDetail")); dojo.parser.parse(dojo.byId("expDetail"));;</script>';
								
							html+= "<table width=260 id='exprefRow" + h + "'><tr><td style='cursor:pointer;' onClick=\'showMyExpRefInfoWindow(\"" + reffeature.attributes.Name + "\", \"Refinery\", " + reffeature.geometry.x + "," + reffeature.geometry.y + ", " + h + " )\'> <strong>" + reffeature.attributes.Name + "</strong><br>" + reffeature.attributes.Address + "<br>" + reffeature.attributes.City + ", " + reffeature.attributes.State + " " + reffeature.attributes.Zipcode + "</td><td align = 'right'><button type='button' dojoType='dijit.form.Button' align='right' >Details" + refDetails + "</button></td></tr></table>"; 
						}
						dojo.byId('expRefAcc').innerHTML = html;
						
						dojo.parser.parse('expRefAcc');
}
function fieldTable(featureSet) {
	
						var html = "";
						for(var h=0; h<featureSet.features.length; h++) {
							var fieldfeature = featureSet.features[h];
							var fieldDetails = '<script type=\"dojo/method\" event=\"onClick\"> dojo.byId(\"expDetail\").innerHTML = \'<br><strong>' + fieldfeature.attributes.FEATURE_ST + '</strong><br><br>';
								
						
								fieldDetails += '<br><br><div dojoType=\"dijit.TitlePane\" title = \"Details\" selected=\"true\">Name:' + fieldfeature.attributes.FEATURE_ST + '</div>'; 
								
								fieldDetails += '\';dijit.byId("Exploration").selectChild(dijit.byId("expDetail")); dojo.parser.parse(dojo.byId("expDetail"));;</script>';
								
							html+= "<table width=260 id='expfieldRow" + h + "'><tr><td style='cursor:pointer;' onClick=\'showMyFieldInfoWindow(\"" + fieldfeature.attributes.FEATURE_ST + "\", \"Oil Field\", " + fieldfeature.geometry.x + "," + fieldfeature.geometry.y + ", " + h + " )\'> <strong>" + fieldfeature.attributes.FEATURE_ST + "</strong><br></td><td align = 'right'><button type='button' dojoType='dijit.form.Button' align='right' >Details" + fieldDetails + "</button></td></tr></table>"; 
						}
						dojo.byId('expFieldAcc').innerHTML = html;
						
						dojo.parser.parse('expFieldAcc');
}
function wellTable(featureSet) {
	
						var html = "";
						for(var h=0; h<featureSet.features.length; h++) {
							var wellfeature = featureSet.features[h];
							var wellDetails = '<script type=\"dojo/method\" event=\"onClick\"> dojo.byId(\"expDetail\").innerHTML = \'<br><strong>' + wellfeature.attributes.OPERATOR + '</strong><br><br>';
						
						wellDetails += '<br><br><div dojoType=\"dijit.TitlePane\" title = \"Info\" selected=\"true\">' + wellfeature.attributes.OPERATOR + '<br>Lease:' + wellfeature.attributes.LEASE + '<br>Field:' + wellfeature.attributes.FIELD + '</div><div dojoType=\"dijit.TitlePane\" title = \"Well Detail\">Type: ';
						
						switch (wellfeature.attributes.WELLCLAS) {
							case 'D&A':
								wellDetails += 'Dry and Abandoned';
								break;
							case 'OIL':
								wellDetails += 'Oil Well';
								break;
							case 'GAS':
								wellDetails += 'Gas Well';
								break;
							case 'AGW':
								wellDetails += 'Abandoned Gas Well';
								break;
							case 'AOW':
								wellDetails += 'Abandoned Oil Well';
								break;
							case 'INJ':
								wellDetails += 'Injection Well';
								break;
							case 'J&A':
								wellDetails += 'Junked and Abandoned';
								break;
							case 'LOC':
								wellDetails += 'Permit';
								break;
							case 'M-G':
								wellDetails += 'Multiple Gas Wells';
								break;
							case 'M-O':
								wellDetails += 'Multiple Oil Wells';
								break;
							case 'MOG':
								wellDetails += 'Multiple Oil and Gass Wells';
								break;
							case 'P&A':
								wellDetails += 'Plugged and Abandoned';
								break;
							case 'SER':
								wellDetails += 'Service Well';
								break;
							case 'SUS':
								wellDetails += 'Suspended Well';
								break;
							case 'T&A':
								wellDetails += 'Temporarily Abandoned';
								break;
							default:
								wellDetails += 'Unknown';
														
						}
						wellDetails += '<br>Well Number: '+ wellfeature.attributes.WELLNO + '<br>Total Depth: ' + wellfeature.attributes.TOTDEPTH + '</div>';
						wellDetails += '\';dijit.byId("Exploration").selectChild(dijit.byId("expDetail")); dojo.parser.parse(dojo.byId("expDetail"));;</script>';
								
							html+= "<table width=260 id='expwellRow" + h + "'><tr><td style='cursor:pointer;' onClick=\'showMyWellInfoWindow(\"" + wellfeature.attributes.OPERATOR + "\", \"Well\", " + wellfeature.geometry.x + "," + wellfeature.geometry.y + ", " + h + " )\'> <strong>" + wellfeature.attributes.OPERATOR + "</strong><br></td><td align = 'right'><button type='button' dojoType='dijit.form.Button' align='right' >Details" + wellDetails + "</button></td></tr></table>"; 
						}
						dojo.byId('expWellAcc').innerHTML = html;
						
						dojo.parser.parse('expWellAcc');
}
	


 
function showMyExpRefInfoWindow(name,type,x,y,num) {
	if (explastclick != "") {
		dojo.byId(explastclick).style.background = "#FFF";
	}
	dojo.byId('exprefRow' + num).style.background = "#F2F2F2";
	explastclick = 'exprefRow' + num;
	var myrefcontent = "<strong>" + name + "</strong>";
	var myPoint = new esri.geometry.Point(x, y, new esri.SpatialReference({ wkid: 4326 }));
	var screenPoint = map.toScreen(myPoint);
	map.infoWindow.setContent(myrefcontent);
	map.infoWindow.setTitle(type);
	
	map.infoWindow.resize(150, 50);
	
	map.infoWindow.show(screenPoint,map.getInfoWindowAnchor(screenPoint));
	
}
       
function showMyFieldInfoWindow(name,type,x,y,num) {
	if (explastclick != "") {
		dojo.byId(explastclick).style.background = "#FFF";
	}
	dojo.byId('expfieldRow' + num).style.background = "#F2F2F2";
	explastclick = 'expfieldRow' + num;
	var myfieldcontent = "<strong>" + name + "</strong>";
	var myPoint = new esri.geometry.Point(x, y, new esri.SpatialReference({ wkid: 4326 }));
	var screenPoint = map.toScreen(myPoint);
	map.infoWindow.setContent(myfieldcontent);
	map.infoWindow.setTitle(type);
	
	map.infoWindow.resize(150, 50);
	
	map.infoWindow.show(screenPoint,map.getInfoWindowAnchor(screenPoint));
	
}
function showMyAllFieldInfoWindow(name,type,x,y,num) {
	if (alllastclick != "") {
		dojo.byId(alllastclick).style.background = "#FFF";
	}
	dojo.byId('allfieldRow' + num).style.background = "#F2F2F2";
	alllastclick = 'allfieldRow' + num;
	var myfieldcontent = "<strong>" + name + "</strong>";
	var myPoint = new esri.geometry.Point(x, y, new esri.SpatialReference({ wkid: 4326 }));
	var screenPoint = map.toScreen(myPoint);
	map.infoWindow.setContent(myfieldcontent);
	map.infoWindow.setTitle(type);
	
	map.infoWindow.resize(150, 50);
	
	map.infoWindow.show(screenPoint,map.getInfoWindowAnchor(screenPoint));
	
}

function showMyWellInfoWindow(name,type,x,y,num) {
	if (explastclick != "") {
		dojo.byId(explastclick).style.background = "#FFF";
	}
	dojo.byId('expwellRow' + num).style.background = "#F2F2F2";
	explastclick = 'expwellRow' + num;
	var mywellcontent = "<strong>" + name + "</strong>";
	var myPoint = new esri.geometry.Point(x, y, new esri.SpatialReference({ wkid: 4326 }));
	var screenPoint = map.toScreen(myPoint);
	map.infoWindow.setContent(mywellcontent);
	map.infoWindow.setTitle(type);
	
	map.infoWindow.resize(150, 50);
	
	map.infoWindow.show(screenPoint,map.getInfoWindowAnchor(screenPoint));
	
} 
function showMyAllWellInfoWindow(name,type,x,y,num) {
	if (alllastclick != "") {
		dojo.byId(alllastclick).style.background = "#FFF";
	}
	dojo.byId('allwellRow' + num).style.background = "#F2F2F2";
	alllastclick = 'allwellRow' + num;
	var mywellcontent = "<strong>" + name + "</strong>";
	var myPoint = new esri.geometry.Point(x, y, new esri.SpatialReference({ wkid: 4326 }));
	var screenPoint = map.toScreen(myPoint);
	map.infoWindow.setContent(mywellcontent);
	map.infoWindow.setTitle(type);
	
	map.infoWindow.resize(150, 50);
	
	map.infoWindow.show(screenPoint,map.getInfoWindowAnchor(screenPoint));
	
} 
