var _execScript;if(window.execScript){_execScript=window.execScript}else{_execScript=function(A){window.setTimeout(A,0)}}function evalHTMLScript(D){var B,A;A=[];var C=/<script[^>]*>([\s\S]*?)<\/script>/gi;while((B=C.exec(D))){A.push(B[1])}A=A.join("\n");if(A){_execScript(A)}}var $startsWith=function(A,B){return(A.indexOf(B)==0)};var $endsWith=function(A,B){return(A.lastIndexOf(B)==(A.length-B.length))};var startswith=function(B,A){return(A.substr(0,B.length)==B)};var endswith=function(B,A){return(A.substr(A.length-B.length,B.length)==B)};var cleanString=function(A){if(typeof A!="undefined"){if($type(A)=="string"){if(startswith('"',A)&&endswith('"',A)){A=A.substring(1,A.length-1)}if(startswith("~",A)){A=A.substr(1)}}}return A};var updateSelectElement=function(A,B){if(typeof console!="undefined"){console.assert(A.getTag()=="select","this method only works on select elements")}if((typeof B=="undefined")||(B===null)){A.selectedIndex=0}else{$A(A.options).some(function(D){var C=(D.value==B);if(C){D.selected=true}return C})}};var ruleid=0;var AdvancedRule=new Class({options:{},initialize:function(A,B){this.id=ruleid;ruleid++;this.controller=A;this.node=$(A.options.template).clone(true);oldid=this.node.getProperty("id");this.node.setProperty("id",oldid+"_"+this.id);this.node.setStyles({display:"block"});if(B){this.model=B}else{this.model={}}this.hascal=false},setup:function(){if(typeof console!="undefined"){console.log("setting up advanced rule")}this.configure();if(typeof console!="undefined"){console.log("registering handlers")}this.registerhandlers();if(typeof console!="undefined"){console.log("done with setup")}},configure:function(){this.searchbutton=$E("span[id=_searchbutton]",this.node);this.minusbutton=$E("span[id=_minusbutton]",this.node);this.plusbutton=$E("span[id=_plusbutton]",this.node);this.textfield=$E("input[id=tfld]",this.node);if(typeof console!="undefined"){console.assert(this.searchbutton,"did not find search");console.assert(this.minusbutton,"did not find minus");console.assert(this.plusbutton,"did not find plus");console.assert(this.textfield,"did not find textfield");console.log("found all buttons & fields")}if(this.textfield){this.textfield.setProperty("id","tfld"+this.id);this.textfield.addEvent("keyup",function(A){A=new Event(A);if(A.key==="enter"){console.log("search button:",this.searchbutton);this.searchbutton.fireEvent("click")}}.bind(this))}},gettextfield:function(){return this.textfield},registerhandlers:function(){if(typeof console!="undefined"){console.log("registering")}var D,B,F;F=this.minusbutton;B=function(J,I){I.controller.remove(I)};F.addEvent("click",B.bindAsEventListener(F,this));F=this.plusbutton;B=function(J,I){I.controller.add()};F.addEvent("click",B.bindAsEventListener(F,this));F=this.searchbutton;B=function(J,I){I.controller.dosearch()};F.addEvent("click",B.bindAsEventListener(F,this));F=$(this.node).getElement("select[id=_attribute_select]");if(typeof console!="undefined"){console.log("e:",F)}D=function(J,I){I.model={attribute:this.options[this.selectedIndex].value,value:null,op:null};I.render()};F.addEvent("change",D.bindAsEventListener(F,this));F=this.node.getElement("select[id=_is_is_not_select]");if(typeof console!="undefined"){console.log("e:",F)}D=function(J,I){I.model.op=this.options[this.selectedIndex].value;I.render()};F.addEvent("change",D.bindAsEventListener(F,this));F=this.node.getElement("select[id=_on_before_after_select]");D=function(J,I){I.model.op=this.options[this.selectedIndex].value;I.render()};F.addEvent("change",D.bindAsEventListener(F,this));F=this.node.getElement("select[id=_eq_gt_lt_select]");D=function(J,I){I.model.op=this.options[this.selectedIndex].value;I.render()};F.addEvent("change",D.bindAsEventListener(F,this));F=this.node.getElement("select[id=_contains_select]");D=function(J,I){I.model.op=this.options[this.selectedIndex].value;I.render()};F.addEvent("change",D.bindAsEventListener(F,this));F=this.node.getElement("select[id=_intersects_select]");D=function(J,I){I.model.op=this.options[this.selectedIndex].value;I.render()};F.addEvent("change",D.bindAsEventListener(F,this));F=this.gettextfield();D=function(J,I){if(typeof console!="undefined"){console.log("fired changefunc for tfld")}I.model.value=this.value;I.render()};F.addEvent("change",D.bindAsEventListener(F,this));F=this.node.getElement("select[id=_type_select]");D=function(J,I){I.model.value=this.options[this.selectedIndex].value;I.render()};F.addEvent("change",D.bindAsEventListener(F,this));F=this.node.getElement("select[id=_rating_select]");D=function(J,I){I.model.value=this.options[this.selectedIndex].value;if(typeof console!="undefined"){console.log("this.value",this.value)}if(typeof console!="undefined"){console.log("options.value:",this.options[this.selectedIndex].value)}I.render()};F.addEvent("change",D.bindAsEventListener(F,this));var G=this.node.getElements("select[class=classifier_search]");var C;for(C=0;C<G.length;C++){F=G[C];D=function(J,I){I.model.value=this.options[this.selectedIndex].value;if(typeof console!="undefined"){console.log("this.value",this.value)}if(typeof console!="undefined"){console.log("options.value:",this.options[this.selectedIndex].value)}I.render()};F.addEvent("change",D.bindAsEventListener(F,this))}G=this.node.getElements("select[class=profile_select]");if(typeof console!="undefined"){console.log("es:",G)}var H=G.length;for(C=0;C<H;C++){F=G[C];D=function(J,I){I.model.value=this.options[this.selectedIndex].value;I.render()};F.addEvent("change",D.bindAsEventListener(F,this))}F=this.node.getElement("select[id=profile_topics_select]");D=function(J,I){I.model.value=this.options[this.selectedIndex].value;I.render()};F.addEvent("change",D.bindAsEventListener(F,this));var E=this;F=this.node.getElement("input[id=search_input]");F.addEvent("change",function(I){E.model.pretty_value='"'+F.value+'"'});var A=new AutocompleteConsbioJson(F,"/cbi/organization/find_organization_2",{"postVar":"prefix","onRequest":Class.empty,"injectChoice":function(I,J){var K=new Element("li").setHTML(this.markQueryValue(I[0]));K.inputValue=I[0];K.stashed={"uuid":I[1]};this.addChoiceEvents(K).injectInside(this.choices)},"ajaxOptions":{"method":"get"},"onShow":function(I,J){$("scroller").adopt(J)},"onHide":function(I,J){E.model.value=null},"onSelect":function(I){E.model.value=this.selected.stashed.uuid}})},render:function(){if(typeof console!="undefined"){console.log("rendering rule")}if(this.controller.rules.length==1){this.minusbutton.setStyle("visibility","hidden")}else{this.minusbutton.setStyle("visibility","visible")}if(this.controller.rules.indexOf(this)==0){this.plusbutton.setStyle("visibility","visible")}else{this.plusbutton.setStyle("visibility","hidden")}if(this.controller.rules.indexOf(this)==(this.controller.rules.length-1)){this.searchbutton.setStyle("visibility","visible")}else{this.searchbutton.setStyle("visibility","hidden")}var d=this.gettextfield();d.removeEvents("click");var U=$A(this.node.getElements("span[class=_options]"));if(typeof console!="undefined"){console.log("opts.length",U.length)}U.each(function(e){e.setStyle("display","none");e.getParent().setStyle("padding-left","0px")});var c=this.model.attribute;if(typeof console!="undefined"){console.log("Atr is "+c)}if($chk(c)){var Q=this.node.getElement("span[id=_is_is_not]");var H=this.node.getElement("span[id=_contains]");var R=this.node.getElement("span[id=_on_before_after]");var O=this.node.getElement("span[id=_eq_gt_lt]");var K=this.node.getElement("span[id=_intersects]");var a=this.node.getElement("span[id=_textfield]");var X=this.node.getElement("span[id=_type]");var V=this.node.getElement("span[id=_rating]");var T=this.node.getElement("span[id=_autocomplete_organization]");var W=this.controller.classifiers;var G=this.controller.profile_entries;if(W.contains(c)){if(typeof console!="undefined"){console.log("classifier_attrs: "+W)}if(typeof console!="undefined"){console.log("ATTR is "+c)}var M=this.node.getElement("span[id=_"+c+"]");Q.setStyle("display","inline");Q.parentNode.setStyle("padding-left","6px");M.setStyle("display","inline");M.parentNode.setStyle("padding-left","6px");if(this.model.op===null){this.model.op=Q.getChildren()[0].getValue()}if(this.model.value===null){this.model.value=M.getChildren()[0].getValue()}}else{if(G.map(function(e){return"profile-"+e}).contains(c)){if(typeof console!="undefined"){console.log("profile attrs: "+G)}if(typeof console!="undefined"){console.log("attr is: "+c)}var I;var D=c.substring("profile-".length);if(typeof console!="undefined"){console.log("el:",D)}if(typeof console!="undefined"){console.log("type:",this.controller.options.profile_types[D])}if(this.controller.options.profile_types[D]=="list_of_orgs"){T.setStyle("display","inline");T.parentNode.setStyle("padding-left","6px")}else{if(this.controller.options.profile_types[D]=="select_from"){var S=this.node.getElement("span[id=_select_from_"+D+"]");S.setStyle("display","inline");S.parentNode.setStyle("padding-left","6px")}else{if(this.controller.options.profile_types[D]=="topics"){var S=this.node.getElement("span[id=profile_topics]");S.setStyle("display","inline");S.parentNode.setStyle("padding-left","6px")}else{a.setStyle("display","inline");a.parentNode.setStyle("padding-left","6px")}}}switch(this.controller.options.profile_types[D]){case"list_of_orgs":case"select_from":case"topics":I=Q;break;default:I=H;break}I.setStyle("display","inline");I.parentNode.setStyle("padding-left","6px")}else{switch(c){case"author":Q.setStyle("display","inline");Q.parentNode.setStyle("padding-left","6px");a.setStyle("display","inline");a.parentNode.setStyle("padding-left","6px");break;case"created":case"lastmodified":R.setStyle("display","inline");R.parentNode.setStyle("padding-left","6px");a.setStyle("display","inline");a.parentNode.setStyle("padding-left","6px");d=this.gettextfield();if(!this.hascal){if(typeof console!="undefined"){console.log("creating options with on hide complete")}var F={};F[this.gettextfield().getProperty("id")]="M j Y";var B=new Calendar(F);B.fx.addEvent("onComplete",(function(){if(B.calendar.getStyle("opacity")==0){if(typeof console!="undefined"){console.log("my cal was hidden")}this.gettextfield().fireEvent("change",[null,this])}}).bind(this));d.addEvent("click",(function(){if(typeof console!="undefined"){console.log("you clicked on the date field")}}).bind(this));this.hascal=true}break;case"name":case"all":case"metadata":H.setStyle("display","inline");H.parentNode.setStyle("padding-left","6px");a.setStyle("display","inline");a.parentNode.setStyle("padding-left","6px");break;case"geoextents":K.setStyle("display","inline");K.parentNode.setStyle("padding-left","6px");var P=$E("span[id=_geoextents]",this.node);P.setStyle("display","inline");P.getParent().setStyle("padding-left","6px");d=$E("div[id=_geo_button]",this.node);d.removeEvents("click");if(!this.controller.options.gmapkey){d.setProperty("title","geo search is disabled in this view.");return }d.addEvent("click",function(){if(typeof console!="undefined"){console.log("this.model:",this.model)}var e=new APILoader(this.controller.options.gmapkey,{onLoad:function(){var j=positionGeoSearch("Search");var g=getGeoSearchInstance();g.setMode(InteractionMode.PAN);g.attachTo(j);var k;var f;k=function(){if(typeof console!="undefined"){console.log("submit handler")}var m=g.getSelection();if(m===null){this.model.value=null}else{this.model.value=m.join(" ")}g.hide();g.detach();g.removeEvent("onSubmit",k);g.removeEvent("onCancel",f)}.bind(this);f=function(){g.hide();g.detach();g.removeEvent("onSubmit",k);g.removeEvent("onCancel",f)}.bind(this);g.addEvent("onSubmit",k);g.addEvent("onCancel",f);g.show();if(this.model.value){var l=this.model.value.split(" ");if(typeof console!="undefined"){console.log("split:",l)}var i=l.map(function(m){return parseFloat(m)});if(typeof console!="undefined"){console.log("float:",i)}var h=new GLatLngBounds(new GLatLng(i[0],i[1]),new GLatLng(i[2],i[3]));g.setSelection(h)}else{g.clearMasks();g.setMode(InteractionMode.PAN);g.dragging_start=null;g.dragging_end=null;g.updateSelection()}}.bind(this)})}.bind(this));break;case"type":Q.setStyle("display","inline");Q.parentNode.setStyle("padding-left","6px");X.setStyle("display","inline");X.parentNode.setStyle("padding-left","6px");break;case"rating":O.setStyle("display","inline");O.parentNode.setStyle("padding-left","6px");V.setStyle("display","inline");V.parentNode.setStyle("padding-left","6px");break;default:break}}}var J=this.model;var D;d=$E("select[id=_attribute_select]",this.node);$A(d.options).some(function(f){var e=(f.value==J.attribute);if(e){f.selected=true}return e});if(W.contains(this.model.attribute)){D=$E("select[id=_is_is_not_select]",this.node);updateSelectElement(D,J.op);D=this.node.getElement("span[id=_"+this.model.attribute+"]");D=D.getElement("select");updateSelectElement(D,J.value)}else{if(G.map(function(e){return"profile-"+e}).contains(this.model.attribute)){var A=null;var D=this.model.attribute.substring("profile-".length);switch(this.controller.options.profile_types[D]){case"list_of_orgs":case"select_from":case"topics":A=this.node.getElement("select[id=_is_is_not_select]");break;default:A=this.node.getElement("select[id=_contains_select]");break}updateSelectElement(A,J.op);if(this.model.value){var Z;switch(this.controller.options.profile_types[D]){case"list_of_orgs":Z=this.node.getElement("input[id=search_input]");Z.value=this.model.value;break;case"select_from":var L=this.node.getElement("span[id=_select_from_"+D+"]");Z=L.getElement("select[class=profile_select]");updateSelectElement(Z,cleanString(this.model.value));break;case"topics":Z=this.node.getElement("select[id=profile_topics_select]");updateSelectElement(Z,cleanString(this.model.value));break;default:break}}}else{switch(this.model.attribute){case"geoextents":D=this.node.getElement("select[id=_intersects_select]");updateSelectElement(D,J.op);break;case"all":case"name":case"metadata":D=$E("select[id=_contains_select]",this.node);updateSelectElement(D,J.op);break;case"author":case"type":D=$E("select[id=_is_is_not_select]",this.node);updateSelectElement(D,J.op);break;case"rating":D=$E("select[id=_eq_gt_lt_select]",this.node);updateSelectElement(D,J.op);break;case"created":case"lastmodified":D=$E("select[id=_on_before_after_select]",this.node);updateSelectElement(D,J.op);break;default:if(typeof console!="undefined"){console.assert(false,"unhandled attribute type: "+Json.toString(this.model))}break}switch(this.model.attribute){case"type":D=$E("select[id=_type_select]",this.node);updateSelectElement(D,J.value);break;case"rating":D=$E("select[id=_rating_select]",this.node);updateSelectElement(D,J.value);break;case"created":case"lastmodified":var Y=/[0-9]{8}/;if(J.value&&J.value.match(Y)){var N=parseInt(J.value.slice(0,4));var E=parseInt(J.value.slice(4,6))-1;var C=parseInt(J.value.slice(6));var b=new Date(N,E,C);J.value=b.toDateString().slice(4)}break;default:break}}}d=this.gettextfield();if(J.value===null){d.value=""}else{d.value=J.value}}if(typeof console!="undefined"){console.log("rendered rule")}},getnode:function(){return this.node},getmodel:function(){return this.model}});AdvancedRule.implement(new Options());var STANDARD_OPTIONS=["author","rating","all","name","metadata","created","lastmodified","type","geoextents"];var AdvancedSearchControl=new Class({options:{gmapkey:null,template:null,url:"/cbi/search/formatquery",onUpdate:Class.empty,onEdit:Class.empty,onDismiss:Class.empty,classifiers:[],profile:[],profile_types:{},include_options:null,literal_queries:null},initialize:function(B,D,C){this.setOptions(C);this.results=$(D);this.sort_order=$("sort_order");this.sort_direction=$("_sort_direction");this.rulesnode=$(B);this.rules=[];this.classifiers=[];var A=0;for(A=0;A<this.options.classifiers.length;A++){this.classifiers[A]=this.options.classifiers[A].toLowerCase()}this.profile_entries=this.options.profile.map(function(E){return E.toLowerCase()});if(typeof console!="undefined"){console.assert(this.rulesnode,"could not find rules node")}if(typeof console!="undefined"){console.assert(this.results,"could not find results container")}if(typeof console!="undefined"){console.assert(this.options.template)}this.options.template=$(this.options.template).clone(true);if(typeof console!="undefined"){console.log("configuring search control")}this.configure();if(typeof console!="undefined"){console.log("registering handlers")}this.registerhandlers();this.ajax=null;this.geoFilter=null},isInert:function(){if(typeof console!="undefined"){console.log(this.rulesnode.getStyle("display"))}return(this.rulesnode.getStyle("display")!="block")},configure:function(){if(this.options.include_options===null){return }var A,C,D,E;D=this.options.include_options.map(function(F){return F.toLowerCase()});A=this.options.template;C=$E("select[id=_attribute_select]",A);for(var B=C.length-1;B>=0;B--){E=C.options[B].value;if(E){E=E.toLowerCase()}if(E=="-- choose --"){continue}if(!D.contains(E)){$(C.options[B]).remove()}}if(typeof console!="undefined"){if(typeof console!="undefined"){console.log("tidied up the select node")}}},dismiss:function(){this.fireEvent("onDismiss")},registerhandlers:function(){if(typeof console!="undefined"){console.log("handler 1, sort order:",this.sort_order)}if(this.sort_order){if(typeof console!="undefined"){console.log("found sort_order")}this.sort_order.addEvent("change",this.dosearch.bindWithEvent(this))}else{if(typeof console!="undefined"){console.log("did not find sort order")}if(typeof console!="undefined"){console.log("sort_order is not defined.")}}},dosearch:function(A){if(this.isInert()){if(typeof console!="undefined"){if(typeof console!="undefined"){console.warn("is INERT")}}return }var B=this.rules.map(function(H,K){var E=H.getmodel();if($chk(E.attribute)&&$chk(E.op)&&($chk(E.value)||$chk(E.pretty_value))){var L=[E.attribute,E.op];var J;if(E.attribute=="geoextents"){if(typeof console!="undefined"){console.log("Initial geoextents value: "+E.value)}J=E.value.trim().split(" ").map(function(M){if(M[0]=="["){M=M.substr(1)}if(typeof console!="undefined"){console.log("w: "+M)}return M.toFloat()});if(typeof console!="undefined"){console.log("Final: "+J)}L.push(J)}else{if((E.attribute=="created")||(E.attribute=="lastmodified")){var G=Date.parse(E.value);if(isNaN(G)){return null}var C=new Date();C.setTime(G);var F=C.getFullYear();var D=C.getMonth()+1;var I=C.getDate();if(D<10){D="0"+D}if(I<10){I="0"+I}J=""+F+D+I;L.push(J)}else{if($chk(E.value)){L.push(E.value)}else{if($chk(E.pretty_value)){L.push(E.pretty_value)}else{if(typeof console!="undefined"){console.error("got into an illegal state; neiher model.value nor model.pretty_value was set.")}}}}}return L}else{return null}});B=B.filter(function(C){return $chk(C)});if(this.options.literal_queries!==null){B.extend(this.options.literal_queries)}if(!this.sort_order){}else{if((this.sort_order.options[this.sort_order.selectedIndex].value=="author_sort")||(this.sort_order.options[this.sort_order.selectedIndex].value=="name_sort")){B.push(["sort_order","desc",this.sort_order.options[this.sort_order.selectedIndex].value])}else{B.push(["sort_order","asc",this.sort_order.options[this.sort_order.selectedIndex].value])}}if(typeof console!="undefined"){console.log("query:",Json.toString(B))}if(this.ajax===null){this.ajax=new XHR({method:"post",onSuccess:function(D){var F=this.updateresults(D);this.fireEvent("onUpdate",F);ellipsizeAll();var E=$$("div.column1 a");if(E){var C=new Tips(E,{className:"mytool"})}}.bind(this),autoCancel:true})}document.body.style.cursor="wait";this.ajax.send(this.options.url,Json.toString(B))},add:function(){var A=new AdvancedRule(this);this.rules.unshift(A);A.getnode().injectTop(this.rulesnode);A.setup();this.renderall()},addLiteral:function(A){if((A.attribute!=="")&&(this.options.include_options!==null)){var C=this.options.include_options.map(function(D){return D.toLowerCase()});if(!C.contains(A.attribute.toLowerCase())){return }}var B=new AdvancedRule(this,A);this.rules.push(B);B.getnode().injectInside(this.rulesnode);B.setup();this.renderall()},remove:function(A){this.rules.remove(A);A.getnode().remove();this.renderall()},renderall:function(){for(var A=0;A<this.rules.length;A++){this.rules[A].render()}},updateresults:function(A){document.body.style.cursor="default";if(typeof console!="undefined"){console.log("updating results")}var B=Json.evaluate(A)[0];evalHTMLScript(B[2]);this.results.setHTML(B[2]);var D=B[0];var C;if((D==0)||(D>1)){C=D+" Results"}else{C="1 Result"}if($("num_results")){$("num_results").setText(C)}if($("rss_link")){$("rss_link").href=B[1]}return D}});AdvancedSearchControl.implement(new Options(),new Events());AdvancedSearchControl.standardOptions=STANDARD_OPTIONS