var mileslk = new Array();

var mlats =  
[36303845,36294997,36285865,36283201,36272027,36264968,36255417,
36245242,36246281,36241158,36229250,36225581,36213740];



var mlngs =  
[-87152669,-87143555,-87139821,-87156301,-87155657,-87170892,-87185869,
-87194109,-87179003,-87169046,-87166300,-87181320,-87187672];



var points = 
 ["Harpeth River mouth","Ramp, w/dock, Pardue Rec Area","Bridge, SR-49","Ramp, 2.4R, Bridge Access Area",
 "Bridge, SR-250 Claylick Road","Bridge, Stringfellow Rd","Bridge, Cedar Hill Road",   "Access, canoe, Cedar Hill Rd",
 "Bridge, Cedar Hill Rd","Access, canoe, Narrows of the Harpeth Park","Park, Mace Bluff Archeological Site",
 "Park, Mound Bottom Archeological Site","Access, canoe, Gossett Tract State Natural Area","Access, canoe, Hwy 70",
 "Bridge, US-70",   
 "Gage, near Kingston Springs & US-70","Access, canoe, Kingston Springs City park","Bridge (Old), Kingston Springs City Park",
"Bridge, Pinnacle Hill Rd (Park Street)","Bridge, RR","Bridge, RR",   
"Bridge, RR",
"Bridge, SR-249 E. Kingston Springs Rd","Bridge, I-40 at Buffalo Gap","Bridge, I-40 at Pegram","Bridge, Riverview Drive, Pegram",
"Park, Hidden Lake State Natural Area",   "Access, canoe, McCrory Lane",
"Bridge, McCrory Lane","Bridge, RR","Bridge, Newsom Station Rd",  "Park, Historic Newsoms Mill",
"Bridge, I-40","Bridge, RR","Bridge, RR",
"Bridge (walking), Harpeth River Park, Bellvue","Bridge, RR","Bridge, Old Harding Pike","Gage, Hwy 100 Bellvue","Bridge, Hwy 100", 
"Access, canoe, Hwy100",
"Bridge, Sneed Rd W.","Bridge, Moran Rd","Bridge, SR-46 Old Hillsboro Rd","Bridge, Cotton Lane","Park, Fieldstone, Franklin",
"Gage, US-431 below Franklin","Park, Judge Fulton Greer",   "Access, canoe, Williamson Co Rec Center",
"Bridge, US-431 Hillsboro Road","Bridge, SR-397 Mack Hatcher memorial Pkwy",
"Bridge, US-31 Franklin Rd","Bridge, RR","Park, Fort Granger","Bridge (walking), Pinkerton Park",
"Gage, SR-96 Franklin",   "Access, canoe, Pinkerton Park",
"Bridge, SR-96 Franklin","Bridge, SR-96 Mack Hatcher Pkwy","Bridge (golfers), Franklin",
"Bridge, I-65 & Robinson Lake outflow","Rapids of the Harpeth","Bridge, Arno Rd","Bridge, Trinity Rd","Bridge, Lampkins Bridge Rd",
"Bridge, I-840 at College Grove","Gage, at McDaniel","Bridge, McDaniel Rd","Bridge, RR",
"Bridge, US-31A","Bridge, Bellenfant or College Grove Rd","Bridge, US-41A","Bridge, Shoemaker Rd",
"Bridge, Farm off Little Rock Rd","Bridge, Little Rock Rd"];

var cvalus =  ["0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0",
"waterdata.usgs.gov/tn/nwis/uv?cb_00060=on&cb_00065=on&format=gif_default&period=7&site_no=03434500",
"0","0","0","0","0","0","0","0","0","0","0","0","0",
"0","0","0","0","0","0","0","0","0","waterdata.usgs.gov/tn/nwis/uv?site_no=03433500&PARAmeter_cd=00065,00060","0",
"0","0","0","0","0","0","waterdata.usgs.gov/tn/nwis/uv?site_no=03432400&PARAmeter_cd=00065,00060","0","0","0","0","0","0","0","0",
"waterdata.usgs.gov/tn/nwis/uv?site_no=03432350&PARAmeter_cd=00065,00060","0","0","0","0","0","0","0","0","0","0",
"waterdata.usgs.gov/tn/nwis/uv?site_no=03432100&PARAmeter_cd=00065,00060",
"0","0","0","0","0","0","0","0"];

