var milesilsag = new Array();

var mlats =  
[41696401,41694328,41691631,41688649,41686389,41684444,41682500,
41679444,41675556,41671389,41663056,41654722,41651389,41651667,
41652222,41655278,41657222,41660278,41653056,41649722,41646944,
41638611,41647222,41658611,41668333,41679722,41694444,41705556,
41718889,41730278];

var mlngs =  
[-87939344,-87921073,-87902675,-87882458,-87862222,-87843056,-87824722,
-87806389,-87786944,-87768889,-87753056,-87736944,-87718056,-87698889,
-87679722,-87661111,-87643889,-87626944,-87622778,-87605556,-87587778,
-87572222,-87562500,-87572778,-87560556,-87552222,-87552500,-87545556,
-87543056,-87535278];

var chtmix = [305,308,311,314,316,318,321,325,329,331,334];

	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=304; var j=-1;
	 for (i=304; i <334; i++)
	  {
	   j++
	   milesilsag[j] = i;
	  }
	 addOptions(milesilsag,document.myform2.mile);
	}

        function preloadOpts()
        {
	      includechange();      
	}

	function UpdateInfo(e)
	{
	        var center = map.GetCenter();
		var ZL = e.zoomLevel;
		document.getElementById("info").innerHTML =
			'Illinois WW Sag:  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(41.696401, -87.939344), 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()
	{
	   jmile = mmile + 304;
	   if (jmile >= 334) 
	    {
	      alert('No Chart Available')
	      return;
	    } 
	    else 
	    {
	    
	      var i=0;
	      for (i=0; i < chtmix.length; i++) {
	         
	         if (jmile < chtmix[i]) {
	             if (i != 0) {
	                zchtno = i + 131;
	                if (zchtno > 139) {
	                    zchtno++;
	                 }   
	             } else {
	                zchtno = 119;
	             }   
	             zchtlink = 'http://www2.mvr.usace.army.mil/NIC2/Documents/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'> Illinois Waterway Sag Channel </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);  
 	       }
             }
        } 


