﻿
//ensures only one identify objects needs to be created for entire website
if(gDojoIdentifyTask){}	
else {var gDojoIdentifyTask = dojo.require("esri.tasks.identify");}

if(gMapToLocalVar){}
else {var gMapToLocalVar;}

var identifyTask, identifyParams;
//used to store contents of XML document
var gIdentifyURL = new Array();
var gIdentifyLayer = new Array();
var gIdentifyField = new Array();
var gPointX;
var gPointY;

function parseWSMFeaturesXML()
{
    gMapToLocalVar = myMap; //tranfers map name from main to local page variable in case map name changes, just change this one spot.


    //gMapToLocalVar.graphics.clear();
    dojo.disconnect(vMapEvents);
    dojo.disconnect(vMapClickEvent);
    dojo.byId("WidgetResults").innerHTML = "&nbsp;";
    dojo.byId("tdHelpTip").innerHTML = "Help: Click on Map to identify things in the watershed";

try //Internet Explorer
  {xmlDoc=new ActiveXObject("Microsoft.XMLDOM");}
catch(e)
  {
  try //Firefox, Mozilla, Opera, etc.
    {xmlDoc=document.implementation.createDocument("","",null);}
  catch(e)
    {
    alert(e.message);
    return;
    }
  }

xmlDoc.async=false;
xmlDoc.load("javascript/widget_WSM_Features1/config.xml");
//fFileLocation = (xmlDoc.url).substr(8);
x=xmlDoc.getElementsByTagName('identifyURL');
for (i=0;i<x.length;i++)
{gIdentifyURL[i] = x[i].childNodes[0].nodeValue;}

x=xmlDoc.getElementsByTagName('identifyLayer');
for (i=0;i<x.length;i++)
{gIdentifyLayer[i] = x[i].childNodes[0].nodeValue;}

x=xmlDoc.getElementsByTagName('identifyField');
for (i=0;i<x.length;i++)
{gIdentifyField[i] = x[i].childNodes[0].nodeValue;}

InitIdentify();

}


function InitIdentify() {
	
	vMapClickEvent = dojo.connect(gMapToLocalVar, "onClick", doIdentify);

	identifyTask = new esri.tasks.IdentifyTask(gIdentifyURL[0]);
	identifyParams = new esri.tasks.IdentifyParameters();
	identifyParams.tolerance = 3;
	identifyParams.returnGeometry = false;
	identifyParams.layerIds = [gIdentifyLayer[0]];
	identifyParams.layerOption = esri.tasks.IdentifyParameters.LAYER_OPTION_ALL;
}

function doIdentify(evt) {

	gMapToLocalVar.graphics.clear();
	
	var symbol = new esri.symbol.SimpleMarkerSymbol();
        symbol.setStyle(esri.symbol.SimpleMarkerSymbol.STYLE_DIAMOND);
        symbol.setColor(new dojo.Color([255,0,0,0.75]));
    var graphic = new esri.Graphic(evt.mapPoint, symbol);
    gMapToLocalVar.graphics.add(graphic);		
 	
	gPointX = evt.mapPoint.x;
	gPointY = evt.mapPoint.y;
	identifyParams.geometry = evt.mapPoint;
	identifyParams.mapExtent = gMapToLocalVar.extent;
	identifyTask.execute(identifyParams, function(IdentifyResult) { ShowResults(IdentifyResult); });
}


