
////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////
//These navigation tools are copyrighted by Vertices, LLC 
//and users agree to be subject to all United States copyright laws.  
//The initial page of any application using these tools 
//must contain the following notice 
//in a prominent location:  "Navigation tools provided by Vertices, LLC."   
//For more information contact gis@vertices.com
////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////

 

  var VertIcon;			
  var FirstPt ;
  var polyStart = false;		
  var lockPl = true;
  var realPts = new Array();
  var polyVertex = new Array();	
  var oldPoly = new GPolyline();		
  var oldRubber = new GPolyline();		
  //var oldPoly;		
  //var oldRubber;		
  var pIdentify = 0;  //112107sj

  var PlyLength=0.0;			
  var oldpt;			
  var clickLast;  
  var ptCat = 0; 
    var drawedRect = false;
    var isClickZoom = false;
    var iniPoint;
    var x_init=0; 
    var y_init=0; 
    var rectWidth=0; 
    var rectHeight=0; 
    var rectStatus = false; 
  
    var HoldZm = false;
    var OldZm = "";
    var areashow = 0;   
    var tsv ="";  
    var areapolygon = new GPolygon();
    //var areapolygon;
    var areapolyVertex = new Array();
    var fntype = -1;



 

function wheelZoom(a) { (a.detail || -a.wheelDelta) < 0 ? map.zoomIn(null,null,true) : map.zoomOut(null,true); }

function MkeCtlLyr()
 {
 
    document.getElementById("map").onmousemove = rectMouseMove;

   document.getElementById("map").onclick = rectClick;  
  
       var rect = document.getElementById("rectangle");     
      document.getElementById("map").appendChild(rect);




 }




function iniImgSet()
{
 lockPl = true;
 

}



function resultLyr(get1,get2,get3,get4)

{
if (get1 == 1)   {      
                       document.getElementById("distBox").style.visibility = "visible" 
                  }
else if (get1 == 0)            {  
                    document.getElementById("distBox").style.visibility = "hidden"  
                  } 

if (get2 == 1)   {      
                       document.getElementById("AreaResult").style.visibility = "visible"  
                  }
else if (get2 == 0)            {  
                    document.getElementById("AreaResult").style.visibility = "hidden"  
                  } 


if (get3 == 1)   {      
                       document.getElementById("TotSegment").style.visibility = "visible" 
                  }
else if (get3 == 0)    { 
                    document.getElementById("TotSegment").style.visibility = "hidden"  
                  } 


if (get4 == 1)   {      
                       document.getElementById("rectangle").style.visibility = "visible" 
                  }
else if (get4 == 0)    { 
                    document.getElementById("rectangle").style.visibility = "hidden"  
                  } 


}



function DelOnlyPl()
{
 
 //if(oldRubber) map.removeOverlay(oldRubber);
 map.removeOverlay(oldRubber);
 //if(oldPoly) map.removeOverlay(oldPoly);
 map.removeOverlay(oldPoly);
 
	for (var n = 0 ; n < realPts.length ; n++ ) {
		var marker = realPts[n];		
		 map.removeOverlay(marker);		
	}
 //end del point
   polyVertex = new Array();
   realPts = new Array();
 lockPl = false;
 polyStart = true;
PlyLength = 0.0; //sj
       //  distBox,area,segment
        resultLyr(0,0,0,0)
     //if(areapolygon) map.removeOverlay(areapolygon);  
     map.removeOverlay(areapolygon);    
}


function HoldPl()
{ panMap()
  lockPl = true;
  polyStart = false;
}

function chimg(getId)
{
var i;
eventfnon =0;
if (getId == 0) { startMeasure();  fntype=0;PlyLength = 0.0; }
else if (getId ==1 ) {if (fntype==1){stopDraw();fntype=-1;} 
                   else   {startDraw(); fntype=1;PlyLength = 0.0; }  }
else if (getId ==2) {DelPt(); }
else if (getId ==3) {stopDraw();fntype=-1;DelOnlyPl();panMap();eventfnon=1;  }
//else if (getId ==3) {stopDraw();fntype=-1;DelOnlyPl(); } //sj071107 added startDraw(); fntype=1;PlyLength = 0.0;
else if (getId ==4) {go_default(); }
else if (getId ==5) {do_center(); }
else if (getId ==6) {clickZmIn(); }
else if (getId ==7) {clickZmOut(); }
else if (getId ==8) {panMap();eventfnon=1;iniImgSet();//090508 for identify
 }



for (i=0;i < 9;i++)  
  {   
    if (i == getId )
   {document.getElementById(i).src="webimage/img" +i+"_1.gif";}
    else
   {document.getElementById(i).src="webimage/img" +i+"_0.gif";}
  }





}


