var milesbw = new Array();

var mlats =  
[32532222,32545556,32545278,32538333,32527778,32517500,32529167,
32544167,32558056,32560556,32570833,32585000,32581111,32567778,
32578611,32587222,32594549,32607200,32614167,32608333,32606389,
32614722,32626944,32637778,32637778,32641944,32635000,32642451,
32656990,32653333,32650556,32654722,32668611,32681667,32693056,
32706389,32715556,32706944,32709722,32723333,32733889,32739444,
32752778,32761667,32772222,32783889,32793333,32806389,32816667,
32828056,32837500,32847778,32852778,32861111,32864167,32874776,
32876944,32882904,32887778,32896111,32898333,32898056,32899167,
32906389,32916389,32924312,32927222,32936389,32937222,32948889,
32948889,32956389,32966111,32973976,32975833,32986111,32988056,
33000278,33012778,33015278,33013611,33017222,33025000,
33016389,33029722,33027222,33013611,33013333,33024722,33038056,
33044444,33056667,33060556,33063889,33074444,33085000,33082778,
33085000,33100000,33111944,33124444,33120556,33125633,33114167,
33118611,
33126667,33138056,33151944,33158611,33168611,33182778,33179444,
33171667,33182500,33194167,33188611,33195556,33202778,33197222,
33200556,33205278,33210278,33214722,33219722,33223611,33225556,
33235833,33246389,33251389,33253611,33253333,33253333,33262778,
33276944,33284444,33288611,33303056,33312500,33324722,33339167,
33353611,33366667,33380000,33391111,33403611,33417778,33430556,
33442222,33451944,33464167,33473611,33486667,33490278,33490556,
33481389,33483889,33497500,33500941,33499722,33509284,33518147,
33529095,33516148,33516667,33528611,33539167,33536111,33545278,
33551389,33560278,33571944,33585000,33592222,33593056,33605000,
33618611];

var mlngs =  
[-87853056,-87845278,-87831111,-87816944,-87823611,-87816944,-87807778,
-87797222,-87800556,-87791944,-87786389,-87784167,-87771389,-87766111,
-87757778,-87746111,-87741435,-87747358,-87757500,-87774444,-87789722,
-87802778,-87807778,-87804722,-87788889,-87773056,-87758611,-87746705,
-87745872,-87758611,-87772222,-87788056,-87793056,-87785556,-87795000,
-87792778,-87800833,-87813611,-87827778,-87830000,-87816389,-87805278,
-87809167,-87818889,-87830556,-87835556,-87823056,-87818611,-87817500,
-87819722,-87825000,-87830278,-87823056,-87832778,-87820556,-87820904,
-87811667,-87812584,-87812222,-87806111,-87794167,-87781111,-87771111,
-87760556,-87769444,-87770301,-87764722,-87756944,-87739444,-87729722,
-87744722,-87741944,-87747222,-87743677,-87734167,-87724722,-87708889,
-87712222,-87713889,-87703056,-87693056,-87681389,-87667778,
-87663056,-87658056,-87642500,-87640278,-87626111,-87632500,-87637222,
-87626111,-87630000,-87616944,-87601389,-87591111,-87587778,-87604444,
-87618056,-87617500,-87608611,-87611111,-87627222,-87640020,-87645556,
-87662222,
-87676389,-87686944,-87690833,-87702778,-87691389,-87694722,-87687778,
-87674722,-87675833,-87680556,-87667500,-87660556,-87652222,-87636389,
-87620556,-87604444,-87588333,-87571944,-87555556,-87539444,-87523056,
-87510556,-87498889,-87483611,-87466389,-87450556,-87433889,-87421667,
-87423056,-87408889,-87393611,-87393056,-87406111,-87413611,-87415556,
-87416111,-87409167,-87412222,-87401389,-87394722,-87390278,-87383889,
-87375278,-87361111,-87350833,-87338056,-87331389,-87319444,-87307222,
-87298889,-87284444,-87288889,-87276042,-87261944,-87264784,-87275272,
-87265086,-87257757,-87247222,-87239722,-87231944,-87215833,-87203889,
-87187778,-87194444,-87203889,-87200833,-87204444,-87222222,-87232500,
-87237222];

	var poiprev = 0;
	var map = null;
	var miltxt= null;
	var minmapsz = VEMiniMapSize.Large;
        var pixX = 0;
        var pixY = 0;
        var mmile = 0;

	function removeOptions(optionMenu)
	{
	  optionMenu.options.length = 0;
	}

	function addOptions(optionList, 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]);
    
	    }
	}

	function includechange()
	{
	 var i=217; var j=-1;
	 for (i=217; i <387; i++)
	  {
	   j++
	   milesbw[j] = i;
	  }
	 addOptions(milesbw,document.myform2.mile);
	}

        function preloadOpts()
        {
	      includechange();      
	}

	function UpdateInfo(e)
	{
	        var center = map.GetCenter();
		var ZL = e.zoomLevel;
		document.getElementById("info").innerHTML =
			'BLACK WARRIOR 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(32.532222, -87.853056), 14 ,'h' ,false);
                
                map.ShowDashboard();                
            
//                map.AttachEvent("onclick", PixelClick);
            
		map.AttachEvent("onendpan", UpdateInfo);
			
		map.AttachEvent("onendzoom", UpdateInfo) ;
		
                map.AttachEvent("onclick", PixelClick2); 
	}
	
	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 >= 25) 
	    {
	      alert('No Chart Available')
	      return;
	    } 
	    else 
	    {
	      var i=0;
	      for (i=0; i < chtmix.length; i++) {
	         if (mmile < chtmix[i]) {
	             zchtno = i + 1;
	             zchtlink = 'http://b02.s3.amazonaws.com/chtpics/BigSandy/chart' + 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 DoAIS() {
            var vcenter = map.GetCenter();
            vclat = vcenter.Latitude;
            vclng = vcenter.Longitude;
            
            zaislink = '../bhais.php?zalat=' + vclat + '&zalng=' + vclng;
//	      alert (zaislink);
            javascript: secondwindow = open(zaislink, 'VEmain', 'scrollbars=yes');            
        }   
        
        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'> Black Warrior River </font>");
            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 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 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);  
 	       }
             }
        } 


