info343/labs/10/solution/twitter.js

// Make all ajax requests on the page call ajaxError in case of error.
$.ajaxSetup({ error: ajaxError });

var REQUESTS = 0;

// Capture the submit event on the search form.
$(document).ready(function() {
   $('#search-form').submit(search);
});

// Call pageChange when currently-displayed internal "page" is about to be changed.
$(document).bind("pagebeforechange", pageChange);

// A search term was entered into the search box.
function search(event) {
   // Prevent the browser from submitting the form.
   event.preventDefault();
   event.stopPropagation();
   $.ajax('http://search.twitter.com/search.json', {
      data: { 'q': $('#search').val() },
      dataType: 'jsonp',
      success: injectResults
   });
   // return false;
}

// When the subpage is about to be changed, determine which page is about to be
// shown so that loading/cleanup of #show-timeline can be performed.
function pageChange(event, data) {
   console.log(data);
   if (typeof data.toPage === "string") {
      var to = $.mobile.path.parseUrl(data.toPage);
      var timeline = /^#show-timeline/;
      var search = /^(#search-page|)$/;
      
      if (to.hash.search(search) !== -1) {
         // We're moving back to the search page. Clear out the profile info
         // on #show-timeline.
         clearProfile();
      } else if (to.hash.search(timeline) !== -1) {
         // We're going to show the timeline. Fetch and inject the profile data
         // into it.
         loadProfile(event, data);
      }
   }
}

// Remove any existing profile/timeline information from #show-timeline.
function clearProfile() {
   $('#avatar').removeAttr('src').removeAttr('alt');
   $('#show-timeline header h1').empty();
   $('#show-timeline header h2').empty();
   $('#user_tweets strong').empty();
   $('#user_following strong').empty();
   $('#user_followers strong').empty();
   $('#user_location strong').empty();
   $('#timeline').empty();
}

function injectResults(data) {
   // alert(JSON.stringify(data.results));
   $('#results').empty();
   $.each(data.results, function(i, tweet) {
      var $img = $('<img>').attr('src', tweet.profile_image_url.replace(/_normal/, '_bigger')).attr('alt', tweet.from_user);
      var $h3 = $('<h3>').text(tweet.from_user_name);
      var $small = $('<small>').text('@' + tweet.from_user);
      $h3.append($small);
      var $p = $('<p>').text(tweet.text);
      var $a = $('<a>').attr('href', '#show-timeline?screen_name=' + tweet.from_user).data('transition', 'slide');
      $a.append($img).append($h3).append($p);
      var $li = $('<li>').append($a);
      $li.appendTo('#results');
   });
   $('#results').listview('refresh');
}

function loadProfile(event, data) {
   $.ajax('http://api.twitter.com/1/users/lookup.json', {
      data: { 'screen_name': data.options.pageData.screen_name },
      dataType: 'jsonp',
      success: injectProfile
   });
   $.ajax('http://api.twitter.com/1/statuses/user_timeline.json', {
      data: { 'screen_name': data.options.pageData.screen_name },
      dataType: 'jsonp',
      success: injectTimeline
   });
}

function injectProfile(data) {
   var user = data[0];
   $('#avatar').attr('src', user.profile_image_url.replace(/_normal/, '_bigger')).attr('alt', user.name);
   $('#show-timeline header h1').text(user.name);
   $('#show-timeline header h2').text('@' + user.screen_name);
   $('#user_tweets strong').text(user.statuses_count);
   $('#user_following strong').text(user.friends_count);
   $('#user_followers strong').text(user.followers_count);
   $('#user_location strong').text(user.location);
   // profileReady();
}

function injectTimeline(tweets) {
   $.each(tweets, function(i, tweet) {
      // var $a = $('<a>');
      var $img = $('<img>').attr('src', tweet.user.profile_image_url.replace(/_normal/, '_bigger')).attr('alt', tweet.user.screen_name);
      var $h3 = $('<h3>').text(tweet.user.name);
      var $small = $('<small>').text('@' + tweet.user.screen_name);
      $h3.append($small);
      var $tweet = $('<p>').text(tweet.text);
      // $a;
      var $li = $('<li>').append($img).append($h3).append($tweet);
      $li.appendTo('#timeline');
   })
   $('#timeline').listview('refresh');
   // profileReady();
}

// function profileReady() {
//    $.mobile.loading('hide');
//    if (--REQUESTS == 0) {
//       // $.mobile.changePage($('#show-timeline'));
//    }
// }

// Provided Ajax error handler function. (Displays useful debugging message.)
function ajaxError(jqxhr, type, error) {
  var msg = "An Ajax error occurred!\n\n";
  if (type == 'error') {
    if (jqxhr.readyState == 0) {
      // Request was never made - security block?
      msg += "Looks like the browser security-blocked the request.";
    } else {
      // Probably an HTTP error.
      msg += 'Error code: ' + jqxhr.status + "\n" + 
             'Error text: ' + error + "\n" + 
             'Full content of response: \n\n' + jqxhr.responseText;
    }
  } else {
    msg += 'Error type: ' + type;
    if (error != "") {
      msg += "\nError text: " + error;
    }
  }
  alert(msg);
}