function startMeasure()
{
 startDraw();
 PlyLength=0.0;
 areashow=0;
}


 function startDraw()
 {

panMap();     
resultLyr(0,3,3,3);

//if(oldRubber) map.removeOverlay(oldRubber);	
map.removeOverlay(oldRubber);  
    
     resultLyr(0,0,0,0);
        
  polyStart = true;

  //if(oldPoly) map.removeOverlay(oldPoly);	
map.removeOverlay(oldPoly);
	for (var n = 0 ; n < realPts.length ; n++ ) {
		var marker = realPts[n];		
		 map.removeOverlay(marker);		
	}

   polyVertex = new Array();
   realPts = new Array();
   lockPl = false; 
    document.getElementById("distBox").style.left = "0px";  
    document.getElementById("distBox").style.top = "0px";
//if(areapolygon) map.removeOverlay(areapolygon);	
 map.removeOverlay(areapolygon);
 areashow=1;

 }


  function stopDraw() {
    polyStart = true;    
      resultLyr(0,3,3,3); 
       //if(oldPoly) map.removeOverlay(oldPoly);
      map.removeOverlay(oldPoly);
    polyVertex[polyVertex.length] = polyVertex[0];
    polyline = new GPolyline(polyVertex, "#000080", 4, 0.50,{ clickable:false });
    map.addOverlay(polyline);		
    oldPoly = polyline;    
    PlyLength = 0.0;  
     //if(oldRubber) map.removeOverlay(oldRubber); 
     map.removeOverlay(oldRubber); 
     areashow=0;
    MapArea();         
    
  }//fn plstop

 function panMap()
 
 {
 ptCat=0;

  isClickZoom = false;
  polyStart = false;
  lockPl == true;
  map.enableDragging();
  document.body.style.cursor = ""; 
      resultLyr(3,3,3,0);  
 }




