var debug=debug||{enabled:false,log:function(){if(this.enabled&&typeof console!=="undefined"){var C=arguments;var B=arguments.length;if(B===1){console.log(C[0])}if(B===2){console.log(C[0],C[1])}if(B===3){console.log(C[0],C[1],C[2])}if(B===4){console.log(C[0],C[1],C[2],C[3])}if(B===5){console.log(C[0],C[1],C[2],C[3],C[4])}}}};debug.enabled=true;var update_layer_zoom_levels;var ZL_MIN_ZOOM=0;var ZL_MAX_ZOOM=20;var CONSTANTS={instructions:new Element("div",{"class":"zl_instructions"}).setHTML('You can set rules to only be visible at certain zoom levels. Set the zoom controls for each rule to set in what range the rule is visible. 0 shows the whole world, 20 is very close up, and the map is currently zoomed to level <span class="current-zoom">x</span>.')};var ZoomLevelComponent=new Class({options:{onUpdate:Class.empty},initialize:function(B,A){this.e=B;this.uuid=A;this.check_model();GEvent.addListener(mapManager.map,"zoomend",function(){var C=$E(".current-zoom");if(C){C.setHTML(mapManager.map.getZoom())}})},check_model:function(){var A="/cbi/visualize/get_session_settings?uuid="+this.uuid+"&t="+(new Date()).getTime();var B=new Ajax(A,{method:"get",onComplete:function(C){var D=Json.evaluate(C)[0];this.redraw(D);this.update_indexes(D);update_layer_zoom_levels();this.fireEvent("onUpdate")}.bind(this)});B.request()},get_zoom_levels:function(C){var B=[];for(var A in C){if(C.hasOwnProperty(A)){B[B.length]=C[A]}}B.sort(function(E,D){return E["z-layer"]-D["z-layer"]});return B},layers:{},indexes:{},update_indexes:function(B){var F,E,D=0;for(var C=0;C<pageManager.layerGroups.length;C++){F=pageManager.layerGroups[C];for(var A=0;A<F.layers.length;A++){E=F.layers[A];this.indexes[E.id]=D;this.layers[E.id]=E;D+=1}}debug.log("indexes:",this.indexes)},make_zoom_control:function(C,F){var E=this;var A=new Element("select");A.addClass("zl_select");var D;A.setProperty("name",C);var B;for(B=ZL_MIN_ZOOM;B<=ZL_MAX_ZOOM;B++){D=new Element("option");if($defined(F)&&F===B){D.setProperty("selected","selected")}D.setProperty("value",B);D.innerHTML=B;D.injectInside(A)}A.onchange=function(){E.set_zoom();update_layer_zoom_levels();E.fireEvent("onUpdate")};return A},set_zoom:function(){var C=get_zoom_levels_state();C=[this.uuid,C];var A="/cbi/visualize/set_zoom_levels";var B=new Ajax(A,{"data":Json.toString(C),onComplete:function(){}});B.request()},setHeight:function(A){},redraw:function(D){this.e.empty();var E=CONSTANTS.instructions.clone();E.injectInside(this.e);$E(".current-zoom").setHTML(mapManager.map.getZoom());D=this.get_zoom_levels(D);debug.log("settings:",D);var H;var N;var B;var A;var K;var G;var J;var L;var F;var M;var I;var C;if(D&&D.length){C=new Element("div",{"id":"nullsoft","styles":{"overflow":"auto"}}).injectInside(this.e);for(H=0;H<D.length;H++){N=D[H];if(N.dataset!==B){B=N.dataset;A=new Element("div",{"class":"zl_dataset_rule"});A.injectInside(C);K=new Element("div",{"class":"zl_dataset_name"});K.innerHTML='<a target="'+B+'" href="/cbi/dataset/datasetPreviewPage?uuid='+B+'">'+N["__dataset_name__"]+"</a>";K.injectInside(A);G=1}M=N["key"];I=N["zoom_levels"];if(!$defined(I)){I=[ZL_MIN_ZOOM,ZL_MAX_ZOOM]}J=new Element("div");J.addClass("zl_rule");J.injectInside(A);L=new Element("span");L.innerHTML="Rule "+G+": ";L.injectInside(J);F=this.make_zoom_control("min-"+M,I[0]);F.injectInside(J);L=new Element("span");L.innerHTML=" to ";L.injectInside(J);F=this.make_zoom_control("max-"+M,I[1]);F.injectInside(J);G++}zlc.resize()}},inputs:[]});ZoomLevelComponent.implement(new Options(),new Events());var ZoomLevelController=MapManipulator.extend({options:{},initialize:function(){this._create_component()},update:function(){},handle_detach:function(){},resize:function(){if(this.bIn){var B=$(mapManager.map.getContainer()).getCoordinates([$("renderingFrame")]);var C=$("nullsoft").getCoordinates([$("renderingFrame")]);var A=(B.top+B.height)-(C.top+C.height);$("nullsoft").setStyle("height",C.height+A)}},handle_attach:function(){this.fx=new Fx.Styles(this.dom,{duration:1500,transitions:Fx.Transitions.linear,onComplete:function(){this.resize()}.bind(this)});this.handle.onclick=function(){if(!this.bIn){zl_update()}this.bIn=!this.bIn;if(this.bIn){this.fx.start({top:(-1)*$("map_canvas").getCoordinates().height})}else{this.fx.start({top:-10})}}.bind(this);this.dom.setStyles({visibility:"hidden"});this.dom.injectAfter($("map_canvas"));this.dom.setStyles({"height":$("map_canvas").getCoordinates().height,"visibility":"visible"})},_create_component:function(){this.dom=new Element("div");this.dom.addClass("zl_component");this.dom.addClass("zl_transparent");this.handle=new Element("div");this.handle.addClass("zl_handle");this.handle.injectInside(this.dom);this.handle.addEvent("click",function(){this.toggleClass("activated")}.bind(this.handle));this.rules=new Element("div");this.rules.addClass("zl_rule_container");this.rules.injectInside(this.dom)},handleAttach:this.handle_attach,handleDetach:this.handle_detach,rules:null,bIn:false,handle:null,dom:null,fx:null});ZoomLevelComponent.implement(new Options(),new Events());var zlc;var zl_comp;function zl_init(){if((typeof pageManager==="undefined")||(typeof mapManager==="undefined")){throw"you must call zl_init after pagemanager & mapmanager are set up"}zlc=new ZoomLevelController();zlc.handle_attach(mapManager.map);zl_comp=new ZoomLevelComponent(zlc.rules,pageManager.concept);mapManager.addEvent("onRefresh",function(){zl_comp.check_model()})}function zl_update(){if(!(zlc&&mapManager)){throw"you must call zl_update after zl_init"}var A=$(mapManager.map.getContainer()).getCoordinates().height;zlc.dom.setStyles({height:A,top:(zlc.bIn)?-A:-10});zlc.resize()}function get_zoom_levels_state_by_index(B,A){throw"this method is deprecated; please use get_zoom_levels_state_by_index_two"}function get_zoom_levels_state(){var B=document.getElements("select.zl_select");var D;var C={};var A;for(A=0;A<B.length;A++){D=B[A].getProperty("name").split("-");if(!$defined(C[D[1]])){C[D[1]]=[null,null]}if(D[0]==="min"){C[D[1]][0]=parseInt(B[A].value,10)}else{if(D[0]==="max"){C[D[1]][1]=parseInt(B[A].value,10)}}}return C}function get_zoom_levels_state_two(){var C=document.getElements("select.zl_select");if((C.length%2)!==0){throw"invalid number of zl_selects"}var E;var D=[];var B;for(B=0;B<C.length;B+=2){var A,F;A=C[B].value.toInt();F=C[B+1].value.toInt();D.push([A,F])}return D}function get_zoom_levels_state_by_index_two(B,A){if(typeof zl_comp.indexes[A]==="undefined"){return[ZL_MIN_ZOOM,ZL_MAX_ZOOM]}else{var C=get_zoom_levels_state_two();return C[zl_comp.indexes[A]]}}update_layer_zoom_levels=function(){debug.log("updating layer zoom levels");var D,E,C,B;for(var A in zl_comp.layers){if(zl_comp.layers.hasOwnProperty(A)){E=get_zoom_levels_state_by_index_two(null,A);debug.log("handling: ",A,E);C=mapManager.layerid_to_overlay[A];if(!C){continue}B=C.getTileLayer();if((B.minResolution()!==E[0])||B.maxResolution()!==E[1]){(function(H,G,F){H.minResolution=function(){return G};H.maxResolution=function(){return F}})(B,E[0],E[1]);C.refresh()}else{}}}};var monitor_zoom_instance=monitor_zoom_instance||{update:null};function monitor_zoom_levels(A,E,G,F,J,B){if(B==="full"&&getBrowser()==="ie7"){return }debug.log("monitoring");debug.log("monitoring:",E,G,F,J,B);if($type(E())!=="array"){throw"tile_layers has wrong return type"}if($type(G())!=="array"){throw"scrimmable has wrong return type"}var C=[];var D=function(N,M){var L=N.getCoordinates();debug.log("e coords:",N,Json.toString(L));if(B==="full"){return new Element("div",{"class":"__monitor_zoom_scrim__","styles":{"position":"absolute","top":0,"left":-30,"width":225,"height":"100%","zIndex":5}})}return new Element("div",{"class":"__monitor_zoom_scrim__","styles":{"position":"absolute","top":0,"left":0,"width":L.width,"height":"100%","zIndex":5}}).setText("This rule is not visible at this zoom level")};var H=function(M){for(var L=0;L<C.length;L++){if(C[L][0]===M){return C[L][1]}}};var I=function(N,M){for(var L=0;L<C.length;L++){if(C[L][0]===N){C[L][1]=M;return }}C.push([N,M])};var K=function(L){debug.log("************ mzl: map zoom changed:",L);debug.log("tl bounds:",E().map(function(V){return[V.minResolution(),V.maxResolution()]}));debug.log("el:",G().map(function(V){return V}));var R=E();var Q=G();var T=J();var M,U,S,O;if(R.length!==Q.length){debug.log("layers & elements did not have the same length!");return }if(R.length!==T.length){throw"layers & parents did not have the same length!"}for(var P=0,N=R.length;P<N;P++){U=R[P];M=Q[P];O=T[P];O.setStyle("position","relative");if((L>=U.minResolution())&&(L<=U.maxResolution())){S=H(M);if(S&&S.getParent()){S.remove();I(M,null)}}else{S=H(M);if(!S){S=D(M,F);I(M,S);if(O){S.injectInside(O)}else{S.injectInside(document.body)}debug.log("injecting scrim:",S)}}}};monitor_zoom_instance.update=K;if(zl_comp){zl_comp.addEvent("onUpdate",function(){debug.log("zl comp fired on update: zoom is",A.getZoom());K(A.getZoom())})}else{debug.log("no zl_comp")}GEvent.addListener(A,"zoomend",function(L,M){debug.log("zoomend on map isntance");K(M)})}
