
var YouKnow = {
  
  search_form: $('find_sold_properties'),
  
  
  init: function()
  {
    if (YouKnow.search_form) {
      YouKnow.init_search_form()
    }
    else {
      YouKnow.init_start();
    }
  },
  
  
  init_start: function()
  {
    new StreetNameSearch ({
      onSuccess: function(transport) {
        $('street_match').update(transport.responseText);
      },
          
      onFailure: function(transport, message) {
        $('street_match').update('<p>'+message+'</p>');
      }
    });
    
    $$('#y_finder_minimum_price', '#y_finder_maximum_price').each(function(el) {
      el.observe('change', function(e) {
        var min = $('y_finder_minimum_price');
        var max = $('y_finder_maximum_price');
        
        minval = parseInt($F(min));
        maxval = parseInt($F(max));
        
        if (minval >= maxval) {
          max.selectedIndex = max.options.length-1;
          min.highlight();
          max.highlight();
        }
        
        if (maxval <= minval) {
          if (max.selectedIndex == 0) {
            max.selectedIndex = max.options.length-1;
          }
          min.selectedIndex = 0;
          min.highlight();
          max.highlight();
        }
      });
    });
  },
  
  
  init_search_form: function()
  {
    YouKnow.okSearchByMap(null);
    // recalculate
    if (r = $('recalculate')) {
      r.observe('click', function(e) {
        e.stop();
        YouKnow.show_recalculating();
        YouKnow.search_form.request({ onSuccess: obj.update_count });
      });
    }
    GEvent.addListener(Global.map.map, "zoomend", YouKnow.okSearchByMap);           
    
    // YouKnow.search_form.observe('submit',YouKnow.setMapBoundsFilter);
    $$("#toolbar li#search_by_map")[0].observe('click',YouKnow.setMapBoundsFilter);
    
    $$("#toolbar li#search_by_map")[0].observe('mouseover', function() {
      $$("#toolbar li#search_by_map")[0].removeClassName('highlighted');
      $$("#toolbar li#search_by_map")[0].addClassName('highlighted');
    });
    $$("#toolbar li#search_by_map")[0].observe('mouseout', function() {
      $$("#toolbar li#search_by_map")[0].removeClassName('highlighted');    
    });
    
    new Form.Observer(YouKnow.search_form, 1, function(frm, params) {
      new Ajax.Request('/you_know?' + params, {
        method: 'get',
        onSuccess: YouKnow.update_count
      });
    });
    
    $$('.at_least_one_checked').each(function(el) {
      checkboxes = el.select('input[type="checkbox"]');
      checkboxes.each(function(checkbox) {
        checkbox.observe('click',
        YouKnow.enforce_checkboxes_checked.bind(checkboxes));
      });
    });
  },
  
  
  // 'this' should be bound to an array of checkboxes
  // Stops user from unchecking a checkbox if no other boxes are checked
  //
  enforce_checkboxes_checked: function(e)
  {
    checkboxes = this;
    // note nasty !! idiom to get boolean value from form control
    values = checkboxes.map( function(cb) { return !! $F(cb) } );
    if (!values.include(true)) {
      e.stop();
    }
  },
  
  
  update_count: function(transport, count)
  {
    if ($('choose_area')) {
      $('choose_area').replace(transport.responseText);
      new Effect.Highlight('choose_area');
    }
    if ($('total')) {
      if (count == null) {
        count = 0;
      }
      $('total').update('Found: '+ count);
    }
  },
  
  
  show_recalculating: function()
  {
    if ($('total')) {
      $('total').update('Recalculating&hellip;');
    }
    if ($$('#choose_area table')[0]) {
      $$('#choose_area td.numeric').each(function(td) {
        td.update('Recalculating&hellip;')
      });
    }
  },
  
  
  okSearchByMap: function(event)
  {
    if (Global.map.map.getZoom() <= 15) {
      $('y_finder_get_map_boundries').setValue(0);
      $$("#toolbar li#search_by_map")[0].removeClassName('disabled');
      $$("#toolbar li#search_by_map")[0].addClassName('disabled');
    }
    else {
      $$("#toolbar li#search_by_map")[0].removeClassName('disabled');
    }
  },
  
  
  setMapBoundsFilter: function(event)
  {
    var top    = "";
    var right  = "";
    var bottom = "";
    var left   = "";
    
    var bounds = Global.map.map.getBounds();
    top    = bounds.getNorthEast().lat();
    right  = bounds.getNorthEast().lng();
    bottom = bounds.getSouthWest().lat();
    left   = bounds.getSouthWest().lng();
    $('y_finder_get_map_boundries').value=1;
    $('y_finder_top').value    = top;
    $('y_finder_right').value  = right;
    $('y_finder_bottom').value = bottom;
    $('y_finder_left').value   = left;
    
    YouKnow.search_form.submit();
  }
  
};
