/**
 * Generate the json data to be plotted in the graph
 * TODO: Ugly code, need to revise  
 * 
 * The data is an array of graph coordinates, which in our case is in this form
 * ([time], [number of likes/comments])  
 * 
 * @param $period
 *   The period to calculate the most popular status (by month, e.g. 6 or 12 months)
 */
function generate_data($statuses, $period = null) {
  $oldest_status = get_oldest($statuses);  
  $oldest_year = get_year($oldest_status['updated_time']); // year of the oldest status    
  $newest_status = get_newest($statuses);  
  $newest_year = get_year($newest_status['updated_time']); // year of the most recent status     
      
  // Currently the graph can only plot time period of 2, 3, 4, 6
  // So make it 12 months if an invalid number is passed
  if (($period == null) || ($period > 12) || ((12 % $period) != 0)) {
  	$period = 12;
  }
  
  $from = "01-01-$oldest_year";
  
  $data = array();  
  for ($year = $oldest_year; $year <= $newest_year + 1; $year++) {  	
  	$to = "01-01-$year";  	
  	for ($month = 1 + $period; $month <= 12 + 1; $month += $period) {
      if ($to != $from) {  	      	    	    	    	  
  	    $most_popular = get_most_popular($statuses, '', $from, $to);
  	    if ($most_popular) {
  	      $popularity = $most_popular['like_count'] + $most_popular['comment_count']; 
   	    }
  	    else {
  	      $popularity = 0;
  	    }    	    	    	 
  	    // Notes: The Flot API can only accept time data as Javascript timestamp
  	    // We need to pass a valid date string and generate timestamp at front end  	      	  
  	    $data['plot_data'][] = array(date('M j, Y', strtotime("$to")), $popularity);
  	    $data['status_data'][] = array(
  	      'status' => status_format_js($most_popular),
  	      'from' => $from,
  	      'to' => $to,
  	    );  	    
  	    
  	    if ($year == $newest_year + 1) {  	      
  	      break;	
  	    }
  	  } 
  	  $next = $month + 1; 	    	    	 
  	  $from = $to;
  	  $to = "01-$month-$year";
  	}  	
  }	  
  return $data;
}
/**
 * Display the oldest status
 */
function print_oldest($statuses) {
  global $user;
  $status = get_oldest($statuses);
  
  echo "<h2>Oldest Retrievable Status</h2>";  
  print_status($status);
}