function ShowResults(pIdentifyResult) {
    var dupeHolder = "";
	var ShowAtt = new Array();
	var AllFields = gIdentifyField[0].split(":");
	var AttribList = "";
	for(var i=0; i<pIdentifyResult.length; i++)
	{
		ShowAtt[i] = pIdentifyResult[i].feature;
		for(var z=0; z<AllFields.length; z++)
		{
			if(AllFields[z].indexOf(",") > 0)
			{
				var indField = AllFields[z].split(",");
				for(var x=0; x<indField.length; x++)
				{
					if(ShowAtt[i].attributes[indField[x]])
					{
					
					 //alert(ShowAtt[i].attributes[indField[x]]);
						if(indField[x] == "Track_No")
						{
							AttribList = AttribList + "Appeal/Protest: <a href='http://cip.sara-tx.org/Website/cip/" + ShowAtt[i].attributes[indField[x+1]] + "' target='_blank' style='color:white'>" + ShowAtt[i].attributes[indField[x]] + "</a><br>";
							x = x + 1;
						}

                        if (indField[x] == "RIVERCODE")
						{						
						    if (dupeHolder != ShowAtt[i].attributes[indField[x]]){
                                AttribList = AttribList + "Stream: <a onclick=\"DownloadWindow('" + ShowAtt[i].attributes[indField[x]] + "','" + ShowAtt[i].attributes[indField[x+3]] + "');\" style='cursor:pointer; text-decoration:underline;'>" + ShowAtt[i].attributes[indField[x]] + "</a><br>";
						        AttribList = AttribList + "Study Type: " + ShowAtt[i].attributes[indField[x+2]] + "<br>";
						        AttribList = AttribList + "County: " + ShowAtt[i].attributes[indField[x+3]] + "<br>";				    

							    dupeHolder = ShowAtt[i].attributes[indField[x]];
							    x = x + 2;
							}
							else
							{
							//AttribList = AttribList + indField[x] + ": " + ShowAtt[i].attributes[indField[x]] + "<br>";
							}						
						}
						else
						{
							//AttribList = AttribList + indField[x] + ": " + ShowAtt[i].attributes[indField[x]] + "<br>";
						}
					}
				}
			}
			else
			{
				if(ShowAtt[i].attributes[AllFields[z]])
				{
					if (AllFields[z] == "NAME")
					{
					    AttribList = AttribList + "Low Water Crossing: <a onclick='WSMFeatures_RequestStreetView();' style='cursor:pointer; color:white; text-decoration:underline;'>" + ShowAtt[i].attributes[AllFields[z]] + "</a><br>";					    
					}
					else if (AllFields[z] == "WS_NAME")
					{
					    if (ShowAtt[i].attributes[AllFields[z]] == "Leon Creek Watershed" || ShowAtt[i].attributes[AllFields[z]] == "Salado Creek Watershed" || ShowAtt[i].attributes[AllFields[z]] == "San Antonio River Watershed")
						AttribList = AttribList + "Watershed: <a href='javascript/widget_WSM_Features1/WS_facts/" + ShowAtt[i].attributes[AllFields[z]] + ".jpg' target='_blank' style='color:white;'>" + ShowAtt[i].attributes[AllFields[z]] + "</a><br>";
					    else
					    AttribList = AttribList + "Watershed: " + ShowAtt[i].attributes[AllFields[z]] + "<br>";
					}
					else if (AllFields[z] == "PROJECTID")
					{
						AttribList = AttribList + "CIP ID: <a href='http://gis.sara-tx.org/website/cip_pdf/CIP_" + ShowAtt[i].attributes[AllFields[z]] + ".pdf' target='_blank' style='color:white;'>" + ShowAtt[i].attributes[AllFields[z]] + "</a><br>";				
					}
					/*
					Added connection to FEMA map panels in Bexar County
					jrefolo
					10/6/2010

                    FEMA panels for all downstream counties added
                    jrefolo
                    1/24/2011
					*/
					else if (AllFields[z] == "FIRM_PAN")
					{	
						AttribList = AttribList + "FEMA Panel: <a href='javascript/widget_WSM_Features1/fema_panels/" + ShowAtt[i].attributes[AllFields[z]] + ".pdf' target='_blank' style='color:white;'>" + ShowAtt[i].attributes[AllFields[z]] + "</a><br>";										
					}

		            /*
		            Added connection to LOMC Resolution documents produced by Joe (not official FEMA documents)
		            jrefolo
		            1/25/2011
		            */

		            else if (AllFields[z] == "Track_No") {
		                AttribList = AttribList + "LOMC: <a href='javascript/widget_WSM_Features1/lomc_resolution/" + ShowAtt[i].attributes[AllFields[z]] + "_Summary.pdf' target='_blank' style='color:white;'>" + ShowAtt[i].attributes[AllFields[z]] + "</a><br>";
		            }

		            else if (AllFields[z] == "PDF") {
		                AttribList = AttribList + "LOMR: <a href='javascript/widget_WSM_Features1/lomr_revalidation/" + ShowAtt[i].attributes[AllFields[z]] + ".pdf' target='_blank' style='color:white;'>" + ShowAtt[i].attributes[AllFields[z]] + "</a><br>";
                    }

					else
					{
						AttribList = AttribList + AllFields[z] + ": " + ShowAtt[i].attributes[AllFields[z]] + "<br>";
					}
				}
					
			}
			AttribList = AttribList + "<p>";
		}
		
	}
	
	var ShowIdentifyResult = "";
	ShowIdentifyResult = ShowIdentifyResult + "<div id='IdentifyResults' style='position:relative; left:100px; top:-1550px; width:200px; height:250px; z-index:100; display:block;'>";
    ShowIdentifyResult = ShowIdentifyResult + "<img src='javascript/widget_WSM_Features1/ResultBG.png' style='opacity:0.9;filter:alpha(opacity=90)'>";
	ShowIdentifyResult = ShowIdentifyResult + "<div id='identifyResults2' style='position:relative; left:10px; top:-482px; z-index:92; display:block; font-size:8pt; width:310px; height:465px; overflow-y: scroll; color:white;'>";
	ShowIdentifyResult = ShowIdentifyResult + "<table width=290><tr><td width=250 valign=top style='color:white;'><h3>WSM Features</h3></td>";
	ShowIdentifyResult = ShowIdentifyResult + "<td width=40 valign=top><img src='images/Move.png' width=20 onmousedown=dragStart(event,'IdentifyResults');><img src='javascript/widget_WSM_Features1/Close.png' onclick='ClearPanel();'></td></tr></table>";	
	if (AttribList != "")
	{ShowIdentifyResult = ShowIdentifyResult + AttribList;}
	else
	{ShowIdentifyResult = ShowIdentifyResult + "Sorry, no results";}
	
	ShowIdentifyResult = ShowIdentifyResult + "</div></div>"; 

    //PositionResults();
    
	dojo.byId("WidgetResults").innerHTML = ShowIdentifyResult;
	
	//alert(ShowAtt.attributes['Community'] + " : " + ShowAtt.attributes['Applicant'] + " , " + ShowAtt2.attributes['WS_NAME']);
}

