﻿window.jQuery&&function(l){var o,r;o={fill:{hidden:false,type:VEShapeType.Polygon,fillColor:new VEColor(255,0,0,0),strokeColor:new VEColor(0,0,0,0)},dotted:{hidden:false,type:VEShapeType.Polygon,lineWidth:3,lineType:"Dash",fillColor:new VEColor(0,0,0,0),strokeColor:new VEColor(0,204,0,1)},outline:{hidden:false,type:VEShapeType.Polygon,lineWidth:3,fillColor:new VEColor(0,0,0,0),strokeColor:new VEColor(0,204,0,1)}};r={fill:{hidden:false,type:VEShapeType.Polygon,fillColor:new VEColor(255,0,0,0),strokeColor:new VEColor(0,
0,0,0)},dotted:{hidden:false,type:VEShapeType.Polygon,lineWidth:3,lineType:"Dash",fillColor:new VEColor(0,0,0,0),strokeColor:new VEColor(255,0,0,0)},outline:{hidden:false,type:VEShapeType.Polygon,lineWidth:2,fillColor:new VEColor(0,0,0,0),strokeColor:new VEColor(0,255,0,1)}};l.polygon=function(h,J){function d(a,b){if(typeof b==="undefined"||b===null)b=true;this.points=[];this.maskPoints=[new VELatLong(0,0),new VELatLong(0,0)];this.polygonOutline=this.polygonDotted=this.polygonFill=null;this.id=d.Count;
this.editable=b;this.completed=false;d.Count+=1;d.Current=this;this.init(a)}var m,y,n,K,L,M,N,O,P,z,Q,R,S,T,A,U,s,B,u,V,W,X,v,C,Y,w,Z,$,aa,ba,ca,da,D,E;h.disableSDT();m={};J&&l.extend(m,J);var F=false,x=false,G=false,p=false,t=null,ea=null,j=[],H=new VEShapeLayer,I=new VEShapeLayer;d.Count=0;d.Current=null;d.ControlLayer=new VEShapeLayer;d.selectPolyPackage=function(){};d.setControlHandles=function(){if(this.Current.editable){this.ControlLayer.DeleteAllShapes();for(var a=this.Current.points.length,
b=0;b<a;b++){var c=new VEShape(VEShapeType.Pushpin,this.Current.points[b]);c.isControlPoint=true;c.SetCustomIcon("/images/Listing/Search/DragHandle.gif");this.ControlLayer.AddShape(c)}}};d.prototype.init=function(a){this.id===0&&h.addShapeLayer(d.ControlLayer);if(a&&a.length>=3){this.points=a.slice(0);this.maskPoints=this.points.slice(0);a=null;a=this.editable?o:r;this.polygonFill=n(a.fill,this.points);this.polygonOutline=n(a.outline,this.points);this.polygonDotted=n(a.dotted,this.points,{hidden:true});
d.setControlHandles(this.points)}};d.prototype.update=function(a){if(a)this.points=a;this.polygonOutline.SetPoints(this.points);this.polygonFill.SetPoints(this.points);this.polygonDotted.SetPoints(this.points);d.setControlHandles(this.points)};d.prototype.clear=function(){h.deleteShape(this.polygonFill);h.deleteShape(this.polygonOutline);h.deleteShape(this.polygonDotted);d.ControlLayer.Hide()};d.prototype.toString=function(){return"PolyPackage<"+this.id+">"};y=function(a,b){a=new d(a,b);j.push(a);
return a};n=function(a,b,c){a=l.extend({},a,c);b=new VEShape(a.type,b);b.HideIcon();a.hidden&&b.Hide();a.strokeColor&&b.SetLineColor(a.strokeColor);a.fillColor&&b.SetFillColor(a.fillColor);a.lineWidth&&b.SetLineWidth(a.lineWidth);if(a.lineType)b.Primitives[0].symbol.stroke_dashstyle=a.lineType;H.AddShape(b);return b};aa=function(a,b){if(a){if((a=h.shapeByID(a))&&a.GetType()===VEShapeType.Pushpin&&a.isControlPoint&&a.GetPoints().toString()===d.Current.points[b].toString())return true}else return false;
return false};ba=function(a){var b=a[a.length-2],c=function(e){var f=Math.pow(10,6),g=Math.round(e.Latitude*1E6*f)/f;e=Math.round(e.Longitude*1E6*f)/f;return{lat:g,lon:e}};a=c(a[a.length-1]);b=c(b);return a.lat===b.lat&&a.lon===b.lon};K=function(a){if(p){var b=h.pixelsToLatLong(new VEPixel(a.mapX,a.mapY));d.Current.points.push(b);d.Current.maskPoints[0]=b;if(d.Current.points.length>1)if(d.Current.polygonFill){if(d.Current.points.length>2&&d.Current.polygonFill.GetType()===VEShapeType.Polyline){h.deleteShape(d.Current.polygonFill);
d.Current.polygonFill=n(o.fill,d.Current.points)}var c=aa(a.elementID,0);b=function(){h.deleteShape(d.Current.polygonOutline);d.Current.polygonOutline=n(o.outline,d.Current.points);h.deleteShape(d.Current.polygonDotted);d.Current.polygonDotted=n(o.dotted,d.Current.points);B()};d.Current.points.length>50&&b();d.Current.points.length===3&&c&&b();if(d.Current.points.length>3&&(a.elementID||a.rightMouseButton))if(c||a.rightMouseButton){a=d.Current.points.pop();ba(d.Current.points)?d.Current.points.push(a):
b()}d.Current.update()}else{d.Current.polygonFill=n(o.fill,d.Current.points,{type:VEShapeType.Polyline,fillColor:new VEColor(255,0,0,0.5),strokeColor:new VEColor(255,0,0,0.5)});d.Current.polygonOutline=n(o.outline,d.Current.points,{type:VEShapeType.Polyline});d.Current.maskPoints[1]=b}d.setControlHandles()}else if(a.elementID&&!a.rightMouseButton&&d.Current!==null&&d.Current.editable){if((t=h.shapeByID(a.elementID))&&t.GetType()===VEShapeType.Pushpin&&t.isControlPoint){x=true;d.Current.polygonDotted.Show();
h.enableGC();l("#"+m.mapDiv).css("cursor","crosshair");b=P(t);if(b>-1)ea=b}if((a=h.shapeByID(a.elementID))&&a.GetType()===VEShapeType.Polygon){G=true;return false}}};M=function(a){var b=h.pixelsToLatLong(new VEPixel(a.mapX,a.mapY));if(p){d.Current.maskPoints[1]=b;if(d.Current.points.length>0){if(d.Current.polygonDotted===null)d.Current.polygonDotted=n(o.dotted,d.Current.maskPoints,{type:VEShapeType.Polyline,hidden:true});d.Current.polygonDotted.SetPoints(d.Current.maskPoints);d.Current.polygonDotted.Show()}}if(x){d.ControlLayer.Show();
t.SetPoints(b);d.Current.points[ea]=b;d.Current.polygonDotted.SetPoints(d.Current.points)}b=v();if(b.length>0){if((a=w(b,h.pixelsToLatLong(new VEPixel(a.mapX,a.mapY))))&&!F)m.onPolygonMouseOver&&m.onPolygonMouseOver();else!a&&F&&m.onPolygonMouseOut&&m.onPolygonMouseOut();F=a}};O=function(){if(!(d.Current===null||!d.Current.editable)){if(x){x=false;d.Current.polygonDotted.Hide();h.disableGC();l("#"+m.mapDiv).css("cursor","");d.Current.polygonFill.SetPoints(d.Current.points);d.Current.polygonOutline.SetPoints(d.Current.points);
m.onPolygonPointMoveComplete&&m.onPolygonPointMoveComplete()}if(G)G=false}};L=function(a){if(!p&&a.elementID){a=h.shapeByID(a.elementID);var b=a.GetShapeLayer();if(b===d.ControlLayer||b===H)if(a.GetType()===VEShapeType.Polygon||a.GetType()===VEShapeType.Pushpin){a=Q(a);if(a!==null&&d.Current!==a){d.Current=a;d.setControlHandles()}d.ControlLayer.Show()}}};N=function(){};R=function(a){p&&a.keyCode===27&&d.Current.points.length>=3&&B();if(d.Current.editable){a.ctrlKey&&a.keyCode===102&&A(10);a.ctrlKey&&
a.keyCode===100&&A(-10)}};P=function(a){var b=-1;a=a.GetPoints();for(var c=d.Current.points.length,e=0;e<c;e++){var f=d.Current.points[e].toString();if(a.toString()===f){b=e;break}}return b};z=function(){if(j){for(var a=j.length;a--;)j[a].clear();j.length=0}};Q=function(a){for(var b=j.length,c=0;c<b;c++){var e=j[c];if(e.polygonFill===a||e.polygonDotted===a||e.polygonOutline===a)return e}return null};S=function(a){a=l.map(a,function(b){return h.latLongsToPixel(b)});return h.pixelsToLatLong(T(a))};
T=function(a){for(var b=new VEPixel(0,0),c=0,e=0,f=0,g=0,k=0,i=0,fa=a.length-1,q=0;q<fa;q++){e=a[q].x;f=a[q].y;g=a[q+1].x;k=a[q+1].y;i=e*k-g*f;c+=i;b.x+=(e+g)*i;b.y+=(f+k)*i}e=a[q].x;f=a[q].y;g=a[0].x;k=a[0].y;i=e*k-g*f;c+=i;b.x+=(e+g)*i;b.y+=(f+k)*i;c*=0.5;b.x/=6*c;b.y/=6*c;return b};A=function(a){d.Current.update(U(d.Current.points,a))};U=function(a,b){var c=h.latLongsToPixel(S(a));return l.map(a,function(e){e=h.latLongsToPixel(e);e=new VEPixel(c.x+(Math.cos(s(b))*(e.x-c.x)-Math.sin(s(b))*(e.y-
c.y)),c.y+(Math.sin(s(b))*(e.x-c.x)+Math.cos(s(b))*(e.y-c.y)));return h.pixelsToLatLong(e)})};s=function(a){return Math.PI*(a/180)};B=function(){u();m.onPolygonComplete&&m.onPolygonComplete()};u=function(){setTimeout(function(){l(document).unbind("contextmenu")},1E3);l("#"+m.mapDiv).css("cursor","");p=false;h.disableGC();if(d!==null&&d.Current!==null){d.Current.polygonDotted!==null&&d.Current.polygonDotted.Hide();d.Current.completed=true}};W=function(a,b){if(a&&b)if(a.length!==b.length)return false;
else{for(var c=0,e=a.length;c<e;c++){var f=a[c].lon||a[c].Longitude,g=b[c].lon||b[c].Longitude;if((a[c].lat||a[c].Latitude)!==(b[c].lat||b[c].Latitude)||f!==g)return false}return true}return false};V=function(a,b){if(a&&b)if(a.length!==b.length)return false;else{for(var c=0,e=a.length;c<e;c++)if(!W(a,b))return false;return true}return false};X=function(a){for(var b=0,c=0,e=a.length;c<e-1;c++){var f=a[c],g=a[c+1];b+=(g.lat-f.lat)*(g.lon+f.lon)}c=a[a.length-1];a=a[0];b+=(a.lat-c.lat)*(a.lon+c.lon);
return b>0};v=function(){for(var a=[],b=0,c=j.length;b<c;b++){var e=[],f=j[b];if(f.completed){for(var g=f.points.length,k=0;k<g;k++){var i=f.points[k];e.push({lat:i.Latitude,lon:i.Longitude})}if(e.length>0)X(e)?a.push(e):a.push(e.reverse())}}return a};C=function(a){return l.map(a,function(b){return new VELatLong(b.lat,b.lon)})};E=function(a,b){if(l.isArray(a)){var c=0,e=0,f=null,g=[];c=0;for(e=a.length;c<e;c++){f=a[c];l.isArray(f)&&f.length>0&&l.isArray(f[0])?g.push(E(f,b)):g.push(b(f))}}else throw"Input must be of type array!";
return g};ca=function(a){if(a.length>0){var b=a[0][0],c=a[0].concat(b),e=[],f=new VEShape(VEShapeType.Polyline,c);f.SetLineColor(r.outline.strokeColor);f.HideIcon();e.push(f);for(f=1;f<a.length;f++){c=c.concat(a[f],a[f][0],b);var g=new VEShape(VEShapeType.Polyline,a[f].concat(a[f][0]));g.SetLineColor(r.outline.strokeColor);g.HideIcon();e.push(g)}a=new VEShape(VEShapeType.Polygon,c);a.SetLineColor(new VEColor(0,0,0,0));a.SetFillColor(r.fill.fillColor);a.HideIcon();return e.concat(a)}return null};da=
function(a){for(var b=[],c=0,e=a.length;c<e;c++)b.push(ca(a[c]));return b};D=function(){I.DeleteAllShapes()};w=function(a,b){for(var c=0,e=a.length;c<e;c++)if(Y(a[c],b))return true;return false};Y=function(a,b){var c=b.Longitude,e=b.Latitude;if(!Z($(a),b))return false;var f=a.length-1;b=false;for(var g=0,k=a.length;g<k;g++){var i=a[g];f=a[f];if(i.lon<c&&f.lon>=c||f.lon<c&&i.lon>=c)if(i.lat+(c-i.lon)/(f.lon-i.lon)*(f.lat-i.lat)<e)b=!b;f=g}return b};Z=function(a,b){var c=b.Longitude;b=b.Latitude;return b>=
a.minLat&&b<=a.maxLat&&c>=a.minLon&&c<=a.maxLon?true:false};$=function(a){if(a){for(var b=90,c=-90,e=180,f=-180,g=0,k=a.length;g<k;g++){var i=a[g];b=Math.min(b,i.lat);e=Math.min(e,i.lon);c=Math.max(c,i.lat);f=Math.max(f,i.lon)}return{minLat:b,minLon:e,maxLat:c,maxLon:f}}return null};(function(){h.addShapeLayer(H);h.addShapeLayer(I);h.attach([{n:"onmouseover",f:L},{n:"onmousedown",f:K},{n:"onkeypress",f:R},{n:"onmouseup",f:O},{n:"onmousemove",f:M},{n:"onmouseout",f:N}])})();return{inDrawMode:function(){return p},
deleteGeoSpatial:D,plotGeoSpatial:function(a){if(a){D();a=da(E(a,C));for(var b=0,c=a.length;b<c;b++)for(var e=a[b],f=0,g=e.length;f<g;f++)I.AddShape(e[f])}},plotPolygons:function(a,b,c){u();if(typeof b==="undefined"||b===null)b=true;if(typeof c==="undefined"||c===null)c=true;if(a){z();for(var e=[],f=0,g=a.length;f<g;f++){var k=C(a[f]);e=e.concat(k);y(k,c).completed=true}b&&h.setView(e)}},deleteAllPolygons:z,deletePolygon:function(a){if(j&&a)for(var b=0,c=j.length;b<c;b++)V(j[b].points,a)&&j.splice(b,
1).clear()},startDrawing:function(){l(document).bind("contextmenu",function(){return false});y();if(!p){d.ControlLayer.DeleteAllShapes();d.ControlLayer.Show()}h.enableGC();p=true;l("#"+m.mapDiv).css("cursor","crosshair")},cancelDrawing:u,getPolygons:v,getEncodedPolygons:function(){return[l.encodePoints(v())]},isPointInPolygons:w,isPointInMultiPolygons:function(a,b){for(var c=0,e=a.length;c<e;c++)if(w(a[c],b))return true;return false},getPolygonCount:function(){var a=0;if(j)for(var b=0,c=j.length;b<
c;b++)j[b]&&j[b].completed&&a++;return a},getPolygonPointCount:function(){return d&&d.Current&&d.Current.points?d.Current.points.length:0}}}}(jQuery);

