if (!window.GLAR) {
  GLAR = {};
}

GLAR.markers = {};

GLAR.init_map = function() {
  GLAR.map = new GMap2(document.getElementById("map_canvas"));
  GLAR.map.removeMapType(G_HYBRID_MAP);
  custom_ui = GLAR.map.getDefaultUI();
  custom_ui.controls.maptypecontrol = false;
  custom_ui.controls.menumaptypecontrol = true;
  GLAR.map.setUI(custom_ui);
  GLAR.map.setCenter(new GLatLng(38.2563, -85.535), 13);
  GLAR.mgr = new MarkerManager(GLAR.map, { borderPadding: 50 });
  GLAR.bounds = new GLatLngBounds();
}

GLAR.add_marker = function(id, lat, lng, html) {
  point = new GLatLng(lat, lng);
  var marker = new GMarker(point);
  GLAR.markers[id] = marker;
  GLAR.mgr.addMarker(marker, 0);
  GEvent.addListener(marker, "click", function() {
    $('#search_results tr').removeClass('selected');
    $('#search_results tr#result_' + id).addClass('selected');
    marker.openInfoWindowHtml(html);
  });
  GLAR.bounds.extend(point);
  GLAR.map.setZoom(GLAR.map.getBoundsZoomLevel(GLAR.bounds));
  GLAR.map.setCenter(GLAR.bounds.getCenter());
}

GLAR.click_marker = function(id) {
  GEvent.trigger(GLAR.markers[id], "click");
}

GLAR.reset_map = function() {
  GLAR.mgr.clearMarkers();
  GLAR.markers = {};
  GLAR.bounds = new GLatLngBounds();
}

GLAR.display_range = function(element, val1, val2, currency, nomin, nomax) {
  if (!val1) {
    val1 = nomin || 'any';
  }
  if (!val2) {
    val2 = nomax || 'any';
  }
  $(element).html(GLAR.format_range(val1, val2, currency));
}

GLAR.format_range = function(val1, val2, currency) {
  val1 = val1.toString();
  val2 = val2.toString();
  if (currency) {
    if (val1.match(/^\d+$/)) {
      val1 = GLAR.format_currency(val1);
    }
    if (val2.match(/^\d+$/)) {
      val2 = GLAR.format_currency(val2);
    }
  }
  return val1 + ' - ' + val2
}

GLAR.format_currency = function(val) {
  val = val.split('');
  for (i = val.length - 3; i > 0; i = i - 3) {
    val.splice(i, 0, ',');
  }
  return '$' + val.join('');
}

GLAR.modify_list = function(klass, e) {
  if (e.target.checked) {
    $.post('../list/add/' + klass + '/' + e.target.value, null, null, 'script');
  } else {
    $.post('../list/remove/' + klass + '/' + e.target.value, null, null, 'script');
  }
}

GLAR.clear_list = function(klass) {
  $.post('../list/clear/' + klass, null, null, 'script');
}

GLAR.make_basic_auth = function(user, pass) {
  var tok = user + ':' + pass;
  var hash = Base64.encode(tok);
  return "Basic " + hash;
}

GLAR.members_only_auth = function() {
  var url = $('#members_only_form').attr('action');
  $.ajax({
      url: url,
      async: false,
      type: 'GET',
      username: $('#members_only_user').val(),
      password: $('#members_only_pass').val(),
      success: function() {
        window.location = url;
      },
      error: function() {
        alert('That username/password combination was not valid. Please try again.');
      }
  });
}

GLAR.live_search = function(el) {
  $('#num_results').html('...');
  if ($('#search_results').length > 0) {
    $('#search_results .result-number').html('<img alt="Ajax_bar" height="19" src="../images/ajax_bar.gif" width="220" />');
    $.get($(el).parents('form').attr('action'), $(el).parents('form').serialize(), null, 'script');
  } else {
    var url = $(el).parents('form').attr('action');
    url = url.replace('/search/', '/count/')
    $.get(url, $(el).parents('form').serialize(), null, 'script');
  }
}

GLAR.set_selected_area = function(val) {
  $('#search_area input').each(function(i, input){
      if ($(input).val() == val) {
        $(input).attr('checked', $(input).attr('checked') ? false : true);
      }
  });
  parent.$.fn.fancybox.close();
  $('#selected_areas_num').html($('#search_area input:checked').length);
  GLAR.live_search($('#search_area').get());
  return false;
}

GLAR.clear_selected_areas = function() {
  $('#search_area input:checked').each(function(i) {
    $(this).removeAttr('checked');
  })
  $('#selected_areas_num').html($('#search_area input:checked').length);
  GLAR.live_search($('#search_area').get());
  return false;
}