var VisualCommentManager3;var VisualComment3;var CommentTextEditor;var getEditorInstance;VisualCommentManager3=MapManipulator.extend({initialize:function(B,A){this.parent({onAttach:this.handleAttach.bind(this),onDetach:this.handleDetach.bind(this),throwExceptions:false});this.concept=B;this.color=A;this.handlers=[];this.polygon=null;this.keyhandler=function(C){C=new Event(C);if(C.key=="esc"){if(this.polygon!==null){}}}.bind(this);this.mouseOverHandler=function(){this.polygon.setFillStyle({opacity:0.5})}.bind(this);this.mouseOutHandler=function(){this.polygon.setFillStyle({opacity:0})}.bind(this);this.polygonEventsHandler=function(){this.handlers.push(GEvent.addListener(this.polygon,"mouseover",this.mouseOverHandler));this.handlers.push(GEvent.addListener(this.polygon,"mouseout",this.mouseOutHandler));this.fireEvent("onShapeClose")}.bind(this);this.commentProxy=new Ajax("/cbi/comment/addVisualComment",{method:"post",onSuccess:function(C){console.log("added comment:",C);this.fireEvent("onCommentCreated")}.bind(this)});this.cancelEditorHandler=function(){var C=getEditorInstance();C.hide();this.detachEditorEvents();this.reset()}.bind(this);this.submitEditorHandler=function(){var C=getEditorInstance();C.hide();this.detachEditorEvents();this.submit(C.getText())}.bind(this);this.showEditor=function(){var C=getEditorInstance();this.attachEditorEvents();C.show(this.getPositionHint())}.bind(this)},handleAttach:function(){this.polygon=new GPolygon([],this.color,3,1,this.color,0.5,{clickable:false});this.gmap.addOverlay(this.polygon);this.polygon.enableDrawing();window.addEvent("keyup",this.keyhandler);this.addEvent("onShapeClose",this.showEditor);this.handlers.push(GEvent.addListener(this.polygon,"endline",this.polygonEventsHandler))},handleDetach:function(){this.polygon.disableEditing();this.gmap.removeOverlay(this.polygon);this.polygon=null;getEditorInstance().hide();window.removeEvent("keyup",this.keyhandler);this.detachEditorEvents();this.removeEvent("onShapeClose",this.showEditor);this.handlers.each(function(A){GEvent.removeListener(A)})},attachEditorEvents:function(){var A=getEditorInstance();A.addEvent("onCancel",this.cancelEditorHandler);A.addEvent("onSubmit",this.submitEditorHandler)},detachEditorEvents:function(){var A=getEditorInstance();A.removeEvent("onCancel",this.cancelEditorHandler);A.removeEvent("onSubmit",this.submitEditorHandler)},submit:function(F){console.log("submitting text:",F);var B=this.polygon;var E=[];var C,A,G;for(A=0,G=B.getVertexCount();A<G;A++){C=B.getVertex(A);E.push([C.lat(),C.lng()])}var D=[this.concept,"no real title",F,E];this.commentProxy.request(Json.toString(D))},getPositionHint:function(){var E=this.gmap;var D=E.getContainer().getCoordinates();var G=this.polygon.getVertex(0);var C;var B=this.polygon;for(var A=1,H=B.getVertexCount();A<H;A++){C=B.getVertex(A);if(C.lat()<G.lat()){G=C}}var F=E.fromLatLngToDivPixel(G);return{x:F.x+D.left,y:F.y+D.top}},reset:function(){var A=this.gmap;this.detach();this.attachTo(A)}});VisualComment3=MapManipulator.extend({initialize:function(C,E,B,A){this.parent({onAttach:this.handleAttach.bind(this),onDetach:this.handleDetach.bind(this),throwExceptions:true});this.movehandler=null;this.isMouseIn=false;this.handlers=[];this.uuid=C;var D=B.map(function(F){return new GLatLng(F[0],F[1])});this.polygon=new GPolygon(D,A,3,0,A,0,{clickable:false});this.enterMapHandler=function(){this.showPolygon();this.showStroke();this.hideFill()}.bind(this);this.leaveMapHandler=function(){this.hidePolygon()}.bind(this);this.mouseMoveHandler=function(G){var F=this.containsPoint(G);if(F&&!this.isMouseIn){this.showFill();this.isMouseIn=true}else{if(!F&&this.isMouseIn){this.isMouseIn=false;this.hideFill()}}}.bind(this)},handleAttach:function(){this.gmap.addOverlay(this.polygon);this.attachEvents()},handleDetach:function(){this.gmap.removeOverlay(this.polygon);this.detachEvents()},attachEvents:function(){this.movehandler=GEvent.addListener(this.gmap,"mousemove",this.mouseMoveHandler);this.handlers.push(this.movehandler);this.handlers.push(GEvent.addListener(this.gmap,"mouseover",this.enterMapHandler));this.handlers.push(GEvent.addListener(this.gmap,"mouseout",this.leaveMapHandler))},detachEvents:function(){this.handlers.each(function(A){GEvent.removeListener(A)});this.movehandler=null},containsPoint:function(A){return VisualComment3.containsPoint(this.polygon,A)},setEnabled:function(A){if(!this.isAttached()){return }if(A&&(this.movehandler===null)){this.attachEvents()}else{if(!A&&(this.movehandler!==null)){this.detachEvents();this.showPolygon();this.showStroke();this.hideFill()}}},showPolygon:function(){this.polygon.show()},hidePolygon:function(){this.polygon.hide()},showStroke:function(){this.polygon.setStrokeStyle({opacity:1})},hideStroke:function(){this.polygon.setStrokeStyle({opacity:0})},showFill:function(){this.polygon.setFillStyle({opacity:0.5})},hideFill:function(){this.polygon.setFillStyle({opacity:0})}});VisualComment3.crosses_segment=function(E,N,A){var F=(A.getSouthWest().lng()<0);var I=N.lng();if(!F&&(I<0)){I=I+360}var G=N.lat();var M=E[0];var C=M.lng();if(!F&&(C<0)){C=C+360}var J=M.lat();var L=E[1];var B=L.lng();if(!F&&(B<0)){B=B+360}var H=L.lat();if(((G>J)&&(G>H))||((G<J)&&(G<H))){return false}if((I>C)&&(I>B)){return false}if((I<C)&&(I<B)){return true}if(G==J){return true}if(G==H){return false}var D=(H-J)/(B-C);var K=D*(I-C)+J;if(D>0){return(G>K)}else{if(D<0){return(G<K)}else{}}return false};VisualComment3.generateSegments=function(D){var A=[];var F,C;for(var B=0,E=D.getVertexCount();B<E;B++){F=D.getVertex(B);if(B==E-1){C=D.getVertex(0)}else{C=D.getVertex(B+1)}if(!F.equals(C)){A.push([F,C])}}return A};VisualComment3.containsPoint=function(F,D){var C=F.getBounds();if(!C.containsLatLng(D)){return false}var A=this.generateSegments(F);var E=0;for(var B=0;B<A.length;B++){if(VisualComment3.crosses_segment(A[B],D,C)){E=E+1}}return((E%2)==1)};var loadComments=function(B,A){A=A||Class.empty;var C=new Ajax("/cbi/comment/getVisualComments",{method:"get",onSuccess:function(F){var H;try{H=Json.evaluate(F)[0]}catch(G){H=[]}var E=H.map(function(L){var K=L.uuid;var M=L.text;var J=L.points;var I=L.color;return new VisualComment3(K,M,J,I)});A(E)}});var D=Object.toQueryString({uuid:B});C.request(D)};CommentTextEditor=new Class({options:{el:"edit_comment",onCancel:Class.empty,onSubmit:Class.empty},initialize:function(C){this.setOptions(C);this.el=$(this.options.el);var D=this.el;$("scroller").adopt(D);var B=$E("#cancel_visual_comment",D);var A=$E("#submit_viz_comment",D);B.addEvent("click",function(){this.fireEvent("onCancel")}.bind(this));A.addEvent("click",function(){this.fireEvent("onSubmit")}.bind(this))},show:function(C){console.log("show commenting widget");this.clearText();var A=C||{x:0,y:0};var B=this.el;B.setStyles({position:"absolute",display:"block",left:(A.x-125),top:A.y,zIndex:"10",width:"250px"})},hide:function(){var A=this.el;A.setStyles({display:"none"})},getText:function(){if(typeof (this.editorContent)=="undefined"){this.editorContent=$E("#comment_body",this.el)}return this.editorContent.value},clearText:function(){if(typeof (this.editorContent)=="undefined"){this.editorContent=$E("#comment_body",this.el)}this.editorContent.value=""}});CommentTextEditor.implement(new Options(),new Events());var COMMENT_TEXT_EDITOR_INSTANCE=null;getEditorInstance=function(){if(COMMENT_TEXT_EDITOR_INSTANCE===null){COMMENT_TEXT_EDITOR_INSTANCE=new CommentTextEditor()}return COMMENT_TEXT_EDITOR_INSTANCE}