function DownloadWindow(i,pSite)
{
	
    var htmlheight = document.body.parentNode.scrollHeight - 25; 
    var htmlwidth = document.body.parentNode.scrollWidth - 25; 
    var windowheight = window.innerHeight - 25; 
    var windowwidth = window.innerWidth - 25;
    if ( htmlheight < windowheight )  
    {
        var OpenX = (windowwidth/2);
		var OpenY = (windowheight/2);
    }
    else
    {
        var OpenX = (htmlwidth/2);
		var OpenY = (htmlheight/2);
    }
    if (pSite == "Bexar")
    { var URLwin = "http://gis.sara-tx.org/website/DFIRM/DisplayModel_mini.asp?lookup=" + i; }
	else if (pSite == "Wilson")
	{var URLwin = "http://gis.sara-tx.org/website/WilsonDownload/DisplayModel_mini.asp?lookup=" + i;}
	else if (pSite == "Karnes")
	{var URLwin = "http://gis.sara-tx.org/website/KarnesDownload/DisplayModel_mini.asp?lookup=" + i; }
	else if (pSite == "Goliad")
	{var URLwin = "http://gis.sara-tx.org/website/GoliadDownload/DisplayModel_mini.asp?lookup=" + i; }		
	else
	{}
	
	var URLparams = "width=410,height=410,left=" + OpenX + ",top=" + OpenY + ",scrollbars=yes";
	window.open(URLwin,"DownloadWin",URLparams);
}

function PositionResults()
{
    var htmlheight = document.body.parentNode.scrollHeight - 25; 
    var htmlwidth = document.body.parentNode.scrollWidth - 25; 
    var windowheight = window.innerHeight - 25; 
    var windowwidth = window.innerWidth - 25;

        document.getElementById("WidgetResults").style.width = "320px";
        document.getElementById("WidgetResults").style.height = "500px";
        document.getElementById("WidgetResults").style.top = "38px";
        
    if ( htmlheight < windowheight ) 
    {
        document.getElementById("WidgetResults").style.left = (windowwidth - 340) + "px";
    }
    else
    {
        document.getElementById("WidgetResults").style.left = (htmlwidth - 340) + "px";
    }
}

function ClearPanel()
{
    //document.getElementById("WidgetResults").style.width = "0px";
    //document.getElementById("WidgetResults").style.height = "0px";
    //document.getElementById("WidgetResults").style.top = "0px";
    //document.getElementById("WidgetResults").style.left = "0px";
    dojo.byId("WidgetResults").innerHTML = "";
    //var i = document.getElementById("WidgetResults");
    //i.setAttribute( 'style', 'position:absolute; left:0px; top:0px; width:0px; height:0px; z-index:90;' );	
			
}



function WSMFeatures_RequestStreetView()
{ 

      var symbol = new esri.symbol.SimpleMarkerSymbol().setStyle(esri.symbol.SimpleMarkerSymbol.STYLE_DIAMOND);
      var vWKID = gMapToLocalVar.spatialReference.wkid;
      var outSR = new esri.SpatialReference({ wkid: 4326 });
      //var vPoint = new esri.geometry.Point(gPointY, gPointX, new esri.SpatialReference({ wkid: vWKID }));
      var vPoint = new esri.geometry.Point(gPointX, gPointY, new esri.SpatialReference({ wkid: vWKID }));
      
      var graphic = new esri.Graphic(vPoint, symbol);
      geoServ.project([vPoint], outSR, function (features) {
          vPoint = features[0];
          gPointX = vPoint.x;
          gPointY = vPoint.y;
          //console.log("x: " + gPointX);
          //console.log("y: " + gPointY);
          //gPointX = geometries[0].x; 
          //gPointY = geometries[0].y; 

          var URLwin = "javascript/widget_WSM_Features1/GoogleStreetView.htm?urlY=" + gPointY + "&urlX=" + gPointX;
          var URLparams = "width=600,height=625,menubar=0,scrollbars=0";
          window.open(URLwin, "StreetViewWin", URLparams);

      });



}


