





// load the Dojo framework
function _gLoadSearchFramework1283443924319() {
  var frameworkUrl = "http://serverapi.arcgisonline.com/jsapi/arcgis/?v=1.3";
  var hasFramework = false || (typeof dojo != "undefined");
  if (!hasFramework && (frameworkUrl != null) && (frameworkUrl.length > 0)) {
  
    // this works in Firefix 3, in IE7 it seems to mess up Dojo's knowledge of
    // when the page is loaded and the dojo.addOnLoad function below never fires
    var elScript = document.createElement("script");
    elScript.src = frameworkUrl;
    elScript.type = "text/javascript";
    var elHead = document.getElementsByTagName("head").item(0);
    elHead.appendChild(elScript);
    
    // wait for the load
    var count = 0;
    var checkLoad = function() {
      count++;
      var hasDojo = (typeof dojo != "undefined");
      if (!hasDojo && (count < 600)) {
        setTimeout(checkLoad,100); 
      } else if (!hasDojo) {
        alert("Search geodata.gov\nThe following script failed to load:\n"+frameworkUrl);
      } else {
        _gSearchStartup1283443924319();
      }
    }
    setTimeout(checkLoad,100);  
    
  } else {
    _gSearchStartup1283443924319();
  }
};

// load the CSS style file
function _gLoadSearchStyle1283443924319() {
  var cssUrl = "http://www.geodata.gov/widgets/widgets.css";
  if ((cssUrl != null) && (cssUrl.length > 0)) {
    var elLink = document.createElement("link");
    elLink.rel= "stylesheet";
    elLink.ttpe = "text/css";
    elLink.href = cssUrl;
    var elHead = document.getElementsByTagName("head").item(0);
    elHead.appendChild(elLink);
  }
};
_gLoadSearchStyle1283443924319();
_gLoadSearchFramework1283443924319();