var clats = 
[36303845,36303791,36286667,36284628,36191389,36171944,36167778,  36152525,
36151389,   36148376,36143342,36139677,36136891,36123949,
36123611,   36121944,   36101631,
36101389,36100855,36100448,36098424,36108455,36107042,36085000,36087691,
36095833,   36094280,   36086694,
36085833,36089763,36080556,   36080654,
36075000,36073082,36070661,36077130,36072584,
36058889,36054444,36053889,   36054130,
36028611,36017222,35993611,35967222,
35956671,35948056,35948177,   35947995,
35947222,35946111,35927778,35926352,35925539,35922651,
35920833,   35920926,
35920278,35900278,35899749,35890000,35871784,35878333,35861944,35851667,
35844421,35832500,35832222,35820767,35799167,
35786111,35766111,35760556,35757028,35756667];


var clngs = 
[-87152669,-87153645,-87146667,-87145631,-87167222,-87160000,-87128333,   -87119326,
-87118611,   -87120885,-87099760,-87100210,-87101932,-87099030,
-87101111,   -87098889,  -87106562,
-87107778,-87107337,-87107664,-87100977,-87074523,-87073908,-87088611,-87062600,
-87054444,   -87029808,   -87025570,
-87025000,-87015621,-86997222,   -86996339,
-86983333,-86987209,-86963852,-86957517,-86954266,
-86944444,-86928611,-86929444,   -86928645,
-86923889,-86899444,-86902222,-86900833,
-86889861,-86881667,-86880666,   -86880127,
-86881111,-86870833,-86865556,-86862516,-86861333,-86862502,
-86865556,   -86864980,
-86865278,-86846389,-86842815,-86829167,-86803263,-86790556,-86763056,-86734167,
-86707031,-86698889,-86698611,-86687944,-86664444,
-86660833,-86646667,-86626944,-86610699,-86608611];

var chtmix = [3,13];
var chtnum = [21,54];

	var poiprev = 0;
	var map = null;
	var miltxt= null;
	var minmapsz = VEMiniMapSize.Large;
        var pixX = 0;
        var pixY = 0;
        var mmile = 0;
        var whichOpt = null;
        
