var Search = {
  weights: new Object(),
  currentLoader: null,
  currentUrl: null,
  currentTimeoutId: null,
  
  reload: function()
  {
    var url = window.location;
    var weights = this.weights;
    for (var dimensionId in weights)
    {
      // Prototype inserts extend() into array instances; just ignore it
      if (dimensionId != "extend" && weights[dimensionId].getValue() != null)
      {
        url += "&" + escape(dimensionId + "[weight]") + "=" + 
          escape(weights[dimensionId].getValue());
      }
    }
    url = url.replace(/\/search\/results/, '/search/embed');
    if (url != this.currentUrl)
    {  
      this.currentUrl = url;
      if (this.loader)
      {
        this.loader.abort();
        this.loader = null;
      }
      var loader = new HttpLoader(url);
      document.getElementById("SearchStatusText").innerHTML = "Loading results...";
      if (false) // Disable fade-in on status box
      {
        Effect.Appear("SearchStatusBox", {duration: 1.0});
      }
      else
      {
        document.getElementById("SearchStatusBox").style.display = "block";
      }
      loader.setProgressCallback(function(state)
        {
        });
      loader.setErrorCallback(function(code, text)
        {
          document.getElementById("SearchStatusBox").style.display = "block";
          document.getElementById("SearchStatusText").innerHTML =
            "<strong>Error (" + code + ") loading results from server</strong>: " + text;
          Search.loader = null;
        });
      loader.setDataCallback(function(data)
        {
          document.getElementById("SearchResultsTableContainer").innerHTML = data;
          if (EFFECTS)
          {
            Effect.Fade("SearchStatusBox", {duration: 0.5});
          }
          else
          {
            document.getElementById("SearchStatusBox").style.display = "none";
          }
          Search.loader = null;
        });
      loader.start();
      this.loader = loader;
    }
  },
  
  reloadIn: function(milliseconds)
  {
    if (this.currentTimeoutId)
    {
      window.clearTimeout(this.currentTimeoutId);
      this.currentTimeoutId = null;
    }
    this.currentTimeoutId = window.setTimeout("Search.reload()", milliseconds);
  },
  
  setupSlider: function(dimensionId, widgetId, barId, knobId, helpId)
  {
    // Set up the slider user interface
    var slider = new Slider($(widgetId), $(barId), $(knobId), 0.0, 1.0, 0.05);
    slider.onBeginDrag = function()
    {
      addStyleClass($(widgetId), "activeSlider");
      $(helpId).style.display = "block";
    };
    slider.onEndDrag = function()
    {
      removeStyleClass($(widgetId), "activeSlider");
      $(helpId).style.display = "none";
    };
    slider.onChange = function()
    {
      // Adjust the colour of the bar
      function hex(v)
      {
        var s = (Math.round(v * 255)).toString(16);
        while (s.length < 2)
        {
          s = "0" + s;
        }
        return s;
      }
      var slider = this;
      var value = (slider.getValue() - slider.min) / slider.max;
      var rgb = hsvToRgb(0.6, 0.7, value);
      $(barId).style.background = 
        "#" + hex(rgb[0]) + hex(rgb[1]) + hex(rgb[2]);
      
      // Display description of current value
      var descriptions = [
        "Not important",
        "Slightly important",
        "Important",
        "Very important",
        "Most important"
      ];
      var description = descriptions[
        Math.floor(value * (descriptions.length - 1))];
      document.getElementById(helpId).innerHTML = description;

      // Update weight      
      Search.weights[dimensionId].setValue(slider.getValue());      
    };
    
    // Connect slider to weight binding
    var weightBinding = Search.weights[dimensionId];
    weightBinding.registerListener(function(binding)
      {
        slider.setValue(binding.getValue());
      });
    slider.setValue(weightBinding.getValue());
  }
}