function DelPt()
{


var marker = realPts[realPts.length-1];
		 map.removeOverlay(marker);   
   realPts.pop(); 
 
    PlyLength -= polyVertex[polyVertex.length-1].distanceFrom(polyVertex[polyVertex.length-2]); //sj
   polyVertex.pop();
   
   
}



  function mapPtChkfn(mapGeoXY) {
    var polyline;  
    
    
	       
      	if (ptCat == 0)
   	{   
     
  		if (lockPl == false)
  		{ 
 
	  		if (polyVertex.length > 0){
	        		document.getElementById("TotSegment").style.visibility = "visible";
				PlyLength += mapGeoXY.distanceFrom(polyVertex[polyVertex.length-1]);  // Total Segment as Meter
          
			        var shwLength;
			        var conVertLength;
			        conVertLength = PlyLength*3.2808;//feet converted
			        
			        if (PlyLength > 5280) { 
			        	conVertLength = conVertLength / 5280;
			                shwLength = conVertLength.toFixed(3) + " mile";
			        }
			        else{
			        
			               shwLength = conVertLength.toFixed(3) + " feet";
			        }
			        
   				document.getElementById("TotSegment").innerHTML = "Total Distance <br>" + shwLength;
           
           
        		}
  
        		polyVertex.push(mapGeoXY);
      

        		if (polyStart){	
          			polyStart = false;
        		}
        		else{
		          	//if(oldPoly) map.removeOverlay(oldPoly);		
		  	  	map.removeOverlay(oldPoly);		
  	    			polyline = new GPolyline(polyVertex, "#000080", 4, 0.50,{ clickable:false });
          			map.addOverlay(polyline);		
          			oldPoly = polyline;
        		}
      			var newCrMarker = new GMarker(mapGeoXY, {icon: VertIcon, draggable: true});
        
         		(realPts.length = 0 ? realPts[0]=newCrMarker : realPts.push(newCrMarker)); 
        		map.addOverlay(newCrMarker); 
        		GEvent.addListener(newCrMarker, "dragstart", function() {
          		oldpt = newCrMarker.getLatLng();	
        		});
              
        		GEvent.addListener(newCrMarker, "dragend", function() {
          		chkRedrawPl(oldpt,newCrMarker.getLatLng());
        		});
        
    		}
    		else
          	{
		
			x_coor=mapGeoXY.x;
			y_coor=mapGeoXY.y;
    if ( pIdentify ==1) //112107sj		             
					  { 
					      if (parent.right.document.getElementById("iframe_identify") )
					         { clkIdentify(x_coor,y_coor) }
							}
		}		   
		if(areashow==1 && shwLength != undefined ) { MapArea(); }
    
	} 
     	else if (ptCat == 1 || ptCat ==2)
     	{  
     		point = mapGeoXY;
         	if ( drawedRect == false ){ 
          		iniPoint = point;   
          		drawedRect = true;
        	}
        	else{   
          		drawedRect = false;    
     
          		if (iniPoint.lat()>point.lat()){
            			if (iniPoint.lng()<point.lng()){
            
              				var geoSW = new GLatLng(point.lat(),iniPoint.lng(),false);
              				var geoNE = new GLatLng(iniPoint.lat(),point.lng(),false);
            			}
           	 		else{
             	 			var geoSW = new GLatLng(point.lat(),point.lng(),false);
              				var geoNE = new GLatLng(iniPoint.lat(),iniPoint.lng(),false);
            			}   
          		}
          		else{
            			if (iniPoint.lng()<point.lng()){
			              	var geoSW = new GLatLng(iniPoint.lat(),iniPoint.lng(),false);
			              	var geoNE = new GLatLng(point.lat(),point.lng(),false);
            			}
            			else{
              				var geoSW = new GLatLng(iniPoint.lat(),point.lng(),false);
              				var geoNE = new GLatLng(point.lat(),iniPoint.lng(),false);
            			}    
          		}
    
                    
          		var geoRectBounds = new GLatLngBounds(geoSW,geoNE);         
          		var newCenter = new GLatLng(((geoRectBounds.getSouthWest()).lat()+(geoRectBounds.getNorthEast()).lat())/2,
                	((geoRectBounds.getSouthWest()).lng()+(geoRectBounds.getNorthEast()).lng())/2 ,false );            
           
              
                 	if (ptCat ==1)
                        {
                        
                        	if(bdetect ==1)  //lets fox            
                              	{
                               		var rectSqzSize = rectWidth + rectHeight;
                                	var ZmFact = parseInt(rectSqzSize/17); 
                                        if (ZmFact < 2 ) { ZmFact = 6; }  
                                        else if (ZmFact < 3 ) { ZmFact = 5; }   
                                        else if (ZmFact < 6 ) { ZmFact = 4; }
                                        else if (ZmFact < 10 ) { ZmFact = 3; }
                                        else if (ZmFact < 14 ) { ZmFact = 2; } 
                                        else    { ZmFact = 1; }         
                                        var newZoom = map.getZoom() + ZmFact;                        
                               	}          
                        	else{ 
                        		var newZoom = map.getBoundsZoomLevel(geoRectBounds); 
                        	}
                        	map.setCenter(newCenter,newZoom);
               		} 
   			else{

        			var rectSqzSize = rectWidth + rectHeight;
        			var ZmFact = parseInt(rectSqzSize/17);      
   
			        if (ZmFact < 2 ) { ZmFact = 6; }  
			        else if (ZmFact < 3 ) { ZmFact = 5; }   
			        else if (ZmFact < 6 ) { ZmFact = 4; }
			        else if (ZmFact < 10 ) { ZmFact = 3; }
			        else if (ZmFact < 14 ) { ZmFact = 2; } 
			        else    { ZmFact = 1; }         
      				var newZoom = map.getZoom() - ZmFact;
          			map.setCenter(newCenter,newZoom);   
                       }
            	} 
     } 
  }


  function chkRedrawPl(old, Numb) {
    
   for (i=0; i<polyVertex.length; i++) {
      if (polyVertex[i]==old) {
       
        polyVertex.splice(i, 1, Numb);
        break;
      }
    }
    //if(oldPoly) map.removeOverlay(oldPoly);		
    map.removeOverlay(oldPoly);		
    polyline = new GPolyline(polyVertex, "#000080", 4, 0.50,{ clickable:false });
    map.addOverlay(polyline);		
    oldPoly = polyline;
  }

 
  function RubberLine(latlong) {
   if (polyVertex.length==0){return;}
 
    var polyline;
    
   if (lockPl == false ) 
    {
 
   if (!polyStart) {
 
      //if(oldRubber) map.removeOverlay(oldRubber);
      map.removeOverlay(oldRubber);
      polyline = new GPolyline([polyVertex[polyVertex.length-1], latlong], "#888888", 2, 1.00,{ clickable:false });
      map.addOverlay(polyline);
      oldRubber = polyline; 
      
        var curDist =   polyVertex[polyVertex.length-1].distanceFrom(latlong)   
       
  	curDist = curDist*3.2808;
	var distBoxElm = document.getElementById("distBox");
        var p = map.fromLatLngToDivPixel(latlong);

 
       if (curDist > 5280)
        {
           curDist = curDist / 5280;
         	document.getElementById("distBox").innerHTML = "Distance: "+ curDist.toFixed(3) + " mile" ;
        }
        else
        {
 	      document.getElementById("distBox").innerHTML = "Distance: "+ curDist.toFixed(3) + " feet" ;
 	      } 	      
	     
              var detectX = parseInt(p.x +20); 
 	      var detectY = parseInt(p.y +10); 	     

	    
 	      
         if (detectX > 900 || detectX < 350){ detectX = 330;} 
 	       if (detectY > 500 || detectY < 90){ detectY = 250;} 
        distBoxElm.style.left = detectX + "px";  
  		   distBoxElm.style.top = detectY + "px";
   
  
      document.getElementById("distBox").style.visibility = "visible";
  
   }     
   }   
  
  }