var patterns = [/Ramp/, /Park/, /Gage/, /Bridge/, /Access/i];

	function removeOptions(optionMenu)
	{
	  optionMenu.options.length = 0;
	}

	function addOptions(optionList, optionMenu, whichopt)
	{
	 var i=0; var j=-1;
	 removeOptions(optionMenu);  // clear out the options
	 for (i=0; i < optionList.length; i++)
	    {
 
	     j++ 
	     if (whichOpt == "1") { 
	        optionMenu[j] = new Option(optionList[i]);
	     } else {
	        jjj = j + 1;
	        optionMenu[j] = new Option("p" + jjj + ". " + optionList[i]);
             }
	    }
	}

	function addOpts(optionList, valueList, optionMenu)
	{
	 var i=0; var j=-1;
	 removeOptions(optionMenu);  // clear out the options
	 for (i=0; i < optionList.length; i++)
	    {
	     j++ 
	     optionMenu[j] = new Option(optionList[i], valueList[i]);
	    }
	}

	function includechange()
	{
	 var i=0; var j=-1;
	 for (i=0; i <13; i++)
	  {
	   j++
	   mileslk[j] = i;
	  }
//	 mileslk[19] = 48;
//	 mileslk[20] = 64.6;
//	 mileslk[21] = 97.6;
//	 mileslk[22] = 174;	 
	 whichOpt = "1"; 
	 addOptions(mileslk,document.myform2.mile,whichOpt);
	 whichOpt = "2";
	 addOptions(points,document.myform2.poioh,whichOpt);
//	 addOpts(points,cvalus,document.myform2.poioh);	 
	}

        function preloadOpts()
        {
	      includechange();      
	}

	function UpdateInfo(e)
	{
	        var center = map.GetCenter();
		var ZL = e.zoomLevel;
		document.getElementById("info").innerHTML =
			'HARPETH RIVER:  Latitude = ' + 
			center.Latitude + 
			', Longitude = ' 
			+ center.Longitude + 
			', Zoom=' + 
			ZL ;  
	}
	
         function GetPixel()
         {
            var pinfo = "";
            // Get pixel info about center of the map
            var pixelI = map.LatLongToPixel(map.GetCenter(), map.GetZoomLevel());
            pinfo += "Map center pixel coordinates are : (" + pixelI.x + ", " + pixelI.y + ")\n\n";
            pixX = pixelI.x;
            pixY = pixelI.y;            
            pinfopix = "pixX = " + pixX + "\n\n";
//            alert(pinfo);
//            alert(pinfopix);
         }  	
	
         function PixelClick2(e)
         {
            var x = e.mapX;
            var y = e.mapY;
            pixel2 = new VEPixel(x, y);
            var LL2 = map.PixelToLatLong(pixel2);
            info2.innerHTML =  LL2 + "<br />";            
//            info2.innerHTML = "LatLong: " + LL2;
//            info2.innerHTML = "Pixel X: " + x + " | Pixel Y: " + y + "<br /> LatLong: " + LL2; 
            map.FindLocations(LL2, GetResults);
         }

         function PixelClick(e)
         {
   	    var x = e.mapX;
      	    var y = e.mapY;
      	    pixel = new VEPixel(x, y);
      	    LL = map.PixelToLatLong(pixel);

            map.FindLocations(LL, GetResults);

         }


         function GetResults(locations)
         {
            if(locations)
            {
	       for(var i=0;i<locations.length;i++)
               {
                   info2.innerHTML+=locations[i].Name + "<br />";
               }
            }
            else
            {
                 info2.innerHTML+='No address found.'; 
            } 
         }
         
	function OnPageLoad()
	{
                preloadOpts()

                map = new VEMap('myMap');
            
                map.SetDashboardSize(VEDashboardSize.Normal);
            
                map.LoadMap(new VELatLong(36.303845, -87.152669), 14 ,'h' ,false);
                
                map.ShowDashboard();                
            
//                map.AttachEvent("onclick", PixelClick);
            
		map.AttachEvent("onendpan", UpdateInfo);
			
		map.AttachEvent("onendzoom", UpdateInfo) ;
		
                map.AttachEvent("onclick", PixelClick2); 
                
//                preloadpois();
                
//                preloadmiles();
	}
	
	function preloadmiles()
	{
	 for (i=0; i <19; i++)
	  {
	   document.myform2.mile.options[i].selected = 1;
	   milechange();
	  }
	   document.myform2.mile.options[0].selected = 1;
	   milechange();
	}
	
	function preloadpois()
	{
	 for (i=0; i <27; i++)
	  {
	   document.myform2.poioh.options[i].selected = 1;
	   poichange();
	  }
	}	
	
	function HideMiniMap()
	{
	        map.HideMiniMap();
	}
	         
	function ShowMiniMap()
	{
	    if (map.GetMapMode() == VEMapMode.Mode3D)
	       {
	          map.SetMapMode(VEMapMode.Mode2D);
	       }
	          GetPixel();
                  var minileft = Math.round(pixX + pixX - 191);
//		alert(minileft);    
	          map.ShowMiniMap(minileft, 0, minmapsz);            
        }
        
	function DoNavcht()
	{
	   if (mmile >= 13) 
	    {
	      alert('No Chart Available')
	      return;
	    } 
	    else 
	    {
	      var i=0;
	      for (i=0; i < chtmix.length; i++) {
	         if (mmile < chtmix[i]) {
	             zchtno = chtnum[i];
	             zchtlink = 'http://b02.s3.amazonaws.com/chtpics/Cumberland/CRCH' + zchtno + '.pdf';
//	             zchtlink = 'http://www.lrp.usace.army.mil/navchart/al/chart' + zchtno + '.pdf';
//	             alert (zchtlink);
	             javascript: secondwindow = open(zchtlink, 'navcht', 'scrollbars=yes');
	             return;
	         }    
	      }
            }  
	}        
        
        function DoInfo() {
            var vcenter = map.GetCenter();
            vclat = vcenter.Latitude;
            vclng = vcenter.Longitude;
            
	      zlink = 'http://maps.live.com/default.aspx?v=2&lvl=14&style=h&cp=' + vclat + '~' + vclng;
//	      alert (zlink);
              javascript: secondwindow = open(zlink, 'VEmain', 'scrollbars=yes');            
            
//        http://maps.live.com/default.aspx?v=2&lvl=14&style=h&cp=25.783538~-80.183171
        }
	
         function AddShapes(mlat, mlng, mmile)
         {
            latLongcnt = new VELatLong(mlat, mlng);
            pin02 = new VEShape(VEShapeType.Pushpin, latLongcnt);
            pin02.SetTitle("<font color='blue'> Harpeth River </font>");
//            if (mmile == 19) {
//               mmile=48;
//            } 
//            if (mmile == 20) {
//               mmile = 64.6;
//            }
//            if (mmile == 21) {
//               mmile=97.6;
//            } 
//            if (mmile == 22) {
//               mmile = 174;
//            }            
            pin02.SetDescription("<font color='blue'> Mile " + mmile + "</font>");            
            pin02.SetCustomIcon("<span style='width:30px; height:15px; font-family:Arial,sans-serif; font-weight:bold; font-size:8pt;" +
               "color:white; overflow:hidden; text-align:center; border:1px solid #FF0000; background-color:blue'>" +
                miltxt + "</span>");
            map.AddShape(pin02);
         } 	

         function AddPoi(mlat, mlng, miltxt, mmile)
         {
            latLongcnt = new VELatLong(mlat, mlng);
            poino = mmile + 1;
            pin02 = new VEShape(VEShapeType.Pushpin, latLongcnt);
            pin02.SetTitle("<font color='blue'> Harpeth River </font>");
            
            if (cvalus[poino] != 0) {
                 var refr =  cvalus[poino];
                 var refr2 = "http://" + refr;
                 var pre1 = "<a href='#'";
                 var pre2 = " onclick='javascript: secondwindow = open(";
                 var pre2a = pre2 + '"';
//                 var ino = 1;
//                 var part2 = "swname" + ino;
                 var part2a = '"swname1"';
                 var part2b = '", ' + part2a;
                 var part2c = part2b + ', ';
//                 var part3 = '"height=600,width=800,scrollbars=yes,resizable=yes"';
                 var part3 = '",,scrollbars=yes,resizable=yes"';                          
                 var part3a = part3 + ");'>";
                 var descrip = pre1 + pre2a + refr2 + part2c + part3a;
//                 alert (descrip);
	         pin02.SetDescription("<br />" + descrip + "<font color='blue'>" + points[mmile] + "</font>" + "</a>");                  
 	                              
            } else {
                 pin02.SetDescription("<font color='blue'>" + points[mmile] + "</font>");
            }            
            
//            if (cvalus[poino] != 0) {
//                 var refr =  cvalus[poino];
//	         pin02.SetDescription("<font color='blue'>" + miltxt + "</font>" +
//	                              "<br /> <a href='http://" + refr + "'>" + refr + "</a>" );          
//            } else {
//                 pin02.SetDescription("<font color='blue'>" + miltxt + "</font>");
//            }
            if ((patterns[0].test(miltxt)) || (patterns[4].test(miltxt))) {          
                pin02.SetCustomIcon("<span style='width:30px; height:15px; font-family:Arial,sans-serif; font-weight:bold; font-size:8pt;" +
               "color:white; overflow:hidden; text-align:center; border:1px solid white; background-color:lightgreen'> P" +
                poino + "</span>");
            } else {
               if (patterns[1].test(miltxt) && patterns[3].test(miltxt)) {
                   pin02.SetCustomIcon("<span style='width:30px; height:15px; font-family:Arial,sans-serif; font-weight:bold; font-size:8pt;" +
                  "color:white; overflow:hidden; text-align:center; border:1px solid red; background-color:orange'> P" +
                   poino + "</span>");
               } else { 
                 if (patterns[1].test(miltxt)) {
                     pin02.SetCustomIcon("<span style='width:30px; height:15px; font-family:Arial,sans-serif; font-weight:bold; font-size:8pt;" +
                    "color:white; overflow:hidden; text-align:center; border:1px solid white; background-color:lightgreen'> P" +
                     poino + "</span>");
                 } else {               
                  if (patterns[2].test(miltxt)) {
                      pin02.SetCustomIcon("<span style='width:30px; height:15px; font-family:Arial,sans-serif; font-weight:bold; font-size:8pt;" +
                     "color:white; overflow:hidden; text-align:center; border:1px solid white; background-color:black'> P" +
                      poino + "</span>");
                  } else {          
                      pin02.SetCustomIcon("<span style='width:30px; height:15px; font-family:Arial,sans-serif; font-weight:bold; font-size:8pt;" +
                     "color:white; overflow:hidden; text-align:center; border:1px solid blue; background-color:red'> P" +
                      poino + "</span>");
                  }    
                }
              }  
            }    

            map.AddShape(pin02);
         } 
         
        function markmile()
	{
	 AddShapes(mlat,mlng,mmile);	
	}

	function milehit(mmile)
	{
        mlat  = mlats[mmile] / 1000000;
        mlng  = mlngs[mmile] / 1000000;        
        var latLong = new VELatLong(mlat,mlng);
        map.PanToLatLong(latLong);        
        markmile(mlat, mlng, mmile); 
        dist = Math.abs(mmile - poiprev)
        document.myform2.distnm.value = Math.round(0.86898 * dist);
        document.myform2.distsm.value =  dist;
        document.myform2.distkm.value = Math.round(1.609344 * dist);
	poiprev = mmile;  
        document.myform2.mile.value = "9999";
	}

	function poihit(mmile)
	{
        mlat = clats[mmile] / 1000000;
//        if (mmile > 5) {
        mlng = clngs[mmile] / 1000000;
//        } else {   
//        mlng  = 0.002403 + clngs[mmile] / 1000000; 
//        }
        var latLong = new VELatLong(mlat,mlng);
        map.PanToLatLong(latLong);        
        AddPoi(mlat, mlng, miltxt, mmile); 
//        dist = Math.abs(mmile - poiprev)
//        document.myform2.distnm.value = Math.round(0.86898 * dist);
//        document.myform2.distsm.value =  dist;
//        document.myform2.distkm.value = Math.round(1.609344 * dist);
//	poiprev = mmile;  
//        document.myform2.mile.value = "9999";
	}
	
	function milechange()
	{
             var k; 
	     var hitval=" "; 
	     for (k=0; k < document.myform2.mile.options.length; k++)
	     {
	       if (document.myform2.mile.options[k].selected)
	       {
	         mmile = k;
	         miltxt = document.myform2.mile[k].text;
                 milehit(mmile);  
 	       }
             }
        } 

        function poichange()
	{
             var m; 
//	     var hitval=" "; 
	     for (m=0; m < document.myform2.poioh.options.length; m++)
	     {
	       if (document.myform2.poioh.options[m].selected)
	       {
	         mmile = m;
	         miltxt = document.myform2.poioh[m].text;
                 poihit(mmile);  
 	       }
             }	
	
//	     poi = parseInt (document.myform2.poioh.value);
//             mmile = poi;
//	     miltxt = poi;               
//             milehit(mmile);   	
	}
