var map_initialized = false;
function initializeMap() {
  if (map_initialized) return;

  map_initialized = true;

  //ajaxify the destination search form
  $('#destinations_search_form').ajaxForm({target: '#destinations', success: function() {$('#google_map').mapController().updateMapMarkers();}});
  $('#attractions_search_form').ajaxForm({target: '#attractions', success: function() {$('#google_map').mapController().updateMapMarkers();}});
  $('#properties_search_form').ajaxForm({target: '#properties', success: function() {$('#google_map').mapController().updateMapMarkers();}});

  //setup the map
  function map_moveend_handler(){
    //as a gevent handler, 'this' is the map
    $('#destinations_max_lat').val(this.getBounds().getNorthEast().lat());
    $('#destinations_min_lat').val(this.getBounds().getSouthWest().lat());
    $('#destinations_max_lng').val(this.getBounds().getNorthEast().lng());
    $('#destinations_min_lng').val(this.getBounds().getSouthWest().lng());
    $('#destinations_search_form').submit();

    $('#attractions_max_lat').val(this.getBounds().getNorthEast().lat());
    $('#attractions_min_lat').val(this.getBounds().getSouthWest().lat());
    $('#attractions_max_lng').val(this.getBounds().getNorthEast().lng());
    $('#attractions_min_lng').val(this.getBounds().getSouthWest().lng());
    $('#attractions_search_form').submit();
    
    $('#properties_max_lat').val(this.getBounds().getNorthEast().lat());
    $('#properties_min_lat').val(this.getBounds().getSouthWest().lat());
    $('#properties_max_lng').val(this.getBounds().getNorthEast().lng());
    $('#properties_min_lng').val(this.getBounds().getSouthWest().lng());
    $('#properties_search_form').submit();
  }

  setTimeout(function() {
    // $('#google_map').mapController(null, '#mappable', {moveend: map_moveend_handler});
		$('#google_map').mapController('#destination_data', '#mappable', {});
  }, 50);
}

function fb_sharer(title, href){
  u=href;
  t=title;
  window.open('http://www.facebook.com/sharer.php?u='+encodeURIComponent(u)+'&t='+encodeURIComponent(t),'sharer','toolbar=0,status=0,width=626,height=436');
  return false;
}

function twitter_sharer(title){
  window.open('http://twitter.com/home/?status='+encodeURIComponent(title))
}

$(function() {
// $('#destination_content_tabs')
//     .tabs({
//       cookie: { expires: 30 },
//       cache: true
//     })
//     .bind('show.ui-tabs', function(e, ui) {
//       if (!map_initialized && ui.panel.id == 'weather_and_attractions') {
//         initializeMap();
//       }
//     });
// 
//   //select programatically since the cookie will override the selected tabs option
//   if (/#review_/.test(location.hash))
//     $('#destination_content_tabs').tabs('select', 1);

  // var selected = $('#destination_content_tabs li').index($('#destination_content_tabs li.ui-tabs-selected')[0]);
  // if (selected == 0)
  //   initializeMap();
  if($('#google_map').length > 0){
    initializeMap();
  }

  $('div.destination_properties_panel img.photo').popups();
  
  // $('.add_attraction_link').modal_link({success: function() {$('#attractions_search_form').submit();}});
});