function MapArea()  
{
     
   with (Math) {   
    var i;
    var j; 
   
   var area = 0.0;
   j = polyVertex.length -1 ;
   if (areashow==1 ) {
  if( polyVertex.length < 3 ) {
       return;}
  } 
  
   for (i = 0 ; i < polyVertex.length; i++)
    { 
    var temMarkerI = polyVertex[i];
    var temMarkerJ = polyVertex[j];
    if (temMarkerI == null || temMarkerJ == null) { return;} //js error
   area  = area + ( temMarkerJ.x * temMarkerI.y - temMarkerJ.y * temMarkerI.x );
     j = i ;
      
    }
   var chkMeasure = (abs(area)/2) * pow(10,11);
  // var wrMeasure = "<font size=1><table border=0 bgcolor=#7F172D><tr><td>";  
   
   if (chkMeasure > 27878400)
    { chkMeasure = chkMeasure/27878400;
      chkMeasure = chkMeasure.toFixed(3);
       wrMeasure = chkMeasure + " sq mile";
      wrMeasure = wrMeasure + "<br>" ;  
   //  wrMeasure = wrMeasure + chkMeasure + " sq mile</td></tr><tr><td>" ;
       wrMeasure = wrMeasure + (640 * chkMeasure).toFixed(3);
       wrMeasure = wrMeasure + " acres";
    }
   else
   {
     chkMeasure = chkMeasure.toFixed(3);
    wrMeasure = chkMeasure + " sq feet";
    wrMeasure = wrMeasure + "<br>" ;    
  //   wrMeasure = wrMeasure + chkMeasure + " sq feet</td></tr><tr><td>" ;
    wrMeasure = wrMeasure +  ((0.000022957) * chkMeasure).toFixed(3) ;
    wrMeasure = wrMeasure + " acre";
    }
    
   // wrMeasure = wrMeasure + "</td></tr></table></font>"
    
      var AreaResult = document.getElementById("AreaResult");
	    var p = map.fromLatLngToDivPixel(polyVertex[0]);
      var h = parseInt(AreaResult.clientHeight); 
    
        
      if (p.x >400 || p.x < 20)
         {p.x = 300;  }
       if (p.y > 400 || p.x < 150 )  
          {      
         p.y = 250;         }
      
      AreaResult.style.left = (p.x - 10) + "px";
  		AreaResult.style.top = (p.y  + 30) + "px"; 
       
       if (areashow ==1 )
       {
       // if(areapolygon) map.removeOverlay(areapolygon);
       map.removeOverlay(areapolygon);
       areapolyVertex = polyVertex;
   areapolygon = new GPolygon(areapolyVertex,"gray",0,0.5,"gray",0.5);
      map.addOverlay(areapolygon)

  AreaResult.style.top = "210px";
  AreaResult.style.left = "370px"; 
       AreaResult.style.visibility = "visible";  
      
      AreaResult.innerHTML = wrMeasure ;
        return;
       } 
       else  {    
      AreaResult.style.visibility = "visible";    
      AreaResult.innerHTML = wrMeasure ; 
             }

     
    }
 
              PlyLength=0.0;      
              lockPl = true;
         
}




  function clickZmIn(){ 

       document.getElementById("rectangle").innerHTML =   "<Div id='ZmCenter' >+</div>";      
       isClickZoom = true;
       OldZm = map.getZoom();     
       map.disableDragging();
       document.body.style.cursor = "crosshair";       
       ptCat = 1;
      drawedRect = false;
     }

