$(document).ready(function() {
  // Bring up form to change location
  $('a.change_location').click(function() {
    $('#location').hide();
    $('#location_form').show();
    $('#location_form input').focus();
    return false;
  });
  
  $('a.cancel_change').click(function() {
    $('#location').show();
    $('#location_form').hide();
    return false;
  });

  // Change your location
  $('#location_form form').submit(function() {
    $('#location_form').hide();
    $('#location_loading').show();
    $('#live_list').fadeOut('fast').load(this.action + '?' + $(this).serialize(), function() {
      $('#live_list').fadeIn('fast');
      $('#location_loading').hide();
      $('#location .clip').text('Reviews near');
      var location = $(this).find('#location_name').html();
      $('#location_form input').val(location);
      if (location.length > 40) {
        location = location.substring(0, 32) + "...";
      }
      $('#location .location_full').text(location);
      $('#location .clear_location').show();
      $('#location').show();
    });
    return false;
  });

  // Clear your location
  $('#location .clear_location > a').click(function() {
    $('#location').hide();
    $('#location_loading').show();
    $('#live_list').fadeOut('fast').load(this.href, function() {
      $('#live_list').fadeIn('fast');
      $('#location_loading').hide();
      $('#location_form input').val('');
      $('#location .clip').text('');
      $('#location .location_full').text('Showing reviews for all locations');
      $('#location .clear_location').hide();
      $('#location').show();
    });
    return false;
  });

  // Change the list type & reset the page to 1
  $('.review_lists a').live('click', function() {
    $.setFragment({ list: $.queryString(this.href).list });
    $.setFragment({ page: 1 });
    $('.review_lists li').removeClass('selected');
    $(this).parent().addClass('selected');
    return false;
  });
  
  // Change page within the current list
  $('.pagination a').live('click', function() {
    $.setFragment({ page: $.queryString(this.href).page });
    return false;
  });
  
  // Enable URL fragment handling
  $.fragmentChange(true);
  
  // Handle the list & pagination requests on the fragment change event
  $(document).bind("fragmentChange.page", function () {  
    $('#live_list').animate({opacity: "0.0"}).load($.queryString(document.location.href, {'page':$.fragment().page, 'list':$.fragment().list}), function() {
      // $(this).animate({opacity: "1.0"}, "fast", "swing", function(){
      //   $("html").animate({ scrollTop: $('.review_lists:first').scrollTop()}, "slow");
      // });
      $(this).animate({opacity: "1.0"});
    });
  });
  
  // Do the right thing if we're loading the page fresh and it has a page fragment
  // This _needs_ to follow the above code
  if ($.fragment().page) {  
    $(document).trigger("fragmentChange.page");
  }
  
  // jMapping
  // $('#map').jMapping({
  //   side_bar_selector: "#review-list",
  //   location_selector: ".review"
  // });
});


// Review
var Review = {
  init: function()
  {
    Review.init_nearby();
  },
  init_nearby: function()
  {
    $('#map').jMapping({
      map_config: function(map)
      {
        map.addControl(new GSmallZoomControl());
      },
      side_bar_selector: "#nearby",
      location_selector: "li.rating",
      link_selector:     "a",
      category_icon_options: {
        'current-1': {
          primaryColor: '#cc0000',
          cornerColor: '#f7a6a6',
          width: 44,
          height: 44
        },
        'current-2': {
          primaryColor: '#cc0000',
          cornerColor: '#f7a6a6',
          width: 44,
          height: 44
        },
        'current-3': {
          primaryColor: '#f64f06',
          cornerColor: '#fad1af',
          width: 44,
          height: 44
        },
        'current-4': {
          primaryColor: '#f64f06',
          cornerColor: '#fad1af',
          width: 44,
          height: 44
        },
        'current-5': {
          primaryColor: '#fdcc23',
          cornerColor: '#fceabe',
          width: 44,
          height: 44
        },
        'current-6': {
          primaryColor: '#fdcc23',
          cornerColor: '#fceabe',
          width: 44,
          height: 44
        },
        'current-7': {
          primaryColor: '#81b51c',
          cornerColor: '#e6fcbb',
          width: 44,
          height: 44
        },
        'current-8': {
          primaryColor: '#81b51c',
          cornerColor: '#e6fcbb',
          width: 44,
          height: 44
        },
        'current-9': {
          primaryColor: '#4ca710',
          cornerColor: '#bbf496',
          width: 44,
          height: 44
        },
        'current-10': {
          primaryColor: '#4ca710',
          cornerColor: '#bbf496',
          width: 44,
          height: 44
        },
        'nearby-1': {
          primaryColor: '#cc0000',
          cornerColor: '#f7a6a6'
        },
        'nearby-2': {
          primaryColor: '#cc0000',
          cornerColor: '#f7a6a6'
        },
        'nearby-3': {
          primaryColor: '#f64f06',
          cornerColor: '#fad1af'
        },
        'nearby-4': {
          primaryColor: '#f64f06',
          cornerColor: '#fad1af'
        },
        'nearby-5': {
          primaryColor: '#fdcc23',
          cornerColor: '#fceabe'
        },
        'nearby-6': {
          primaryColor: '#fdcc23',
          cornerColor: '#fceabe'
        },
        'nearby-7': {
          primaryColor: '#81b51c',
          cornerColor: '#e6fcbb'
        },
        'nearby-8': {
          primaryColor: '#81b51c',
          cornerColor: '#e6fcbb'
        },
        'nearby-9': {
          primaryColor: '#4ca710',
          cornerColor: '#bbf496'
        },
        'nearby-10': {
          primaryColor: '#4ca710',
          cornerColor: '#bbf496'
        }
      }
    });
    
    // Activate first item (i.e., the review we're on)
    Review.activate_current();
    
    // Click on <h1>
    $('#review h1 a, .address').click(Review.activate_current);
  },
  activate_current: function()
  {
    $('#nearby .current a').click();
    return false;
  }
}