function hideAllEventLists() {
  $$('.event_list').each(function(e) { e.hide(); });
}

// Google Maps stuff!

var ev_obj_holder;
var map;
var dir;

function initializeMap(ev_obj) { 
  if (GBrowserIsCompatible()) {
    ev_obj_holder = ev_obj;
    map = new GMap2(document.getElementById("map"));    
    map.setCenter(new GLatLng(37.4419, -122.1419), 10);
    map.addControl(new GMapTypeControl());
    map.addControl(new GSmallZoomControl());
    map.setMapType(G_SATELLITE_MAP);
    
    function createMarker(latlng, ev) {
      var marker = new GMarker(latlng);
      var html = "<span style=\"font-size: 0.8em;\"><strong>" + ev.name + "</strong><br />" + ev.address + "<br />" + ev.city + ", " + ev.state + " " + ev.zip + "</span>";
      map.openInfoWindowHtml(latlng, html);
      return marker;
    }
    
    var bounds = new GLatLngBounds;
    var latlng = new GLatLng(ev_obj.lat, ev_obj.lng)
    bounds.extend(latlng);
    map.addOverlay(createMarker(latlng, ev_obj));
    map.setCenter(bounds.getCenter(), 16);
    map.checkResize();
  }
}  

var further_directions_link;
function getDirections(from_addr, to_addr) {
  dir = new GDirections(map);
  GEvent.addListener(dir, "error", function(x) {
      alert('Error ' + dir.getStatus().code + ': Sorry, we could not gather directions for that address. Please try to be a bit more specific.');
  });
  dir.load(from_addr + ' to ' + to_addr, { "getSteps" : true });
  further_directions_link = "http://maps.google.co.uk/maps?f=q&source=s_q&hl=en&geocode=&q=" + from_addr + "+to+" + to_addr + "&ie=UTF8&t=h&z=6"
  GEvent.addListener(dir, "load", showDirections);
}

var map_shown = false;
var target_height = 350;
var target_width = 530;
function showEventMap() {
  if(!map_shown) {
    $('map_action_word').update("Hide");
    new Effect.BlindDown('event_map_container');
  } else {
    $('map_action_word').update("Show");
    new Effect.Morph('event_map_container', { style : 'width: 0px; height: 0px;' });
  }
  map_shown = !map_shown;
}

function showDirections() {
  var rhtml = "<a href=\"" + further_directions_link + "\">Full</a> <ol id=\"map_directions_list\" class=\"map_directions_list\">";
  var route = dir.getRoute(0);
  var steps = route.getNumSteps();
  for(var i=0; i < steps; i++) {
    var step = route.getStep(i);
    rhtml = rhtml + "<li class=\"map_directions_step\">" + step.getDescriptionHtml() + "</li>";
  }
  $('map_dirs').update(rhtml + "</ol>");
  $('event_map_container').morph('height: ' + (target_height + 100) + 'px;');
}

function toggleDirInput() {
  $('map_dir_prompt').toggle();
  $('get_dirs_link_container').toggle();
}

function clearAllTabSelections() {
  $$('.events_tab_link').each(function(e) { e.removeClassName('activeTab'); });
}

function selectTab(tab_name) {
  clearAllTabSelections();
  $('tab_link_' + tab_name).addClassName('activeTab');
  showEventsSpinner();
}

var effect_duration = 0.2;
function showEventsSpinner() {
  new Effect.Fade('tab_container', { 'queue' : 'end', 'duration' : effect_duration });
  new Effect.Appear('event_tab_spinner', { 'queue' : 'end', 'duration' : effect_duration });
}

function showTabContainer() {
  new Effect.Fade('event_tab_spinner', { 'queue' : 'end', 'duration' : effect_duration });
  new Effect.Appear('tab_container', { 'queue' : 'end', 'duration' : effect_duration });
}

var friends = [];

function addFriend(id, name) {
  if(hasNotInvitedFriend(id)) {
    friends[friends.length] = new Hash({ 'name' : name, 'id' : id });
    updateHiddenField();
  }
}

function removeFriend(id, name) {
  if(!hasNotInvitedFriend(id)) {
    friends.each(function(u_item) {
      if (u_item.get("id") == id) { friends = friends.without(u_item); }
    });
    updateHiddenField();
  }
}

function updateHiddenField() {
  var fstring = "";
  friends.each(function(u_item) { fstring = fstring + u_item.get("id") + ","; });
  $('users').value = fstring;
  
  var nstring = "";
  friends.each(function(u_item) { nstring = nstring + "<a href=\"javascript:removeFriend(" + u_item.get("id") + ", '" + u_item.get("name") + "');\">" + u_item.get("name") + "</a> "; });
  $('friends_invited_list').update(nstring);
}

function hasNotInvitedFriend(id) {
  found = true;
  friends.each(function(u_item) {
    if(u_item.get("id") == id) { found = false; }
  });
  return found;
}