function clickZmOut(){       
      document.getElementById("rectangle").innerHTML =   "<Div id='ZmCenter' >-</div>";      
      isClickZoom = true;
      map.disableDragging();
      document.body.style.cursor = "crosshair"; 
            ptCat = 2; 
     drawedRect = false; 
                    
    }



 function mouseX(evt) 
      {
        if (evt.pageX) return evt.pageX;
      	else if (evt.clientX)
          return evt.clientX + (document.documentElement.scrollLeft ?
         	  document.documentElement.scrollLeft :
         	  document.body.scrollLeft);
      	else return null;
      }
      
      function mouseY(evt) 
      {
      	if (evt.pageY) return evt.pageY;
      	else if (evt.clientY)
         	return evt.clientY + (document.documentElement.scrollTop ?
         		document.documentElement.scrollTop :
         		document.body.scrollTop);
      	else return null;
      }
      
      function rectClick(e) 
      {
   
        if (!e) var e = window.event;	
        if (!isClickZoom) return;
        
        if (!rectStatus)
        {
      

          x_init=mouseX(e)- parseInt(document.getElementById("map").offsetLeft); 
          y_init=mouseY(e)- parseInt(document.getElementById("map").offsetTop); 
           
   
          rectStatus=true;  

          document.getElementById("rectangle").style.visibility="visible";
           rectMouseMove(e);         
 
        }
        else
        {
          if(bdetect ==1){ mapPtChkfn(point); }//ff
          document.getElementById("rectangle").style.visibility="hidden"; 
          rectStatus=false;
          isClickZoom = false;
          map.enableDragging();
          document.body.style.cursor = "default";
         
          if (ptCat == 1) {clickZmIn();
   
           }
           else if (ptCat == 2) {clickZmOut();
    
        }
           
        }

   
      }
      
      function rectMouseMove(e){
        
        if (!isClickZoom) return;
        if (!rectStatus) return;		
      
        if (!e) var e = window.event;	
      
     
        var CurX = mouseX(e)- parseInt(document.getElementById("map").offsetLeft);
        var CurY = mouseY(e)- parseInt(document.getElementById("map").offsetTop);
        (CurX<x_init)? CurX +=3:CurX -=3;
        (CurY<y_init)? CurY +=3:CurY -=3;
                  

        rectWidth=Math.abs(CurX - x_init);
        if (CurX >= x_init) 
        { 
      
       	  document.getElementById("rectangle").style.left = x_init+"px"; 
        } 
        else 
        { 
       	  document.getElementById("rectangle").style.left = CurX+"px"; 
        } 
        document.getElementById("rectangle").style.width = rectWidth+"px";
     


        rectHeight=Math.abs(CurY - y_init); 
        if (CurY >= y_init) 
        { 
        document.getElementById("rectangle").style.top = y_init+"px"; 
        } 
        else 
        { 
          document.getElementById("rectangle").style.top = CurY+"px"; 
        } 
        document.getElementById("rectangle").style.height = rectHeight+"px";
       //==========
       	if (rectHeight<30 || rectWidth<30){
        	document.getElementById("ZmCenter").style.display = "none";
        }
        else{
        	document.getElementById("ZmCenter").style.display = "block";
        }
        //===========
        var chkCenter = rectHeight/2;
        
        if( chkCenter > 1 )
        { document.getElementById("ZmCenter").style.marginTop = chkCenter-20+"px";}
      
      }  



function do_center()
{

map.setCenter(new GLatLng(wrlat,wrlon), wrzm);

}
 