function _gSearchStartup1283443924319() {
  dojo.require("dijit._Widget");
  dojo.require('dojox.layout.FloatingPane');
  dojo.require("dojo.io.script");
  
  dojo.addOnLoad(function(){

    // default configuration
    var _gSearchNamespace = "gos";
    var _gSearchRequestPath = "http://www.geodata.gov/widgets/searchjs.jsp";  
    
    var _gSearchParams = {
      dockable: false,
      closable: true, 
      maxable: false,
      resizable: true,
      
      anchorButtonId: _gSearchNamespace+"-search-button",
      anchorTextId: _gSearchNamespace+"-search-text",
      anchor: "",
      caption: "Search geodata.gov",
      configVariable: _gSearchNamespace+"SearchCfg",
      cssNamespace: "gos",
      imagesUrl: "http://www.geodata.gov/widgets/images",
      info: "<a href=\"javascript:void(0);\" onclick=\"dijit.byId('gos-search-pane').search()\">Back to Results</a><h4>About Geospatial One-Stop (GOS) Search Widget</h4><hr/><p>The Geospatial One-Stop Search Widget enables users to discover, view, and access geospatial information within the Geospatial One-Stop portal <a href='http://www.geodata.gov' target='_blank'>(http://www.geodata.gov)</a>. The user can preview live map services, view formatted detailed metadata, or view the raw metadata in xml format. Users also have the option to view search results as an RSS Feed or save the results as a KML file.</p><p>For instructions on installing the GOS Search Widget or to view a user guide please visit <a href='http://www.geodata.gov/wps/portal/gos/Help_Center/GOSSearchWidget' target='_blank'>http://www.geodata.gov/wps/portal/gos/Help_Center/GOSSearchWidget</a></p><h5>About Geospatial One-Stop (GOS)</h5><hr/><p>Geospatial One-Stop is one of 24 E-Government initiatives sponsored by the Federal Office of Management and Budget (OMB) to facilitate communication and sharing of geographic data and resources to enhance government efficiency and improve citizen services by making it easier, faster, and less expensive for all levels of government and the public to access geospatial information.  Geospatial One-Stop is managed by the U.S. Geological Survey's National Geospatial Program (NGP) office.</p><p>For more information about GOS, visit <a href='http://www.geodata.gov' target='_blank'>http://www.geodata.gov</a></p>",
      label: "Find:",
      max: 30,
      msgNoMatch: "No matching records were located.",
      preferredTop: 50,
      
      propContentType: "",
      propDataCategory: "",
      propBBox: "-76.522801,38.303970,-74.273818,39.990708",
      propSpatialRel: "esriSpatialRelWithin",
      propAfter: "",
      propBefore: "",
      propValidAfter: "",
      propValidBefore: "",
      propIsPartOf: "",
      propPublisher: "",
      propSource: "",
      propOrderBy: "",
  
      restUrl: "http://catalog.geodata.gov/geoportal/rest/find/document",
      searchProxyUrl: "http://www.geodata.gov/widgets/searchProxy.jsp",
      tipClose: "Close",
      tipInfo: "About",
      tipKML: "KML File",
      tipRSS: "RSS Feed",
      tipSearch: "Search",
      title: "Search geodata.gov"
      
    };
    
    // function to build the search pane widget
    var _gBuildSearchPane = function(params) {
      var nd = _gInitializeSearchPaneNode(params);
      var anchor = new gos.SearchAnchor(params,null);
      anchor.startup();
      var pane = new gos.SearchPane(params,nd);
      pane.startup();
    }
    
    // declare the anchor
    dojo.provide("gos.SearchAnchor");
    dojo.declare("gos.SearchAnchor",dijit._Widget,{
    
      // build the inner controls associated with the widget
      build: function() {
      
        // find the script element
        var insertionNode = null;
        dojo.query("script").forEach(function(item) {
          if (item.src.indexOf(_gSearchRequestPath) == 0) {
            if (insertionNode == null) insertionNode = item;
          }
        });
        
        if (insertionNode == null) {
          alert(this.caption+"\nUnable to find widget insertion point.");
        }
            
        if (this.anchor == "link") {
          var elLink = document.createElement("a");
          elLink.id = this.anchorButtonId;
          elLink.setAttribute("href","javascript:void(0);");
          elLink.appendChild(document.createTextNode(this.caption));
          insertionNode.parentNode.insertBefore(elLink,insertionNode);
          
        } else if (this.anchor == "button") {
          var elBtn = document.createElement("button");
          elBtn.className = this.cssNamespace+"-search-anchor-button";
          elBtn.id = this.anchorButtonId;
          elBtn.appendChild(document.createTextNode(this.caption));
          insertionNode.parentNode.insertBefore(elBtn,insertionNode);
          
        } else if (this.anchor == "fieldset") {
         
          var elSet = document.createElement("fieldset");
          elSet.className = this.cssNamespace+"-search-anchor";
          insertionNode.parentNode.insertBefore(elSet,insertionNode);
          
            var elLbl = document.createElement("label");
            elLbl.setAttribute("for",this.anchorTextId);
            elLbl.appendChild(document.createTextNode(this.caption));
            elSet.appendChild(elLbl);
       
            var elGrp = document.createElement("div");
            elSet.appendChild(elGrp);
            
            var elTxt = document.createElement("input");
            elTxt.id = this.anchorTextId;
            elTxt.type = "text"
            elTxt.size = 30;
            elTxt.maxlength = 1024;
            elGrp.appendChild(elTxt);
          
            var elImg = document.createElement("img");
            elImg.id = this.anchorButtonId;
            elImg.src = this.imagesUrl+"/search.png";
            elImg.title = this.tipSearch;
            elImg.alt = this.tipSearch;
            elGrp.appendChild(elImg);
        }
        
      },
    
      // override, startup
      startup: function() {
        if (this._started) return;
        this.inherited("startup",arguments);
        if (this.anchor != "none") {
          if ((this.anchor != "link") && (this.anchor != "button") && 
              (this.anchor != "fieldset")) {
            this.anchor = "fieldset";
          }
          this.build();
        }
      },
    
      // override, post create
      postCreate: function() {  
        this.inherited("postCreate",arguments);   
      }
    });
       
    // declare the widget as a FloatingPane extension
    dojo.provide("gos.SearchPane");
    dojo.declare("gos.SearchPane",dojox.layout.FloatingPane,{
    
      anchorButtonId: "",
      anchorTextId: "",
      caption: "",
      configVariable: "",
      cssNamespace: "",
      imagesUrl: "",
      info: "",
      msgNoMatch: "",
      preferredTop: 50,
      restUrl: "",
      searchProxyUrl: "",
      tipClose: "",
      tipInfo: "",
      tipKML: "",
      tipRSS: "",
      tipSearch: "",
      
      _anchoredTextNode: null,
      _wasRepositioned: false,
       
      // return a KML response associated with the query
      asKML: function() {
        window.open(this.makeRestUrl("kml"),"_blank");
      },
      
      // return an RSS response associated with the query
      asRSS: function() {
        window.open(this.makeRestUrl("georss"));
      }, 
    
      // build the inner controls associated with the widget
      build: function() {
        
        var makeIcon = function(src,title) {
          var elImg = document.createElement("img");
          elImg.src = src;
          elImg.title = title;
          elImg.alt = title;
          return elImg;
        };
        
        var thisId = this.id;
        var idPfx = this.id;
        var cssPfx = this.cssNamespace+"-search-pane";    
        var elBody = document.createElement("div");
        elBody.id = idPfx+"-body";
        elBody.className = cssPfx+"-body";
        this.setContent(elBody);
        
          var elControl = document.createElement("div");
          elControl.id = idPfx+"-control";
          elControl.className = cssPfx+"-control";
          elBody.appendChild(elControl);
          
            var elLabel = document.createElement("label");
            elLabel.id = idPfx+"-label";
            elLabel.setAttribute("for",idPfx+"-text");
            elLabel.appendChild(document.createTextNode(this.label));
            elControl.appendChild(elLabel);
           
            var elText = document.createElement("input");
            elText.id = idPfx+"-text";
            elText.type = "text"
            elText.size = 40;
            elText.maxlength = 1024;
            elControl.appendChild(elText);
            dojo.connect(elText,"onkeypress",this,"onKeyPressText");
           
            var elBtn = elBtn = makeIcon(this.imagesUrl+"/search.png",this.tipSearch);
            elControl.appendChild(elBtn);
            dojo.connect(elBtn,"onclick",this,"search");
            
            var elSep = document.createElement("span");
            elSep.innerHTML = "&nbsp;&nbsp;&nbsp;";
            elControl.appendChild(elSep);
                    
            elBtn = makeIcon(this.imagesUrl+"/rss.png",this.tipRSS);
            elControl.appendChild(elBtn);
            dojo.connect(elBtn,"onclick",this,"asRSS");
            
            elBtn = makeIcon("http://www.google.com/earth/images/google_earth_link.gif",this.tipKML);
            elControl.appendChild(elBtn);
            dojo.connect(elBtn,"onclick",this,"asKML");
  
            elBtn = makeIcon(this.imagesUrl+"/info.png",this.tipInfo);
            elControl.appendChild(elBtn);
            dojo.connect(elBtn,"onclick",this,"showInfo");
            
            elBtn = makeIcon(this.imagesUrl+"/loading.gif","");
            elBtn.id = idPfx+"-loading";
            elBtn.style.visibility = "hidden";
            elControl.appendChild(elBtn);
            dojo.connect(elBtn,"onclick",this,"showInfo");
            
            dojo.query("span.dojoxFloatingCloseIcon",this.domNode).forEach(function(item) {
              item.title = dijit.byId(thisId).tipClose;
            });
                
          var elResults = document.createElement("div");
          elResults.id = idPfx+"-results";
          elResults.className = cssPfx+"-results";
          elBody.appendChild(elResults);
              
        // connect all anchor text boxes to the onKeyPressText function
        // i.e. all elements where: id=this.anchorTextId or name=this.anchorTextId 
        dojo.query("[id='"+this.anchorTextId+"'], [name='"+this.anchorTextId+"']").forEach(function(item) {
          dojo.connect(item,"onkeypress",dijit.byId(thisId),"onKeyPressText");
        });
              
        // connect all anchor buttons to the onAnchorButtonClicked function
        // i.e. all elements where: id=this.anchorButtonId or name=this.anchorButtonId 
        dojo.query("[id='"+this.anchorButtonId+"'], [name='"+this.anchorButtonId+"']").forEach(function(item) {
          dojo.connect(item,"onclick",dijit.byId(thisId),"onAnchorButtonClicked");
        });
              
      },
      
      // override, cllose the widget
      close: function() {
        this.hide(null);
      },
      
      // override, comment out this.domNode.style.display = "none";
      hide: function(/* Function? */ callback){
        dojo.fadeOut({
          node:this.domNode,
          duration:this.duration,
          onEnd: dojo.hitch(this,function() {
            //this.domNode.style.display = "none";
            this.domNode.style.visibility = "hidden";
            if(this.dockTo && this.dockable){
              this.dockTo._positionDock(null);
            }
            if (typeof callback == "function") {callback();}
          })
        }).play();
      },
      
      // make the rest url
      makeRestUrl: function(fmt) {
      
        var appendParam = function(url,name,value) {
          if ((value != null) && (value.length > 0)) {
            url += "&"+name+"="+escape(dojo.trim(value));
          }
          return url;
        }
        
        var sUrl = this.restUrl;
        if (sUrl.indexOf("?") != -1) {
          sUrl = sUrl.substring(0,sUrl.indexOf("?"));
        }
        sUrl += "?f="+escape(fmt);
        
        var nMax = 30;
        if ((typeof this.max == "number") && (this.max > 0) && (this.max < 100)) {
          nMax = this.max;
        }
        sUrl += "&max="+escape(nMax);
        
        var sText = this.validateText();
        if (sText.length > 0) {
          sUrl += "&searchText="+escape(sText);  
        }
        
        sUrl = appendParam(sUrl,"contentType",this.propContentType);
        sUrl = appendParam(sUrl,"dataCategory",this.propDataCategory);
        sUrl = appendParam(sUrl,"bbox",this.propBBox);
        sUrl = appendParam(sUrl,"spatialRel",this.propSpatialRel);
        sUrl = appendParam(sUrl,"after",this.propAfter);
        sUrl = appendParam(sUrl,"before",this.propBefore);
        sUrl = appendParam(sUrl,"validAfter",this.propValidAfter);
        sUrl = appendParam(sUrl,"validBefore",this.propValidBefore);  
        sUrl = appendParam(sUrl,"isPartOf",this.propIsPartOf);
        sUrl = appendParam(sUrl,"publisher",this.propPublisher);
        sUrl = appendParam(sUrl,"source",this.propSource);
        sUrl = appendParam(sUrl,"orderBy",this.propOrderBy);

        return sUrl; 
      },
                     
      // handle a click on an achor button
      onAnchorButtonClicked: function(e) {
        this.open();
        var elAnchorText = null;
        
        var queryAnchor = function(anchorTextId,qScope) {
          var el = null;
          var qStr = "[id='"+anchorTextId+"'], [name='"+anchorTextId+"']";
          dojo.query(qStr,qScope).forEach(function(item) { 
            if (el == null) {
              if ((typeof item.value != "undefined") && (item.value != null)) el = item;
            }
          });
          return el;
        };
        
        // look for a close search text anchor
        if (!e) e = window.event;
        if (e) {
          var target = (window.event) ? e.srcElement : e.target;
          elAnchorText = queryAnchor(this.anchorTextId,target.parentNode);
          if (elAnchorText == null) {
            elAnchorText = queryAnchor(this.anchorTextId,target.parentNode.parentNode);
          }
        }
        if (elAnchorText == null) {
          elAnchorText = queryAnchor(this.anchorTextId,null);
        }
        
        if (elAnchorText != null) {
          this._anchoredTextNode = elAnchorText;
          dojo.byId(this.id+"-text").value = dojo.trim(elAnchorText.value);;
          this.search();
        }
      },
      
      // handle a key press within a search text box
      onKeyPressText: function(e) {
        if (!e) e = window.event;
        if (e) {
          var nKey = (e.keyCode) ? e.keyCode : e.which;
          if (nKey == 13) {
            var target = (window.event) ? e.srcElement : e.target;
            var bAnchored = (typeof target.id == "undefined") || 
                            (target.id == null) || (target.id != this.id+"-text");
            if (bAnchored) {
              if ((typeof target.value != "undefined") && (target.value != null)) {
                this._anchoredTextNode = target;
                dojo.byId(this.id+"-text").value = dojo.trim(target.value);;
                this.open();
                this.search();
              }
            } else {
              this.search();
            }
          }
        } 
      },
      
      // expand/contract a resultant record on a title click
      onTitleClicked: function(e) {
        if (!e) e = window.event;
        var target = (window.event) ? e.srcElement : e.target; 
        if (target != null) {
          var qScope = target.parentNode;
          if (target.tagName.toLowerCase() == "img") qScope = qScope.parentNode;
          dojo.query("div.abstract",qScope).forEach(function(item) {
            if (item.style.display == "block") item.style.display = "none";
            else item.style.display = "block";
          });
          dojo.query("div.links",qScope).forEach(function(item) {
            if (item.style.display == "block") item.style.display = "none";
            else item.style.display = "block";
          });
        }
      },
       
      // open the widget
      open: function() {
        if (this.domNode.style.visibility == "hidden") {
          this.reposition();
          //this.show(dojo.hitch(this,this.reposition));
          this.show(null);
        } else {
          this.reposition();
        }
      },
      
      // override, post create
      postCreate: function() {  
        this.inherited("postCreate",arguments);
        this.domNode.style.visibility = "hidden";       
      },
      
      renderResponse: function(response,wasSearched) {
        var thisId = this.id;
        var resultsId = this.id+"-results";
        var count = 0;
        var nDuration = 400;
        var elResults = dojo.byId(resultsId);
  
        if (false) {
          var animOut = dojo.fadeOut({node: elResults, duration: nDuration,
            onEnd: dojo.hitch(this,function(){
              elResults.style.display = "none";
              elResults.visibility = "hidden";
              elResults.innerHTML = response;
              var animIn = dojo.fadeIn({node: elResults, duration: nDuration,
                beforeBegin: dojo.hitch(this,function(){
                  elResults.style.display = "";
                  elResults.visibility = "visible";
                })
              }).play();
            })
          }).play();
        } else {
          elResults.innerHTML = response;
        }
        
        dojo.query("div.title",resultsId).forEach(function(item) {
          count++;
          dojo.connect(item,"onclick",dijit.byId(thisId),"onTitleClicked");
        });
        if (wasSearched && (count == 0) && (response.length == 0)) {
          elResults.innerHTML = this.msgNoMatch;
        }
        
        dojo.byId(this.id+"-loading").style.visibility = "hidden";
        
        //var animIn = dojo.fadeIn({node: elResults, duration: nDuration,
        //  beforeBegin: dojo.hitch(this,function(){
        //    elResults.style.display = "";
        //    elResults.visibility = "visible";
        //  })
        //}).play();
        
      },
      
      // reposition to ensure visibility
      reposition: function() {
        var nd = this.domNode;
        var vp = dijit.getViewport();
        var mb = dojo.marginBox(nd);
        var ab = dojo.coords(nd,true);
        if ( !this._wasRepositioned ||
             (vp.t > (ab.t+10)) || ((ab.t+ab.h) > (vp.t+vp.h+100)) ||
             (vp.l > (ab.l+100)) || ((ab.l+ab.w) > (vp.l+vp.w+100)) ) {
          this._wasRepositioned = true;
          var nL = Math.floor(vp.l+((vp.w-mb.w)/2));
          var nT = Math.floor(vp.t+((vp.h-mb.h)/2));
          var nPrefTop = this.preferredTop;
          if ((typeof nPrefTop == "number") && (nPrefTop > 0) && (nPrefTop < (vp.h+10))) {
            var nMaxTop = vp.t + nPrefTop;
            if (nT > nMaxTop) nT = nMaxTop;
          }
          if (nT < vp.t) nT = (vp.t+10);
          //if (nL < vp.l) nL = (vp.l+10);
          dojo.style(nd,{left:nL+"px",top:nT+"px"});
        }
      },
      
      // execute the search
      search: function() {
        var thisId = this.id;
        var sUrl = this.makeRestUrl("htmlfragment");
        var useProxy = ((this.searchProxyUrl != null) && (this.searchProxyUrl.length > 0));    
  
        dojo.byId(this.id+"-loading").style.visibility = "visible";
        if (!useProxy) {
          dojo.xhrGet({
            handleAs: "text",
            load: function(responseObject,ioArgs){
                  dijit.byId(thisId).renderResponse(responseObject,true);},
            preventCache: true,
            url: sUrl
          });
          
        } else {
          sUrl = this.searchProxyUrl+"?url="+escape(sUrl);        
          dojo.io.script.get({
            url: sUrl,
            load: function(responseObject,ioArgs){
                    dijit.byId(thisId).renderResponse(responseObject.innerHTML,true);
                  },
            preventCache: true,
            callbackParamName: "callback"
          });
          
        }
      },
      
      // override, show the widget
      show: function(/* Function? */callback){
        var anim = dojo.fadeIn({
          node: this.domNode,
          duration: this.duration,
          beforeBegin: dojo.hitch(this,function(){
            this.domNode.style.display = "";
            this.domNode.style.visibility = "visible";
            if (this.dockTo && this.dockable) {this.dockTo._positionDock(null);}
            
            // This line of original Dojo code was causing problems, moved to onEnd:
            // if (typeof callback == "function") {callback();}
            this._isDocked = false;
            if (this._dockNode) {
              this._dockNode.destroy();
              this._dockNode = null;
             }
          }),
          onEnd: dojo.hitch(this,function(){
            if (typeof callback == "function") {callback();}
          })
        }).play();
        
        // This line of original Dojo code was causing problems, element grows in size
        // this.resize(dojo.coords(this.domNode));
      },
      
      // show info about the widget
      showInfo: function() {
        this.renderResponse(this.info,false);
      },
      
      // override, startup
      startup: function() {
        if (this._started) return;
        this.inherited("startup",arguments);
        this.build();
        this.reposition();
      },
      
      // validate and synchronize text input
      validateText: function() {
        var elText = dojo.byId(this.id+"-text");
        var sText = dojo.trim(elText.value);
        if (sText != elText.value) elText.value = sText;
        if ((typeof this._anchoredTextNode != "undefined") && (this._anchoredTextNode != null)) {
          this._anchoredTextNode.value = sText;
        }
        return sText;
      }
      
    });
      
    // function to collect customized configuration and to make the serach pane node
    var _gInitializeSearchPaneNode = function(params) {
    
      // look for customized configuration
      var param;
      var scriptParams = null;
      try {
        dojo.query("script["+params.configVariable+"]").forEach(function(item) {
          if (item.getAttribute(params.configVariable) != null) {
            scriptParams = eval("("+item.getAttribute(params.configVariable)+")");
          }
        });
        if (scriptParams != null) {
          for(param in scriptParams) params[param] = scriptParams[param];
        }
      } catch (err) {}
      try {
        var inlineParams = eval(params.configVariable);
        if (inlineParams != null) {
          for(param in inlineParams) params[param] = inlineParams[param];
        }
      } catch (err) {}  
        
      // create a base container node
      var idPfx = _gSearchNamespace+"-search-pane";
      var cssPfx = params.cssNamespace+"-search-pane"
      var elContainer = document.createElement("div");
      elContainer.id = idPfx+"-container";
      elContainer.className = cssPfx+"-container";
      dojo.body().appendChild(elContainer);
      
      // create a the DOM node for the widget
      var elPane = document.createElement("div");
      elPane.id = idPfx;
      elPane.className = cssPfx;
      elPane.style.position = "absolute";
      elPane.style.display = "none";
      elContainer.appendChild(elPane);
      
      return elPane;
    }
    
    // trigger the widget build
    _gBuildSearchPane(_gSearchParams);
      
  });

}
