// locations javascript

window.addEvent('domready', function() {
  locations = new Locations();
});

window.addEvent('unload', function() {
  GUnload();
});

var Locations = new Class({

  form: Object,
  fieldset: Object,
  map: Object,
  icon: Object,
  markerOptions: Object,
  geocoder: Object,
  
  initialize: function() {

    this.form = document.getElementById('frm_locations'); // IE7 has problem with $('frm_locations');
    this.fieldset = $('fieldset-fst_locations_details');

    // location_results_footnote
    
    if(GBrowserIsCompatible()) {
      this.map = new GMap2($('map'));
      this.map.setCenter(new GLatLng(-37.8215, 144.9632), 10);
      this.map.enableContinuousZoom();
      this.map.enableDoubleClickZoom();
      this.map.addControl(new GSmallMapControl());
      
      this.icon = new GIcon(G_DEFAULT_ICON);
      this.icon.image = "http://maps.google.com/mapfiles/arrow.png";
      this.icon.shadow = "http://maps.google.com/mapfiles/arrowshadow.png";
      this.icon.iconSize = new GSize(34, 30);
      
      this.markerOptions = {icon:this.icon};
    }
  },
  
  search: function(postcode, suburb) {
    var request = new Request({
      method: 'get',
      url: '/ajax/location-search',
      data: {
        'postcode': postcode,
        'suburb': suburb
      },
      link: 'cancel',
      onComplete: function(response) {
        if($defined($('locations_wrapper'))) $('locations_wrapper').destroy();
        var td = new Element('td', {'html': response});
        var tr = new Element('tr', {'id': 'locations_wrapper'});
        td.inject(tr); tr.inject($('location_input'), 'after');        

        $$('td.suburb').each(function(td) {
          var address = td.get('text') + ', ' + td.getNext('td').get('text') + ', ' + td.getNext('td').getNext('td').get('text');
          var a = new Element('a', {
            'href': '/locations',
            'text': td.get('text'),
            'title': 'Show on Map',
            'events': {
              'click': function() { this.set_map(address); return false; }.bind(this)
            }
          });
          td.set('text', ''); a.inject(td);
        }.bind(this));
        
        if($defined($('location_results_footnote'))) {
          var disclaimer  = new Element('span', {html: 'Note: Map function may display inaccurate locations'});
          disclaimer.inject($('location_results_footnote'));
        }        
        
      }.bind(this)
    }).send();
  },

  clear: function() {
    this.form.postcode.value = '';
    this.form.suburb.value = '';
    if($defined($('locations_wrapper'))) { $('locations_wrapper').destroy(); }
    this.map.clearOverlays();
    this.map.setCenter(new GLatLng(-37.8215, 144.9632), 10);
    window.scrollTo(0,0);
  //  if(navigator.appVersion.indexOf('MSIE 6.') != -1 || navigator.appVersion.indexOf('MSIE 5.') != -1) {
  //    this.form.style.height = '250px'; divHeightFix();
  //  }
    return false;
  },
  
  set_map: function(address) {
    this.geocoder = new GClientGeocoder();
    this.map.clearOverlays();  
    this.geocoder.getLatLng(address + ', Australia', function(point) {
      if(!point) {
        this.map.setCenter(new GLatLng(-37.8215, 144.9632), this.map.getZoom()); alert(address + " not found");
      } else {
        this.map.setCenter(point, this.map.getZoom());
        var marker = new GMarker(point);
        this.map.addOverlay(new GMarker(point, this.markerOptions));
      }
    }.bind(this));
  }
  
});