var ClickDetector3;var MAX_NUMBER_LAYERS=1000;var SCRIM_LAYER=1001;var HIGHLIGHT_LAYER=1002;var instance_number=0;ClickDetector3=MapManipulator.extend({options:{thresholdFactor:5,"onVisibilityChanged":Class.empty},initialize:function(D,B){instance_number++;this.className="ClickDetector3 ("+instance_number+")";this.parent({onAttach:this.handleAttach.bind(this),onDetach:this.handleDetach.bind(this),throwExceptions:true});this.setOptions(B);this.handlers=[];this.concept=D;this.clickLocation=null;this.queryLocationProxy=new Ajax("/cbi/maptool/queryLoc",{method:"post",onFailure:function(G){var F=getDetailDisplayInstance();F.clearContent("waiting");F.clearContent("data")},onSuccess:function(G){G=G.trim();var F=getDetailDisplayInstance();F.clearContent("waiting");if(G.length===0){F.clearContent("data")}else{var I=this.gmap;var H=I.getContainer().getCoordinates();var J=I.fromLatLngToContainerPixel(this.clickLocation);F.setLocationHint({x:J.x+H.left,y:J.y+H.top},this.clickLocation);F.setContent("data",G)}}.bind(this)});var C=function(F){F.disableDragging();F.disableDoubleClickZoom();F.disableScrollWheelZoom()};var E=function(F){F.enableDragging();F.enableDoubleClickZoom();F.enableScrollWheelZoom()};this.highlightLayer=null;var A=new GTileLayer(null,null,null,{"tileUrlTemplate":"/static_cbi/i/scrim.png","isPng":true,"opacity":1});this.scrimLayer=new GTileLayerOverlay(A,{"zPriority":SCRIM_LAYER});this.bHasScrim=false;this.highlightLocationProxy=new Ajax("/cbi/identify/highlight_record",{"method":"get","onSuccess":function(H){if(!this.isAttached()){return }if(typeof console!=="undefined"){console.log("this:",this)}var G,F;H=Json.evaluate(H)[0];if(this.highlightLayer!==null){this.gmap.removeOverlay(this.highlightLayer);if(H===null&&this.bHasScrim){this.gmap.removeOverlay(this.scrimLayer);this.bHasScrim=false}}if(typeof console!=="undefined"){console.log("t:",H)}if(H!==null){G=new Date();F=new GTileLayer(null,null,null,{"tileUrlTemplate":"/tiles_data/"+H+"/z{Z}x{X}y{Y}.png?"+G.getTime(),"isPng":true,"opacity":1});this.highlightLayer=new GTileLayerOverlay(F,{"zPriority":HIGHLIGHT_LAYER});this.gmap.addOverlay(this.highlightLayer);if(!this.bHasScrim){this.gmap.addOverlay(this.scrimLayer);this.bHasScrim=true}}if(typeof console!=="undefined"){console.log("scrim:",this.bHasScrim);console.log("this.gmap",this.gmap)}if(this.bHasScrim){C(this.gmap)}else{E(this.gmap)}}.bind(this)});this.queryLocationProxyTwo=new Ajax("/cbi/legend/get_detail_legend",{"method":"get","onFailure":function(G){var F=getDetailDisplayInstance();F.clearContent("waiting");F.clearContent("data")},"onSuccess":function(G){G=G.trim();var F=getDetailDisplayInstance();F.clearContent("waiting");if(G.length===0){F.clearContent("data")}else{var H=this.gmap.getContainer().getCoordinates();var I=this.gmap.fromLatLngToContainerPixel(this.clickLocation);F.setLocationHint({"x":I.x+H.left,"y":I.y+H.top},this.clickLocation);if(typeof console!=="undefined"){if(typeof scratchArea!=="undefined"){console.debug("computed size:",scratchArea.computeSize(G))}}F.setContent("data",G)}}.bind(this)});this.clickHandler=function(L,H){var I=this.gmap;var M=getDetailDisplayInstance();this.clickLocation=H;var J=I.getContainer().getCoordinates();var R=I.fromLatLngToContainerPixel(this.clickLocation);M.setLocationHint({x:R.x+J.left,y:R.y+J.top},H);M.setContent("data","Retrieving data...");M.setContent("waiting",'<img width="15" height="15" src="/static_cbi/i/loading.gif">');var K=Math.pow(2,I.getZoom())*255;var N=(180/K);var O=(360/K);var P=[this.concept,[H.lat(),H.lng()],[N,O]];var Q={"m":this.concept,"n":H.lat()+":"+H.lng(),"o":N+":"+O,"exclude":this._hack_get_excluded_layers().join(",")};var F=this.gmap.getBounds();var G={"concept":this.concept,"latlng":[H.lat(),H.lng()].join(","),"zoom":this.gmap.getZoom(),"bbox":[F.getSouthWest().lat(),F.getSouthWest().lng(),F.getNorthEast().lat(),F.getNorthEast().lng()].join(",")};this.queryLocationProxyTwo.request(Object.toQueryString(Q));this.highlightLocationProxy.request(Object.toQueryString(G))}.bind(this);this.visibilityHandler=function(F){if(this.isAttached()){if(!F){if(this.scrimLayer!==null){this.gmap.removeOverlay(this.scrimLayer)}if(this.highlightLayer!==null){this.gmap.removeOverlay(this.highlightLayer)}}this.fireEvent("onVisibilityChanged",F)}}.bind(this);getDetailDisplayInstance().addEvent("onVisibilityChanged",this.visibilityHandler)},_hack_get_excluded_layers:function(){if(typeof console!="undefined"){if(typeof SNAPSHOT_LAYERIDS!=="undefined"){console.log("layers:",SNAPSHOT_LAYERIDS)}}var A=[];if(typeof SNAPSHOT_LAYERIDS!=="undefined"){var C;for(var B=0;B<SNAPSHOT_LAYERIDS.length;B++){C=SNAPSHOT_LAYERIDS[B];if(dollar("toggle-"+C)&&!dollar("toggle-"+C).getProperty("checked")){A.push(C)}}}return A},handleAttach:function(){if(typeof console!=="undefined"){console.log("attaching:",this,this.gmap)}this.attachEvents()},handleDetach:function(){if(typeof console!=="undefined"){console.log("detaching:",this,this.gmap)}var A=this.gmap;this.gmap=null;getDetailDisplayInstance().clearAllContent();A.enableDragging();A.enableDoubleClickZoom();A.enableScrollWheelZoom();this.detachEvents();if(this.gmap===null){return }if(this.scrimLayer!==null){this.gmap.removeOverlay(this.scrimLayer)}if(this.highlightLayer!==null){this.gmap.removeOverlay(this.highlightLayer)}},attachEvents:function(){this.handlers.push(GEvent.addListener(this.gmap,"click",this.clickHandler))},detachEvents:function(){this.handlers.each(function(A){GEvent.removeListener(A)})}})
