var map;
var latlngs = {
  rockdale: new google.maps.LatLng(-33.950596, 151.139120),
  kogarah: new google.maps.LatLng(-33.963286, 151.133938),
  milton: new google.maps.LatLng(-35.315616, 150.434887),
  bexley: new google.maps.LatLng(-33.962761, 151.113982),
  hurstville: new google.maps.LatLng(-33.96426, 151.10282)
};
var markers = new Object();

$(function() {
  // First, add the links to all sections
  $('.venue_title').wrapInner('<a></a>').children()
    .each(function(i, elem) {
      var venue = $(elem).parents('.venue').attr('id').substring(2);
      var func = function() { showVenue(venue); };
      $(elem).attr('href', '#' + venue).click(func);
    });

  // Now, see what we have.
  var href = window.location.toString();
  var hash_loc = href.indexOf('#');
  var venue = 'rockdale';
  if (hash_loc > 0) {
    venue = href.substring(hash_loc + 1);
  }
  $('.venue').not('#v_' + venue).children('.venue_detail').hide();
  if (!venue in latlngs) {
    venue = 'rockdale';
  }

  // Create the map div.
  $('#sMain').append('<div class="map"></div>');
  map = new google.maps.Map($('.map').get(0), {
    center: latlngs[venue],
    zoom: 15,
    mapTypeId: google.maps.MapTypeId.ROADMAP
  });


  // Add the markers to the map.
  for (var venue in latlngs) {
    makeMarker(venue);
  }
});


function makeMarker(venue) {
  // Get the map centre, and move the map there.
  var centre = latlngs[venue];

  // Get a description for the marker / info window.
  var title = $('#v_' + venue).children('.venue_title').text();
  var details = $('#v_' + venue).children('.venue_detail').html();

  // Create the marker and store.
  var options = { title: title };
  var marker = new google.maps.Marker({
    position: centre,
    map: map
  });
///  marker.bindInfoWindowHtml('<p><strong>' + title + '</strong><br/>' +
///      details + '</p>');
  markers[venue] = marker;
}


function showVenue(venue) {
  // Get the map centre, and move the map there.
  var centre = latlngs[venue];
  if (!centre) {
    centre = latlngs['rockdale'];
    venue = 'rockdale';
  }
  map.panTo(centre);

  // Show only the sections we care about.
  $('.venue').not('#v_' + venue).children('.venue_detail').hide();
  $('#v_' + venue).children('.venue_detail').show();

  // Now highlight the marker.
  marker = markers[venue];
  var title = $('#v_' + venue).children('.venue_title').text();
  var details = $('#v_' + venue).children('.venue_detail').html();
  var options = { title: title };
///  marker.openInfoWindowHtml('<p><strong>' + title + '</strong><br/>' +
///      details + '</p>');
///  GEvent.trigger(marker, 'click');
}

