﻿// JScript File

//document.onmousedown=CreateBoundingBox;
var selectionInProgress=false;
var zb =  document.getElementById("boundingBox"); 
var corner1lat;
var corner1lon;
var kk=0;
var x1,y1,x2,y2;

var zbshape;
//document.onmousemove=resizeZoomBox;

//document.onmousemove=moveHandler;
function moveHandler(e){

    if (selectionInProgress==true && kk==1){
        resizeZoomBox(e);
     } 
}

function StartBoundingBox(){
  selectionInProgress=true;
  kk=0; 
  var mapDiv = document.getElementById("myMap");
  mapDiv.style.cursor = "default";

  
}
function CreateBoundingBox(e){
  if (selectionInProgress){
    kk++;
    if(kk==1){
        y1 = e.mapY;
        x1 = e.mapX;
        y2 = e.mapY;
        x2 = e.mapX;
      }
      if (kk==2){
       

        selectionInProgress=false;
        //map.Remove
        //addzbshape();
        kk=0;

        var p1 = map.PixelToLatLong(new VEPixel(x1,y1));
        var p2 = map.PixelToLatLong(new VEPixel(x2,y2));
        x1 = p1.Longitude;
        x2 = p2.Longitude;
        y1 = p1.Latitude;
        y2 = p2.Latitude;
        document.getElementById("SouthLat").value=Math.round((y1 < y2 ? y1 : y2)*1000)/1000;  
        document.getElementById("WestLong").value=Math.round((x1 < x2 ? x1 : x2)*1000)/1000; 
        document.getElementById("NorthLat").value=Math.round((y1 > y2 ? y1 : y2)*1000)/1000;
        document.getElementById("EastLong").value=Math.round((x1 > x2 ? x1 : x2)*1000)/1000;
        var boxArray = new Array();
        boxArray.push(p1);
        boxArray.push(p2);
        map.SetMapView(boxArray);
        selectionInProgress = false;
        kk = 0;
        
      }
    }
}
function addzbshape(){
    if (x1!=x2 || y1!=y2){
        try{
            var p1 = map.PixelToLatLong(new VEPixel(x1,y1));
            var p2 = map.PixelToLatLong(new VEPixel(x1,y2));
            var p3 = map.PixelToLatLong(new VEPixel(x2,y2));
            var p4 = map.PixelToLatLong(new VEPixel(x2,y1));
            zbshape = new VEShape(VEShapeType.Polygon,new Array(p1,p2,p3,p4)); 
            zbshape.SetLineWidth(1);
            zbshape.SetLineColor(new VEColor(255,100,100,1.0));
            zbshape.SetFillColor(new VEColor(255,255,255,0.2)); 
            zbshape.HideIcon();          
            map.AddShape(zbshape);
        }catch(er){
            status = "exception: addzbshape: " + er.message;
        }
    }
}
//function somefunction(eastLon,westLon,northLat,southLat){

//map.SetMapView([new VELatLong(northLat,westLon), 
//                new VELatLong(southLat, eastLon)]);

//}

function resizeZoomBox(e){
    y2 = e.mapY;
    x2 = e.mapX;

    try{
        map.DeleteShape(zbshape);
    }catch(er){
       status = "exception: resize: " + er.message;
    }
    addzbshape();
}



