﻿
// 
// SoSearch class 
//

function SoSearch (map, opts)
{   
    var me = this;
    this.map = map;
    this.localSearch = null;
    var me = this;
    this.selectedResults = [];
    this.currentResults = [];
    this.searchForm = null;
    this.searchWellContainerId = opts.searchWellId || "search-results-container";
    this.searchWellId = opts.searchWellId || "search-results";
    this.searchInputId = opts.searchInputId || "searchText";
    this.searchButtonId = opts.searchButtonId || "searchSubmit";
    this.localSearch = new GlocalSearch();
    this.localSearch.setCenterPoint(this.map);
    
    this.defaultIcon = new GIcon();
    this.defaultIcon.image = "/images/markers/postcode_location_icon.png";
    this.defaultIcon.shadow = "/images/markers/shadow_postcode_location_icon.png";
    this.defaultIcon.iconSize = new GSize(44.0, 39.0);
    this.defaultIcon.iconAnchor = new GPoint(22.0, 20.0);
    this.defaultIcon.infoWindowAnchor = new GPoint(20, 43);
    
    this.onLocalSearch = function() {  me.parseSearchResults(); };
    
    this.localSearch.setSearchCompleteCallback(null, this.onLocalSearch);

    // Ignore listings - note this doesn't work
    // this.localSearch.setRestriction(GSearch.RESTRICT_TYPE, GlocalSearch.TYPE_KMLONLY_RESULTS);
    
    // Wire up the search button click handler - no done in masp.asp then somap call us
    //$("input#" + this.searchButtonId).click( function(e) { e.preventDefault(); me.findAll(); });
    
    // Search using the search input supplied in opts
    this.findAll = function()
    {
        var s = $("input#" + this.searchInputId).val();
        if (s.length > 2)
        {   
            this.localSearch.execute("\"" + s + "\", UK") ;
        }
    };
    

    // Called when Local Search results are returned, we clear the old results and load the new ones.
    this.parseSearchResults = function(s)
    {
        var me = this;
       //GLog.write("In parseSearchResults");
      if (!this.localSearch.results || this.localSearch.length == 0)
      {
        alert("No results found");
        return;
      }
      
      // Only deal with the first marker at the moment - reast in beta 2 perhaps
      for (var i = 0; i < this.currentResults.length; i++) {
          this.map.removeOverlay(this.currentResults[i].marker());
      }

      this.currentResults = [];
      var first = this.localSearch.results[0];
 
      // move the map to the first result
      if (first != null)
      {
        this.currentResults.push(new LocalResult(first, i));
        this.map.addOverlay(this.currentResults[0].marker(this.defaultIcon));
        this.map.setZoom(11);
        this.map.panTo(new GLatLng(parseFloat(first.lat), parseFloat(first.lng)));
      }
      else
      {
        alert("No matches found");
      }
      
   };
   
   
    
   
    //
    // LocalSearch class
    //

    // A class representing a single Local Search result returned by the
    // Google AJAX Search API.
    function LocalResult(result, index) {
      this.result_ = result;
    };
    
    // Returns the GMap marker for this result, creating it with the given
    // icon if it has not already been created.
    LocalResult.prototype.marker = function(opt_icon) {
       var me = this;
      if (this.marker_) return this.marker_;
      var marker = new GMarker(new GLatLng(parseFloat(this.result_.lat),
                                         parseFloat(this.result_.lng)),
                                        {icon: opt_icon, zIndexProcess: me.zIndex});
      GEvent.bind(marker, "click", this, function() {
        marker.openInfoWindow("<p>The signpost marks the position of the postcode or location <br/>you searched for. Tick one or more of the sections on the <br/>left hand menu to see listings in this area.</p>");
      });
      this.marker_ = marker;
      return marker;
    };
    
    LocalResult.prototype.zIndex = function(marker, b)
    {
        var zIndexAddition = 200;
        return GOverlay.getZIndex(marker.getPoint().lat()) + zIndexAddition * 1000000;
    };
};

