jQuery(function($){
  function performRetrieval(){
    // Retrieve the story ID from the URI.
    var id = new String(window.location.hash).substr(1);
    
    // Clear out any previous stories or messages, show thinking indicator.
    $('.api-container-item').remove();
    $('.api-message').hide();
    $('.api-thinking').show();
    
    // Create search interface by registering username, request, and callbacks; perform search.
    var story_interface = new Story('Y2F0X3NlYXJjaDpjNHRfNTM0cmNo', id, processResponse, processError);
    story_interface.exec();
  }
  
  
  function processError(xml_http_request, status, exception){
    $('.api-thinking').hide();
    $('#api-message-search_error').show();
  }
  
  
  function processResponse(story){
    var story_id = 'story-' + story.id;
    var title = story.title;
    var content = story.content;
    var published_identity = story.person.published_identity;
    var subtitle = published_identity + ' writes:';

    // Display the retrieved story.
    var story_row = '<div class="api-container-item" id="' + story_id + '">';
    story_row += (title != null) ? '<h2>' + title + '</h2>' : '';
    story_row += 
        '<div class="api-container-item-subtitle">' + subtitle + '</div>' + 
        '<div class="api-container-item-content">' + content + '</div>' +
        '</div>';
    $('.api-container').append(story_row);

    $('.api-thinking').hide();
  }
  
  // Prepare page when the DOM is ready.
  $(document).ready(function(){ 
    performRetrieval();
  });
});
