function USNG2(){var d="A,B,C,D,E,F,G,H,J,K,L,M,N,P,Q,R,S,T,U,V".split(","),f="F,G,H,J,K,L,M,N,P,Q,R,S,T,U,V,A,B,C,D,E".split(","),n="A,B,C,D,E,F,G,H".split(","),q="J,K,L,M,N,P,Q,R".split(","),t="S,T,U,V,W,X,Y,Z".split(","),w="C,D,E,F,G,H,J,K,L,M,N,P,Q,R,S,T,U,V,W,X".split(","),B=[-80,-72,-64,-56,-48,-40,-32,-24,-16,-8,0,8,16,24,32,40,48,58,64,72],z=Array(20);for(i=0;20>i;i++)z[i]=110946.259*B[i];this.llDistance=function(b,o){lat_s=b.lat*Math.PI/180;lat_f=o.lat*Math.PI/180;d_lon=(o.lon-b.lon)*Math.PI/
180;return Math.atan2(Math.sqrt(Math.pow(Math.cos(lat_f)*Math.sin(d_lon),2)+Math.pow(Math.cos(lat_s)*Math.sin(lat_f)-Math.sin(lat_s)*Math.cos(lat_f)*Math.cos(d_lon),2)),Math.sin(lat_s)*Math.sin(lat_f)+Math.cos(lat_s)*Math.cos(lat_f)*Math.cos(d_lon))};this.fromUTM=function(b,o,g,s,p){var k;grid_square_set=b%6;ew_idx=Math.floor(g/1E5)-1;ns_idx=Math.floor(s%2E6/1E5);switch(grid_square_set){case 1:k=n[ew_idx]+d[ns_idx];break;case 2:k=q[ew_idx]+f[ns_idx];break;case 3:k=t[ew_idx]+d[ns_idx];break;case 4:k=
n[ew_idx]+f[ns_idx];break;case 5:k=q[ew_idx]+d[ns_idx];break;case 0:k=t[ew_idx]+f[ns_idx];break;default:throw"USNG: can't get here";}for(var c=Math.floor(g%1E5).toString(),a=Math.floor(s%1E5).toString();5>c.length;)c="0"+c;for(;5>a.length;)a="0"+a;5<p?(digits=p-5,g=c+(g%1).toFixed(digits).substr(2,digits),s=a+(s%1).toFixed(digits).substr(2,digits)):(g=c.substr(0,p),s=a.substr(0,p));return usng_string=""+b+o+k+g+s};this.toUTMFromFullParsedUSNG=function(b,o,g,s,p,k){var c=0,a=0;switch(b%6){case 1:c=
d;a=n;break;case 2:c=f;a=q;break;case 3:c=d;a=t;break;case 4:c=f;a=n;break;case 5:c=d;a=q;break;case 0:c=f;a=t;break;default:throw"Can't get here";}ew_idx=a.indexOf(g[0]);ns_idx=c.indexOf(g[1]);if(-1==ew_idx||-1==ns_idx)throw"USNG: Invalid USNG 100km grid designator.";c=1E5*(ew_idx+1)+s;a=1E5*(ns_idx+0)+p;min_northing=z[w.indexOf(o)];a+=2E6*Math.ceil((min_northing-a)/2E6);ll=utm_proj.invProj(b,c,a);ll_utm_zone=Math.floor((ll.lon- -180)/6)+1;ll_grid_zone=w[Math.floor((ll.lat- -80)/8)];ll_grid_zone!=
o&&(a-=2E6,ll=utm_proj.invProj(b,c,a),ll_utm_zone=Math.floor((ll.lon- -180)/6)+1,ll_grid_zone=w[Math.floor((ll.lat- -80)/8)]);if(ll_utm_zone!=b||ll_grid_zone!=o)throw"USNG: calculated coordinate not in correct UTM or grid zone! Supplied: "+b+o+" Calculated: "+ll_utm_zone+ll_grid_zone;return{zone:b,easting:c,northing:a,precision:k}};this.toUTM=function(b,o){var g=0,s=0,p=0,k=null,c=null,a=null,e=null,b=b.replace(/ /g,"");re=/([0-9]+)$/;if(fields=re.exec(b))k=fields[0],p=k.length/2,scale_factor=Math.pow(10,
5-p),g=Number(k.substr(0,p))*scale_factor,s=Number(k.substr(p,p))*scale_factor;b=b.substr(0,b.length-2*p);re=/([A-Z][A-Z]$)/;(fields=re.exec(b))&&(c=fields[0]);b=b.substr(0,b.length-2);re=/([0-9]+)([A-Z])/;if(fields=re.exec(b))e=fields[1],a=fields[2];if(!e||!a||!c)if(c&&o){for(var j=1E3,m=null,h=null,l=Math.floor((o.lon- -180)/6)+1,e=l-1;e<=l+1;e++)for(grid_zone_idx=0;20>grid_zone_idx;grid_zone_idx++){a=w[grid_zone_idx];try{result=this.toLonLat(e%60+a+c+k),arc_distance=this.llDistance(o,result),arc_distance<
j&&(j=arc_distance,m=e%60,h=a)}catch(C){}}if(m&&h)e=m,a=h;else throw"USNG: Couldn't find a match";}else if(o){for(var j=1E3,r=h=m=null,l=Math.floor((o.lon- -180)/6)+1,A=Math.floor((o.lat- -80)/8),e=l-1;e<=l+1;e++)for(grid_zone_idx=A-1;grid_zone_idx<=A+1;grid_zone_idx++){var a=w[grid_zone_idx],u,v;switch(e%6){case 1:u=d;v=n;break;case 2:u=f;v=q;break;case 3:u=d;v=t;break;case 4:u=f;v=n;break;case 5:u=d;v=q;break;case 0:u=f;v=t;break;default:throw"Can't get here";}for(ns_idx=0;20>ns_idx;ns_idx++)for(ew_idx=
0;8>ew_idx;ew_idx++)try{c=v[ew_idx]+u[ns_idx],result=this.toLonLat(e%60+a+c+k),arc_distance=this.llDistance(o,result),arc_distance<j&&(j=arc_distance,m=e%60,h=a,r=c)}catch(D){}}if(m&&h)e=m,a=h,c=r;else throw"USNG: Couldn't find a match";}else throw"USNG: Not enough information to locate point.";return this.toUTMFromFullParsedUSNG(e,a,c,g,s,p)};this.fromLonLat=function(b,o){usng_string=new String;for(var g=b.lon,d=b.lat;-180>g;)g+=180;for(;180<g;)g-=180;utm_zone=Math.floor((g- -180)/6)+1;if(!(-80<
d&&80>d))throw"USNG: Latitude must be between -80 and 80. (Zones A and B are not implemented yet.)";var f=w[Math.floor((d- -80)/8)],g=utm_proj.proj(utm_zone,g,d);return this.fromUTM(utm_zone,f,g.utm_easting,g.utm_northing,o)};this.toLonLat=function(b,d){result=this.toUTM(b,d);ll=utm_proj.invProj(result.zone,result.easting,result.northing);ll.precision=result.precision;return ll};this.UTM=function(){var b=(40680631590769-6356752.3*6356752.3)/40680631590769,d=b/(1-b),g=b*b,f=b*g,p=Math.PI/180;this.proj=
function(k,c,a){var e=-(6*(30-k)+3)*p,j=a*p,m=c*p,h=Math.sin(j),l=Math.cos(j),c=h/l,a=c*c,n=a*a,h=6378137/Math.sqrt(1-b*h*h),r=d*l*l,e=l*(m-e),m=6378137*(15*g/256+45*f/1024),l=-223234795*f/3072,j=6378137*(1-b/4-3*g/64-5*f/256)*j+-6378137*(3*b/8+3*g/32+45*f/1024)*Math.sin(2*j)+m*Math.sin(4*j)+l*Math.sin(6*j),l=1-a+r,q=5-18*a+n+72*r-58*d,m=Math.pow(e,5);x=0.9996*h*(e+l*Math.pow(e,3)/6+q*m/120)+5E5;l=(5-a+9*r+4*r*r)*Math.pow(e,4)/24;y=0.9996*(j+h*c*(e*e/2+l+(61-58*a+n+600*r-330*d)*(m*e/720)));return{utm_zone:k,
utm_easting:x,utm_northing:y}};this.invProj=function(k,c,a){var k=-(6*(30-k)+3)*p,e=Math.sqrt(1-b),e=(1-e)/(1+e),j=e*e,m=e*j,c=c-5E5,a=a/0.9996/(6378137*(1-b/4-3*(g/64)-5*(f/256))),j=1.3125*j-1.71875*j*j,a=a+(1.5*e-0.84375*m)*Math.sin(2*a)+j*Math.sin(4*a)+151*m/96*Math.sin(6*a),h=Math.sin(a),e=Math.cos(a),m=h/e,j=6378137/Math.sqrt(1-b*h*h),l=m*m,n=d*e*e,h=1-b*h*h,h=6378137*(1-b)/Math.sqrt(h*h*h),c=c/(0.9996*j),r=c*c,q=c*r,u=r*r,v=l*l,t=n*n;temp2=5+3*l+10*n-4*t-9*d;temp4=61+90*l+298*n+45*v-252*d-3*
t;temp5=(1+2*l+n)*q/6;temp6=5-2*n+28*l-3*t+8*d+24*v;lat=180*(a-j*m/h*(r/2-temp2*(u/24)+temp4*q*q/720))/Math.PI;lon=180*(k+(c-temp5+temp6*c*u/120)/e)/Math.PI;return{lon:lon,lat:lat}}};utm_proj=new this.UTM}
OpenLayers.Control.MGRSMousePosition=OpenLayers.Class(OpenLayers.Control,{element:null,prefix:"",separator:", ",suffix:"",numDigits:5,granularity:10,lastXy:null,displayProjection:null,initialize:function(d){OpenLayers.Control.prototype.initialize.apply(this,arguments)},destroy:function(){this.map&&this.map.events.unregister("mousemove",this,this.redraw);OpenLayers.Control.prototype.destroy.apply(this,arguments)},draw:function(){OpenLayers.Control.prototype.draw.apply(this,arguments);this.element||
(this.div.left="",this.div.top="",this.element=this.div);this.redraw();return this.div},redraw:function(d){var f;if(null==d)f=new OpenLayers.LonLat(0,0);else{if(null==this.lastXy||Math.abs(d.xy.x-this.lastXy.x)>this.granularity||Math.abs(d.xy.y-this.lastXy.y)>this.granularity){this.lastXy=d.xy;return}f=this.map.getLonLatFromPixel(d.xy);if(!f)return;this.displayProjection&&f.transform(this.map.getProjectionObject(),this.displayProjection);this.lastXy=d.xy}d=this.formatOutput(f);d!=this.element.innerHTML&&
(this.element.innerHTML=d)},formatOutput:function(d){var f=OpenLayers.INCHES_PER_UNIT,f=this.map.getResolution()*f[this.map.getUnits()]*(1/f.m),n=parseInt(6-Math.ceil(Math.log(f)/2.302585092994046)),f=parseInt(this.numDigits),q=new USNG2,n=-80<d.lat&&80>d.lat?q.fromLonLat(d,n-1):"undefined";return this.prefix+d.lon.toFixed(f)+this.separator+d.lat.toFixed(f)+" / MGRS: "+n+this.suffix},setMap:function(){OpenLayers.Control.prototype.setMap.apply(this,arguments);this.map.events.register("mousemove",this,
this.redraw)},CLASS_NAME:"OpenLayers.Control.MousePosition"});
