var GeoSearch3;var GeoCoords;var getGeoSearchInstance;var patchExplorerStyles;var positionGeoSearch,positionFakeSearch;var loadControls,setupControls,panControls;var GMAP_INSTANCE=null;var MIN_ZOOM_LEVEL=0;GeoSearch3=DrawableBounds.extend({options:{onSubmit:Class.empty,onCancel:Class.empty,showMeta:false},pushZoomAndPosition:function(){this.gmap.savePosition();this.bHasSavedPositon=true},popZoomAndPosition:function(){if(this.bHasSavedPositon){this.gmap.returnToSavedPosition();this.bHasSavedPositon=false}},enableMapControls:function(){this.bMapControlsEnabled=true;$("_zoomin").addClass("zoomIn").removeClass("disabledZoomIn");$("_zoomout").addClass("zoomOut").removeClass("disabledZoomOut");$("_zoomStepContainer").addClass("zoomSteps").removeClass("disabledZoomSteps");for(var A in panControls){if(panControls.hasOwnProperty(A)){panControls[A].enable()}}this.attachControlEvents()},disableMapControls:function(){this.bMapControlsEnabled=false;$("_zoomin").removeClass("zoomIn").addClass("disabledZoomIn");$("_zoomout").removeClass("zoomOut").addClass("disabledZoomOut");$("_zoomStepContainer").removeClass("zoomSteps").addClass("disabledZoomSteps");for(var A in panControls){if(panControls.hasOwnProperty(A)){panControls[A].disable()}}this.detachControlEvents()},initialize:function(A){this.parent({showDelete:true,updateOnAttach:true,opacity:0.5});this.setOptions(A);this.bHasSavedPositon=false;this.handlers_=[];this.selection=null;this.geocoords=new GeoCoords();this.submitButton=$("geo_submit");this.drawButton=$("draw_geo_selection");this.closeButton=$("xgeo_search_widget");this.widget=$("geo_search_widget");this.zoomToggles=$("_zoomStepContainer").getChildren();this.bMapControlsEnabled=true;this.closeHandler=function(){this.hide();this.detach()}.bind(this);this.drawHandler=function(){if(this.hasExplictBounds()){this.simulateDelete()}this.setMode(InteractionMode.DRAW_SELECTION)}.bind(this);this.selectionChangeHandler=function(C,B,E,D){this.geocoords.setSouthEast(C,D);this.geocoords.setNorthWest(E,B);this.geocoords.setArea(this.computeArea(C,B,E,D));this.selection=[C,B,E,D]}.bind(this);this.submitHandler=function(){console.log("submitted");this.fireEvent("onSubmit")}.bind(this);this.cancelHandler=function(){this.fireEvent("onCancel")}.bind(this);this.delete_handler=function(){this.setMode(InteractionMode.PAN);hideMapBoundsWarning();resetDatasetBoundsSelect();this.clearSelection()}.bind(this);this.zoom_out_handler=function(){var B=2;if(this.gmap.getZoom()>B){this.gmap.zoomOut()}}.bind(this);this.zoom_in_handler=function(){this.gmap.zoomIn()}.bind(this);this.map_zoom_handler=function(B,C){var D=2;C=C-D;this.zoomToggles.each(function(F,E){if(E<=C){F.addClass("onstate")}else{F.removeClass("onstate")}})}.bind(this)},attachControlEvents:function(){$("_zoomout").addEvent("click",this.zoom_out_handler);$("_zoomin").addEvent("click",this.zoom_in_handler);var B=2;var A=function(C){if(this.bMapControlsEnabled){this.gmap.setZoom(C)}};console.log("attaching controls to toggles");this.zoomToggles.each(function(D,C){D.addEvent("click",A.bind(this,C+B))},this)},detachControlEvents:function(){$("_zoomout").removeEvent("click",this.zoom_out_handler);$("_zoomin").removeEvent("click",this.zoom_in_handler);console.log("detaching controls to toggles");this.zoomToggles.each(function(B,A){B.removeEvents("click")},this)},handleAttach:function(){loadControls(function(){setupControls(this.gmap)}.bind(this));this.closeButton.addEvent("click",this.closeHandler);this.submitButton.addEvent("click",this.submitHandler);this.drawButton.addEvent("click",this.drawHandler);this.addEvent("onSelectionChange",this.selectionChangeHandler);this.attachControlEvents();this.addEvent("onDelete",this.delete_handler);this.handlers_.push(GEvent.addListener(this.gmap,"zoomend",this.map_zoom_handler));this.dataset_bounds=$("dataset_bounds");this.dataset_geosearch_proxy=null;this.select_handler=function(B){if(this.dataset_geosearch_proxy===null){this.dataset_geosearch_proxy=new Ajax("/cbi/search/get_bounds",{method:"get",onComplete:function(C){console.log("got response for bounds:",C);var E=Json.evaluate(C)[0];var D=E.map(function(G){return new GLatLng(G[0],G[1],true)});var F=new GLatLngBounds(D[0],D[1]);this.setBounds(F)}.bind(this)})}if(B.selectedIndex!==0){console.log("selected:",B.options[B.selectedIndex].value);this.dataset_geosearch_proxy.request(Object.toQueryString({dataset:B.options[B.selectedIndex].value}))}else{if(B.selectedIndex===0){this.simulateDelete()}}hideDatasetBoundsAttachment()}.bind(this,this.dataset_bounds);if(this.dataset_bounds){this.dataset_bounds.addEvent("change",this.select_handler)}else{console.error("could not find dataset_bounds")}var A=$("dataset_geo_selection");if($defined(A)){$("dataset_geo_selection").addEvent("click",function(){if(bAttachmentVisible){hideDatasetBoundsAttachment();bAttachmentVisible=false}else{showDatasetBoundsAttachment(this,[$("scroller")]);bAttachmentVisible=true}})}$("dataset_area_attachment_close").addEvent("click",function(){hideDatasetBoundsAttachment()});this.addEvent("onMaskCleared",function(){this.enableMapControls()});this.addEvent("onMaskCompleted",function(){this.disableMapControls()}.bind(this));this.parent()},setBounds:function(G){var J=this.gmap.getBoundsZoomLevel(G);var F=(J>=2);if(F){hideMapBoundsWarning()}else{J=2;showMapBoundsWarning(this.gmap,this.cover)}var A=G.getCenter();if(F){var I=G.getSouthWest();var E=G.getNorthEast();var B=this.gmap.fromLatLngToContainerPixel(I);var H=this.gmap.fromLatLngToContainerPixel(E);var C=new GPoint((B.x+H.x)/2,(B.y+H.y)/2);var D=this.gmap.fromContainerPixelToLatLng(C);A=D}this.gmap.setCenter(A,J);this.resetMasks();this.hideCover();this.parent(G)},handleDetach:function(){this.parent();this.submitButton.removeEvent("click",this.submitHandler);this.closeButton.removeEvent("click",this.closeHandler);this.drawButton.removeEvent("click",this.drawHandler);this.removeEvent("onSelectionChange",this.selectionChangeHandler);this.removeEvent("onDelete",this.delete_handler);this.dataset_bounds.removeEvent("change",this.select_handler);var A=$("dataset_geo_selection");if($defined(A)){$("dataset_geo_selection").removeEvents("click")}$("dataset_area_attachment_close").removeEvents("click");this.detachControlEvents();this.handlers_.each(function(B){GEvent.removeListener(B)});resetDatasetBoundsSelect()},computeArea:function(I,F,B,C){var H=[[I,F],[B,F],[B,C],[I,C],[I,F]];var D=H.map(function(J){return new GLatLng(J[0],J[1])});var E=new GPolygon(D);var G=E.getArea();var A=(G/(1600*1600)).toFixed(2);return A},getSelection:function(){return this.selection},setSelection:function(C){var A=C.getCenter();var B=Math.max(0,this.gmap.getBoundsZoomLevel(C)-1);GMAP_INSTANCE.setCenter(A,B);this.map_zoom_handler(null,B);this.setMode(InteractionMode.DRAW_SELECTION);this.cover.setStyles({background:"transparent"});setOpacity(this.cover,1);this.dragging_start={latlng:C.getSouthWest()};this.dragging_end={latlng:C.getNorthEast()};this.updateMasks();this.bSelectionSet=true},clearSelection:function(){this.bSelectionSet=false},handleCoverEvents:function(){return !this.bSelectionSet},show:function(){this.widget.setStyles({display:"block"});GMAP_INSTANCE.checkResize();GMAP_INSTANCE.setCenter(new GLatLng(39.5,-98.3),4);this.map_zoom_handler(null,4)},hide:function(){this.widget.setStyles({display:"none"})}});GeoCoords=new Class({initialize:function(){this.se_long=$("se_long");this.se_lat=$("se_lat");this.nw_long=$("nw_long");this.nw_lat=$("nw_lat");this.total_area=$("area")},setSouthEast:function(A,B){if(A&&B){this.se_long.innerHTML=B.toFixed(4);this.se_lat.innerHTML=A.toFixed(4)}},setNorthWest:function(A,B){if(A&&B){this.nw_long.innerHTML=B.toFixed(4);this.nw_lat.innerHTML=A.toFixed(4)}},setArea:function(A){this.total_area.innerHTML=A+" sq. miles"}});setupControls=function(G){if(typeof (PanMapControl)!="undefined"){var F=G.getSize();var E=2;var C=new PanMapControl();var A=new PanMapControl();var B=new PanMapControl();var D=new PanMapControl();C.setup({dir:"n"});A.setup({dir:"s"});B.setup({dir:"w"});D.setup({dir:"e"});G.addControl(C,new GControlPosition(G_ANCHOR_TOP_LEFT,new GSize((F.width-C.getSize().x)/2,E)));G.addControl(A,new GControlPosition(G_ANCHOR_BOTTOM_LEFT,new GSize((F.width-A.getSize().x)/2,E)));G.addControl(B,new GControlPosition(G_ANCHOR_TOP_LEFT,new GSize(E,(F.height-B.getSize().y)/2)));G.addControl(D,new GControlPosition(G_ANCHOR_TOP_RIGHT,new GSize(E,(F.height-D.getSize().y)/2)));panControls={north:C,south:A,west:B,east:D};console.log("i've setup the pan controls:",panControls)}else{setupControls.delay(200,setupControls,G)}};loadControls=function(D){D=D||Class.empty;if(typeof (PanMapControl)=="undefined"){var B="/static_cbi/js/mapcontrols.js";var A,C;if(window.ie){A={onreadystatechange:D}}else{A={onload:D}}C=new Asset.javascript(B,A)}else{D()}};var GEO_SEARCH_INSTANCE=null;getGeoSearchInstance=function(A){if(GEO_SEARCH_INSTANCE===null){var B=A||false;GEO_SEARCH_INSTANCE=new GeoSearch3({overflown:[$("scroller")],showMeta:B})}return GEO_SEARCH_INSTANCE};patchExplorerStyles=function(B){console.log("patching styles");var A;[".greeni1",".greeni2",".greeni3",".whitei1",".whitei2",".whitei3",".gTabHead",".encoder"].each(function(C){A=$ES(C,B);console.log(C,A.length,A);A.setStyle("width","auto")});console.log("patched styles")};positionFakeSearch=function(){var D=$("Search").getCoordinates([$("scroller"),$("sizer"),$(document.getElementsByTagName("body")[0])]);D.width=870;var A=$("scroller");var B={border:"3px solid black",background:"blue",width:Math.floor(D.width*0.9)+"px",height:"500px",left:D.left+Math.floor((0.1*D.width)/2)+"px",top:"100px",position:"absolute"};console.log("coords:",Json.toString(D));console.log("styles:",Json.toString(B));var C=new Element("div",{styles:B});C.injectInside(A)};positionGeoSearch=function(E){var G=$("geo_search_widget");if(GMAP_INSTANCE===null){var A=$("scroller");A.adopt(G);if(window.ie){patchExplorerStyles(G);G.setStyle("border","1px solid black")}GMAP_INSTANCE=new GMap2($("map_canvas"))}var H=$(E);var F=H.getCoordinates([$("scroller"),$("sizer"),$(document.getElementsByTagName("body")[0])]);if(window.ie){F.width=870}var C=F.width*0.9;var B=F.left+((F.width-C)/2);var D=100;G.setStyles({width:C,left:B+"px",top:D+"px"});GMAP_INSTANCE.setCenter(new GLatLng(39.5,-98.3),4);return GMAP_INSTANCE};var DATASET_BOUNDS_ATTACHMENT_INITIALIZED=false;var bAttachmentVisible=false;var showDatasetBoundsAttachment=function(D,A){A=A||[$("scroller")];var C=D.getCoordinates(A);var G=$("dataset_area_attachment");if(!DATASET_BOUNDS_ATTACHMENT_INITIALIZED){$("scroller").adopt(G)}var E=120;var B=40;var F=15;G.setStyles({display:"block",position:"absolute",left:C.right,top:C.bottom-(E-B+(F/2)+(C.height/2))+$("scroller").getSize().scroll.y,zIndex:101});DATASET_BOUNDS_ATTACHMENT_INITIALIZED=true};var hideDatasetBoundsAttachment=function(){if(!DATASET_BOUNDS_ATTACHMENT_INITIALIZED){return }var A=$("dataset_area_attachment");A.setStyles({display:"none"})};var resetDatasetBoundsSelect=function(){if($("dataset_bounds").selectedIndex!==0){$("dataset_bounds").selectedIndex=0;$("dataset_bounds").fireEvent("change")}};var BOUNDS_WARNING_ELEMENT=null;var showMapBoundsWarning=function(E,A){if(BOUNDS_WARNING_ELEMENT===null){var D=E.getSize();BOUNDS_WARNING_ELEMENT=new Element("div",{styles:{width:D.width,height:50,backgroundColor:"yellow",display:"none",position:"absolute",left:0,top:0}});var C="This dataset is larger than the visible map area.";var B="The dataset area will be searched in its entirety.";new Element("div").setText(C).setStyles({fontSize:"14px",marginTop:"10px",fontWeight:"bold",marginLeft:"auto",marginRight:"auto",textAlign:"center"}).injectInside(BOUNDS_WARNING_ELEMENT);new Element("div").setText(B).setStyles({marginLeft:"auto",marginRight:"auto",textAlign:"center"}).injectInside(BOUNDS_WARNING_ELEMENT);setOpacity(BOUNDS_WARNING_ELEMENT,0.75);BOUNDS_WARNING_ELEMENT.injectInside(A)}else{if(BOUNDS_WARNING_ELEMENT.getParent()!=A){A.adopt(BOUNDS_WARNING_ELEMENT)}}BOUNDS_WARNING_ELEMENT.setStyles({display:"block"})};var hideMapBoundsWarning=function(){if(BOUNDS_WARNING_ELEMENT){BOUNDS_WARNING_ELEMENT.setStyles({display:"none"})}}
