<?xml version="1.0" encoding="UTF-8"?>
<commit>
  <added type="array"/>
  <modified type="array">
    <modified>
      <diff>@@ -1,9 +1,6 @@
 #!/usr/bin/php
 &lt;?php
-
-
 $url = 'http://twitter.com/statuses/public_timeline.json';
-// $url = 'public_timeline.json';
 
 $json = file_get_contents($url);
 
@@ -21,13 +18,7 @@ foreach ($pubtweets-&gt;docs as &amp;$doc) {
 	unset($newdoc);
 }
 
-// echo &quot;&lt;pre&gt;&quot;; echo print_r($pubtweets, true); echo &quot;&lt;/pre&gt;&quot;;
-
-
 $newjson = json_encode($pubtweets);
-// $json = json_encode(json_decode($json));
-
-// echo &quot;&lt;pre&gt;&quot;; echo print_r($json, true); echo &quot;&lt;/pre&gt;&quot;;
 
 $cdb_url = 'http://127.0.0.1:5984/publictweets_sources/_bulk_docs';
 
@@ -36,29 +27,22 @@ $http-&gt;setContentType('application/json');
 $http-&gt;setRawPostData($newjson);
 $http-&gt;send();
 
-exit();
-
-// update view
-$url = 'http://127.0.0.1:5984/publictweets_sources/_view/sourcetotal/total?group=true';
+/*
+	update view
+*/
+// $url = 'http://127.0.0.1:5984/publictweets_sources/_view/sourcetotal/total?group=true';
+$url = 'http://127.0.0.1:5984/publictweets_sources/_view/all/counts?group=true';
 $json_counts = file_get_contents($url);
 
 $counts = json_decode($json_counts);
 
 foreach($counts-&gt;rows as &amp;$row) {
 	$row-&gt;key = strip_tags(stripslashes($row-&gt;key));
-	// echo &quot;&lt;pre&gt;&quot;; echo print_r($row-&gt;key, true); echo &quot;&lt;/pre&gt;&quot;;
 }
 
-// echo &quot;&lt;pre&gt;&quot;; echo print_r($counts, true); echo &quot;&lt;/pre&gt;&quot;;
 
 $json_counts = json_encode($counts);
 
-// echo &quot;&lt;pre&gt;&quot;; echo print_r($json_counts, true); echo &quot;&lt;/pre&gt;&quot;;
-
-
-// $json_counts = strip_tags(stripslashes($json_counts));
-
-file_put_contents('/var/www/funkatron.com/htdocs/twitter-source-stats.json', $json_counts);
-
-// echo &quot;&lt;pre&gt;&quot;; echo print_r($http-&gt;getResponseMessage(), true); echo &quot;&lt;/pre&gt;&quot;;
-// echo &quot;&lt;pre&gt;&quot;; echo print_r($json, true); echo &quot;&lt;/pre&gt;&quot;;
+if ($json_counts) {
+	file_put_contents('/var/www/funkatron.com/htdocs/twitter-source-stats.json', $json_counts);
+}
\ No newline at end of file</diff>
      <filename>bin/funkatron_twitterpublic_min.php</filename>
    </modified>
    <modified>
      <diff>@@ -123,7 +123,7 @@ $config['subclass_prefix'] = 'MY_';
 | DO NOT CHANGE THIS UNLESS YOU FULLY UNDERSTAND THE REPERCUSSIONS!!
 |
 */
-$config['permitted_uri_chars'] = 'a-z 0-9~%.:_\,-';
+$config['permitted_uri_chars'] = 'a-z 0-9~%.:_\,+#-';
 
 
 /*</diff>
      <filename>config/config.php</filename>
    </modified>
    <modified>
      <diff>@@ -6,12 +6,30 @@ class Main extends Controller {
 
 	const JSON_FILE_PATH = '/var/www/funkatron.com/htdocs/twitter-source-stats.json';
 
+	const PAGE_TITLE     = 'Twitter Sources';
+
+	const HTTP_STATUS_OK = '200 OK';
+	const HTTP_STATUS_BAD_REQUEST = '400 Bad Request';
+	const HTTP_STATUS_INTERNAL_ERROR = '500 Internal Server Error';
+	
+	const HTTP_CONTENT_TYPE_JSON = 'Content-Type: application/json';
+
 	public function Main() {
 		parent::Controller();	
 	}
 	
-	public function index() {
+	/**
+	 * Returns the current source stats
+	 *
+	 * @param string $format Either 'html' or 'json'
+	 * @return void
+	 * @author Ed Finkler	
+	 */
+	public function index($format=&quot;html&quot;) {
 		$data = json_decode(file_get_contents(self::JSON_FILE_PATH));
+		
+
+		
 		$rows = $data-&gt;rows;
 		unset($data);
 		
@@ -19,60 +37,91 @@ class Main extends Controller {
 		
 		$view_data = $this-&gt;_prepDataForView($rows);
 		$view_data['last_updated']  = $last_mod;
+		if (strtolower($format) === &quot;json&quot;) {
+			
+			$this-&gt;_sendAsJSON($view_data);
+			return;
+		}
+
+		$view_data['page_title']  = self::PAGE_TITLE;
 		
 		$this-&gt;load-&gt;view('main', $view_data);
 	}
 
 
-	public function today()
+	public function today($format=&quot;html&quot;)
 	{
 		$this-&gt;load-&gt;model('msources');
 		$rows = $this-&gt;msources-&gt;getStatsForPeriod('today',1);
 		
 		$view_data = $this-&gt;_prepDataForView($rows);
 		$view_data['last_updated']  = filemtime(self::JSON_FILE_PATH);
+		if (strtolower($format) === &quot;json&quot;) {
+			
+			$this-&gt;_sendAsJSON($view_data);
+			return;
+		}
 		$view_data['period_start']  = 'today';
 		$view_data['period_duration'] = 1;
+		$view_data['page_title']  = self::PAGE_TITLE;
 
 		
 		$this-&gt;load-&gt;view('main', $view_data);
 	}	
 	
 	
-	public function yesterday()
+	public function yesterday($format=&quot;html&quot;)
 	{
 		$this-&gt;load-&gt;model('msources');
 		$rows = $this-&gt;msources-&gt;getStatsForPeriod('yesterday',1);
 		
 		$view_data = $this-&gt;_prepDataForView($rows);
 		$view_data['last_updated']  = filemtime(self::JSON_FILE_PATH);
+		if (strtolower($format) === &quot;json&quot;) {
+			
+			$this-&gt;_sendAsJSON($view_data);
+			return;
+		}
 		$view_data['period_start']  = 'yesterday';
 		$view_data['period_duration'] = 1;
+		$view_data['page_title']  = self::PAGE_TITLE;
 		
 		$this-&gt;load-&gt;view('main', $view_data);
 	}
 
-	public function lastsevendays() {
+	public function lastsevendays($format=&quot;html&quot;) {
 		$this-&gt;load-&gt;model('msources');
 		$rows = $this-&gt;msources-&gt;getStatsForPeriod('-1 week',7);
 		
 		$view_data = $this-&gt;_prepDataForView($rows);
 		$view_data['last_updated']  = filemtime(self::JSON_FILE_PATH);
+		if (strtolower($format) === &quot;json&quot;) {
+			
+			$this-&gt;_sendAsJSON($view_data);
+			return;
+		}
 		$view_data['period_start']  = '-1 week';
 		$view_data['period_duration'] = 7;
+		$view_data['page_title']  = self::PAGE_TITLE;
 		
 		$this-&gt;load-&gt;view('main', $view_data);
 		
 	}
 
-	public function lastmonth() {
+	public function lastmonth($format=&quot;html&quot;) {
 		$this-&gt;load-&gt;model('msources');
 		$rows = $this-&gt;msources-&gt;getStatsForPeriod('-1 month',30);
 		
 		$view_data = $this-&gt;_prepDataForView($rows);
 		$view_data['last_updated']  = filemtime(self::JSON_FILE_PATH);
+		if (strtolower($format) === &quot;json&quot;) {
+			
+			$this-&gt;_sendAsJSON($view_data);
+			return;
+		}
 		$view_data['period_start']  = '-1 month';
 		$view_data['period_duration'] = 30;
+		$view_data['page_title']  = self::PAGE_TITLE;
 		
 		$this-&gt;load-&gt;view('main', $view_data);
 		
@@ -80,6 +129,27 @@ class Main extends Controller {
 	
 	
 	
+	/**
+	 * takes a php structure (an array or object) and serves it as JSON
+	 *
+	 * @param object $data 
+	 * @param string $status 
+	 * @return void
+	 * @author Ed Finkler
+	 */
+	private function _sendAsJSON($data, $status = self::HTTP_STATUS_OK)
+	{
+		$rsJSON = json_encode($data);
+		$this-&gt;output-&gt;set_header(&quot;HTTP/1.0 &quot;.$status);
+		$this-&gt;output-&gt;set_header(&quot;HTTP/1.1 &quot;.$status);
+		$this-&gt;output-&gt;set_header(self::HTTP_CONTENT_TYPE_JSON);
+		$this-&gt;output-&gt;set_output($rsJSON);
+		return;
+	}	
+	
+	
+
+	
 	private function _prepDataForView(array $rows)
 	{
 		$total  = 0;
@@ -131,11 +201,27 @@ class Main extends Controller {
 		$view_data['source_counts'] = $counts;
 		$view_data['sources_total'] = $total;
 		$view_data['chart_url']     = $url;
+		$view_data['source_percentages'] = $this-&gt;_calcPercentages($view_data);
+
 		
 		return $view_data;
 	}
 	
-	
+
+	private function _calcPercentages($view_data) {
+		
+		// echo &quot;&lt;pre&gt;&quot;; echo print_r($view_data, true); echo &quot;&lt;/pre&gt;&quot;;
+		
+		$view_data['source_percentages'] = array();
+		
+		foreach ($view_data['source_counts'] as $key=&gt;$val) {
+			$per = number_format(($val/$view_data['sources_total'])*100, 3);
+			$view_data['source_percentages'][$key] = $per;
+		}
+		
+		return $view_data['source_percentages'];
+	}
+
 	
 	private function _makeGchartUrl(array $labels, array $values, array $colors)
 	{</diff>
      <filename>controllers/main.php</filename>
    </modified>
    <modified>
      <diff>@@ -1,12 +1,12 @@
 &lt;?php
-error_reporting('on');
+ini_set('display_errors','on');
 require_once('/var/www/shared/gChart2.php');
 
 class Trends extends Controller {
 
 	const JSON_FILE_PATH = '/var/www/funkatron.com/htdocs/twitter-source-stats.json';
 
-	const PAGE_TITLE     = 'Twitter Trend Stats';
+	const PAGE_TITLE     = 'Twitter Trends';
 
 	public function Trends() {
 		parent::Controller();	
@@ -30,7 +30,7 @@ class Trends extends Controller {
 		$view_data['period_interval'] = 'hour';
 		$view_data['period_start']  = '-1 hour';
 		$view_data['period_duration'] = 1;
-		$view_data['page_title']  = self::PAGE_TITLE;
+		$view_data['page_title']  = self::PAGE_TITLE . &quot;: Last Hour&quot;;
 		
 		
 		$this-&gt;load-&gt;view('trends', $view_data);
@@ -55,7 +55,7 @@ class Trends extends Controller {
 		$view_data['period_interval'] = 'hour';
 		$view_data['period_start']  = '-12 hours';
 		$view_data['period_duration'] = 12;
-		$view_data['page_title']  = self::PAGE_TITLE;
+		$view_data['page_title']  = self::PAGE_TITLE . &quot;: Last 12 Hours&quot;;
 		
 		
 		$this-&gt;load-&gt;view('trends', $view_data);
@@ -80,7 +80,7 @@ class Trends extends Controller {
 		$view_data['period_interval'] = 'hour';
 		$view_data['period_start']  = '-'.$duration.' hours';
 		$view_data['period_duration'] = $duration;
-		$view_data['page_title']  = self::PAGE_TITLE;
+		$view_data['page_title']  = self::PAGE_TITLE . &quot;: Last 24 Hours&quot;;
 		
 		
 		$this-&gt;load-&gt;view('trends', $view_data);
@@ -136,7 +136,7 @@ class Trends extends Controller {
 		// $view_data['last_updated']  = filemtime(self::JSON_FILE_PATH);
 		$view_data['period_start']  = 'today';
 		$view_data['period_duration'] = 1;
-		$view_data['page_title']  = self::PAGE_TITLE;
+		$view_data['page_title']  = self::PAGE_TITLE . &quot;: Today&quot;;
 		
 		
 		$this-&gt;load-&gt;view('trends', $view_data);
@@ -148,12 +148,10 @@ class Trends extends Controller {
 		$this-&gt;load-&gt;model('mtrends');
 		$rows = $this-&gt;mtrends-&gt;getStatsForPeriod('yesterday',1);
 		
-		// echo &quot;&lt;pre&gt;&quot;; echo print_r($rows, true); echo &quot;&lt;/pre&gt;&quot;;
-		
 		$view_data = $this-&gt;_prepDataForView($rows);
 		$view_data['period_start']  = 'yesterday';
 		$view_data['period_duration'] = 1;
-		$view_data['page_title']  = self::PAGE_TITLE;
+		$view_data['page_title']  = self::PAGE_TITLE . &quot;: Yesterday&quot;;
 		
 		$this-&gt;load-&gt;view('trends', $view_data);
 	}
@@ -165,7 +163,7 @@ class Trends extends Controller {
 		$view_data = $this-&gt;_prepDataForView($rows);
 		$view_data['period_start']  = '-7 days';
 		$view_data['period_duration'] = 7;
-		$view_data['page_title']  = self::PAGE_TITLE;
+		$view_data['page_title']  = self::PAGE_TITLE . &quot;: Last Seven Days&quot;;
 		
 		$this-&gt;load-&gt;view('trends', $view_data);
 		
@@ -178,7 +176,7 @@ class Trends extends Controller {
 		$view_data = $this-&gt;_prepDataForView($rows);
 		$view_data['period_start']  = '-1 month';
 		$view_data['period_duration'] = 30;
-		$view_data['page_title']  = self::PAGE_TITLE;
+		$view_data['page_title']  = self::PAGE_TITLE . &quot;: Last Month&quot;;
 		
 		$this-&gt;load-&gt;view('trends', $view_data);
 		
@@ -189,9 +187,24 @@ class Trends extends Controller {
 	
 	private function _showTermStats($rows, $period_start, $num_hours)
 	{
+		$offset_secs = (int)date('Z');
+		
 		// build arrays
 		$xlabels = array_keys($rows[0]-&gt;points);
+		
+		// echo &quot;&lt;pre&gt;&quot;; echo print_r($xlabels, true); echo &quot;&lt;/pre&gt;&quot;;
+		
 		array_shift($xlabels);
+		
+		
+		
+		/*
+			Adjust for GMT so labels show local server time
+		*/
+		foreach($xlabels as &amp;$val) {
+			$val = $val+$offset_secs;
+		}
+		
 		$maxval = 0;
 		foreach($rows as $row) {
 			
@@ -201,7 +214,7 @@ class Trends extends Controller {
 			
 			// echo &quot;&lt;pre&gt;&quot;; echo print_r($maxval, true); echo &quot;&lt;/pre&gt;&quot;;
 			foreach($row-&gt;points as &amp;$val) {
-				$val = round(($val/$maxval)*100,2);
+				$val = round(($val/$maxval)*100,0);
 			}
 			
 			ksort($row-&gt;points);
@@ -219,8 +232,8 @@ class Trends extends Controller {
 		/*
 			make view_data array
 		*/
-		$view_data['source_counts'] = $counts;
-		$view_data['sources_total'] = $total;
+		// $view_data['source_counts'] = $counts;
+		// $view_data['sources_total'] = $total;
 		$view_data['chart_url']     = $url;
 
 		
@@ -228,7 +241,10 @@ class Trends extends Controller {
 		$view_data['period_interval'] = 'hour';
 		$view_data['period_start']    = $period_start;
 		$view_data['period_duration'] = $num_hours;
-		$view_data['page_title']  = self::PAGE_TITLE;
+		$view_data['terms']           = $terms;
+		$terms_str = implode(', ', $terms);
+		$view_data['page_title']  = self::PAGE_TITLE . &quot; for &quot; . urldecode($terms_str);
+		
 		
 		
 		$this-&gt;load-&gt;view('trends', $view_data);
@@ -321,29 +337,71 @@ class Trends extends Controller {
 	
 	
 	
-	private function _makeGchartBarUrl(array $labels, array $values, array $colors)
+	private function _makeGchartBarUrl(array $xlabels, array $yvals, array $colors, array $terms)
 	{
+		$max_x = 100;
+		$points = count($xlabels);
+		// echo &quot;&lt;pre&gt;&quot;; echo print_r($xlabels, true); echo &quot;&lt;/pre&gt;&quot;;
+		// echo &quot;&lt;pre&gt;&quot;; echo print_r($yvals, true); echo &quot;&lt;/pre&gt;&quot;;
+		// echo &quot;&lt;pre&gt;&quot;; echo print_r($terms, true); echo &quot;&lt;/pre&gt;&quot;;
+
+		$interval = round($max_x/($points-1),0);
+		
+		$xaxis = array();
+		for ($x=0; $x&lt;$points; $x++) {
+			if ($interval*$x &gt; $max_x) {
+				$xaxis[] = $max_x;
+			} else {
+				$xaxis[] = $interval*$x;
+			}
+
+		}
+		$xaxis = implode(',',$xaxis);
+		
+		
 		$url  = &quot;http://chart.apis.google.com/chart?&quot;;
 
 		// type
-		$url .= &quot;&amp;cht=bvs&quot;;
+		$url .= &quot;&amp;cht=bvg&quot;;
 
 		// size
 		$url .= &quot;&amp;chs=700x300&quot;;
-		$url .= &quot;&amp;chbh=10&quot;;
+		$url .= &quot;&amp;chbh=3,0,12&quot;;
 
 		// data
-		$url .= &quot;&amp;chd=t:&quot;.implode(',',$values);
-		// $url .= &quot;&amp;chds=1,100&quot;;
+		
+		foreach($yvals as $yval) {
+			$x_y = array();
+			// $x_y['x']=$xaxis;
+			$x_y['y']=$yval;
+			$data_sets[] = implode(&quot;|&quot;, $x_y);
+		}
+		$data_string = implode(&quot;|&quot;, $data_sets);
+		$url .= &quot;&amp;chd=t:$data_string&quot;;
+		
+		// $url .= &quot;&amp;chd=t:&quot;.implode(&quot;|&quot;,$yvals);
+
+
+		// x-axis labels
+		$url .= &quot;&amp;chxt=x&quot;;
+		foreach($xlabels as $unixtime) {
+			$xlbls[] = urlencode(date('ga', $unixtime));
+		}
+		$url .= &quot;&amp;chxl=0:|&quot;.implode('|', $xlbls);
 
 		// colors
-		$url .= &quot;&amp;chco=&quot;.implode('|', $colors);
+		$url .= &quot;&amp;chco=&quot;.implode(',', $colors);
 
 		// legend
-		$url .= &quot;&amp;chdl=&quot;.implode('|', $labels);
-		// $url .= &quot;&amp;chdlp=b&quot;;
+		$url .= &quot;&amp;chdl=&quot;.implode('|', $terms);
+		$url .= &quot;&amp;chdlp=b&quot;;
 		
+		// gridlines
+		echo &quot;&lt;pre&gt;&quot;; echo print_r($interval, true); echo &quot;&lt;/pre&gt;&quot;;
+		$url .= &quot;&amp;chg=&quot;.$interval.&quot;,10,1,3&quot;;
+		$url .= &quot;&amp;chf=c,ls,0,FFFFCC,&quot;.($interval/100).&quot;,FFFFFF,&quot;.($interval/100);
 		
+		// echo &quot;&lt;textarea style='width:95%;height:100px'&gt;$url&lt;/textarea&gt;&quot;;
 		
 		return $url;
 	}
@@ -354,6 +412,13 @@ class Trends extends Controller {
 		
 		$max_x = 100;
 		$points = count($xlabels);
+		
+		// foreach($yvals as &amp;$yvalstr) {
+		// 	$yvals = explode(',', $yvalstr);
+		// 	$yvalstr = $this-&gt;array_to_extended_encoding($yvals);
+		// }
+		
+		
 		// echo &quot;&lt;pre&gt;&quot;; echo print_r($xlabels, true); echo &quot;&lt;/pre&gt;&quot;;
 		// echo &quot;&lt;pre&gt;&quot;; echo print_r($yvals, true); echo &quot;&lt;/pre&gt;&quot;;
 		// echo &quot;&lt;pre&gt;&quot;; echo print_r($terms, true); echo &quot;&lt;/pre&gt;&quot;;
@@ -419,6 +484,33 @@ class Trends extends Controller {
 	
 	
 	
+	private function _gchartEncodeExtended($string) {
+		$characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-.';
+		$first = floor($value / 64);
+        $second = $value % 64;
+        $encoding .= $characters[$first] . $characters[$second];
+		return $encoding;
+	}
+	
+	
+	private function array_to_extended_encoding($array)
+	{
+	    $characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-.';
+
+	    $encoding = '';
+	    foreach ($array as $value) {
+	        $first = floor($value / 64);
+	        $second = $value % 64;
+	        $encoding .= $characters[$first] . $characters[$second];
+			// echo &quot;&lt;pre&gt;&quot;; echo print_r($value, true); echo &quot;&lt;/pre&gt;&quot;;
+			// echo &quot;&lt;pre&gt;&quot;; echo print_r($encoding, true); echo &quot;&lt;/pre&gt;&quot;;
+	    }
+
+	    return $encoding;
+	}
+	
+	
+	
 	
 	private function _random_color($type='any'){
 		</diff>
      <filename>controllers/trends.php</filename>
    </modified>
    <modified>
      <diff>@@ -62,7 +62,7 @@ class MSources extends Model {
 		/*
 			cache has expired or DNE, so fall through and query
 		*/
-		$url = &quot;http://127.0.0.1:5984/publictweets/_view/source/byday?group=true&amp;startkey=$startkey&amp;endkey=$endkey&quot;;
+		$url = &quot;http://127.0.0.1:5984/publictweets_sources/_view/sources/byday?group=true&amp;startkey=$startkey&amp;endkey=$endkey&quot;;
 		// echo &quot;&lt;pre&gt;&quot;; echo print_r($url, true); echo &quot;&lt;/pre&gt;&quot;;
 		$json_counts = file_get_contents($url);
 		// echo &quot;&lt;pre&gt;&quot;; echo print_r($json_counts, true); echo &quot;&lt;/pre&gt;&quot;;</diff>
      <filename>models/msources.php</filename>
    </modified>
    <modified>
      <diff>@@ -21,7 +21,16 @@ class MTrends extends Model {
 	 */
 	public function getStatsForPeriod($date_start, $num_days, $nocache=null)
 	{
+		$offset_secs = (int)date('Z');
+		
+		// if ($term) { $nocache='nocache'; }
+		
+		// echo &quot;&lt;pre&gt;&quot;; echo print_r($term, true); echo &quot;&lt;/pre&gt;&quot;;
 		$date_start = strtotime($date_start);
+		// echo &quot;&lt;pre&gt;&quot;; echo print_r($date_start, true); echo &quot;&lt;/pre&gt;&quot;;
+		// $date_start = $date_start + $offset_secs; // trends data is stored in GMT, so adjust for offset
+		// echo &quot;&lt;pre&gt;&quot;; echo print_r($date_start, true); echo &quot;&lt;/pre&gt;&quot;;
+		
 		/*
 			create start key
 		*/
@@ -30,7 +39,11 @@ class MTrends extends Model {
 		$d = date('j', $date_start);
 		$startkey = &quot;[$Y,$m,$d]&quot;;
 		
+		// echo &quot;&lt;pre&gt;&quot;; echo print_r(date(&quot;Y-m-d H:i:s&quot;, $date_start), true); echo &quot;&lt;/pre&gt;&quot;;
+		
 		$date_end = $date_start+($num_days*24*60*60);
+		
+		// echo &quot;&lt;pre&gt;&quot;; echo print_r(date(&quot;Y-m-d H:i:s&quot;, $date_end), true); echo &quot;&lt;/pre&gt;&quot;;
 		/*
 			create end key
 		*/
@@ -42,6 +55,9 @@ class MTrends extends Model {
 		$hashkey = 'TRENDS-'.$startkey.'_to_'.$endkey;
 		
 		
+		// echo &quot;&lt;pre&gt;&quot;; echo print_r($hashkey, true); echo &quot;&lt;/pre&gt;&quot;;
+		
+		
 		/*
 			Cache lookup
 		*/
@@ -118,10 +134,16 @@ class MTrends extends Model {
 	public function getStatsForHourPeriod($datetime_start, $num_hours, $term=null, $nocache=null)
 	{
 		
+		$offset_secs = (int)date('Z');
+		
 		if ($term) { $nocache='nocache'; }
 		
 		// echo &quot;&lt;pre&gt;&quot;; echo print_r($term, true); echo &quot;&lt;/pre&gt;&quot;;
 		$date_start = strtotime($datetime_start);
+		// echo &quot;&lt;pre&gt;&quot;; echo print_r($date_start, true); echo &quot;&lt;/pre&gt;&quot;;
+		// $date_start = $date_start + $offset_secs; // trends data is stored in GMT, so adjust for offset
+		// echo &quot;&lt;pre&gt;&quot;; echo print_r($date_start, true); echo &quot;&lt;/pre&gt;&quot;;
+		
 		/*
 			create start key
 		*/
@@ -208,8 +230,9 @@ class MTrends extends Model {
 		/*
 			Cache this
 		*/
-		file_put_contents(self::CACHE_PATH.&quot;$hashkey&quot;, json_encode($statObjs));
-		
+		if (!$nocache) {
+			file_put_contents(self::CACHE_PATH.&quot;$hashkey&quot;, json_encode($statObjs));
+		}
 		
 
 		
@@ -243,8 +266,10 @@ class MTrends extends Model {
 		
 		$terms = explode(',',$term);
 		
-		// echo &quot;&lt;pre&gt;&quot;; echo print_r($term, true); echo &quot;&lt;/pre&gt;&quot;;
+		$offset_secs = (int)date('Z');
 		$date_start = strtotime($datetime_start);
+		$date_start = $date_start - $offset_secs; // trends data is stored in GMT, so adjust for offset
+
 		/*
 			create start key
 		*/
@@ -273,7 +298,7 @@ class MTrends extends Model {
 		*/
 		$samplepoints = array();
 		
-		for ($x=$unixstart; $x&lt;$unixend; $x += (60*60)) {
+		for ($x=$unixstart; $x&lt;=$unixend; $x += (60*60)) {
 			$samplepoints[$x] = 0;
 		}
 		// echo &quot;&lt;pre&gt;&quot;; echo print_r($samplepoints, true); echo &quot;&lt;/pre&gt;&quot;;
@@ -322,7 +347,11 @@ class MTrends extends Model {
 			foreach ($result-&gt;rows as $row) {
 				// echo &quot;&lt;pre&gt;&quot;; var_dump($row); echo &quot;&lt;/pre&gt;&quot;;
 				if (strtolower($row-&gt;key[4]) == strtolower($term)) {
-					$unixhour = strtotime($row-&gt;key[0].&quot;-&quot;.($row-&gt;key[1]+1).&quot;-&quot;.$row-&gt;key[2].' '.($row-&gt;key[3]+1).&quot;:00:00&quot;);
+					
+					// $strtime = $row-&gt;key[0].&quot;-&quot;.($row-&gt;key[1]+1).&quot;-&quot;.$row-&gt;key[2].' '.($row-&gt;key[3]+1).&quot;:00:00&quot;;
+					// echo &quot;&lt;pre&gt;&quot;; echo print_r($strtime, true); echo &quot;&lt;/pre&gt;&quot;;
+					// $unixhour = strtotime($strtime);
+					$unixhour = mktime($row-&gt;key[3]+1, 0, 0, $row-&gt;key[1]+1, $row-&gt;key[2], $row-&gt;key[0]);
 					// echo date(&quot;Y-m-d H:i:s&quot;, $unixhour).&quot;&lt;br&gt;&quot;;
 					$stats-&gt;points[$unixhour] = $row-&gt;value;
 				}
@@ -337,8 +366,9 @@ class MTrends extends Model {
 		/*
 			Cache this
 		*/
-		file_put_contents(self::CACHE_PATH.&quot;$hashkey&quot;, json_encode($statObjs));
-		
+		if (!$nocache) {
+			file_put_contents(self::CACHE_PATH.&quot;$hashkey&quot;, json_encode($statObjs));
+		}
 		
 
 		</diff>
      <filename>models/mtrends.php</filename>
    </modified>
    <modified>
      <diff>@@ -1,9 +1,10 @@
-	&lt;script src=&quot;http://www.google-analytics.com/urchin.js&quot; type=&quot;text/javascript&quot;&gt;
+	&lt;script type=&quot;text/javascript&quot;&gt;
+	var gaJsHost = ((&quot;https:&quot; == document.location.protocol) ? &quot;https://ssl.&quot; : &quot;http://www.&quot;);
+	document.write(unescape(&quot;%3Cscript src='&quot; + gaJsHost + &quot;google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E&quot;));
 	&lt;/script&gt;
 	&lt;script type=&quot;text/javascript&quot;&gt;
-	_uacct = &quot;UA-2032506-1&quot;;
-	urchinTracker();
+	var pageTracker = _gat._getTracker(&quot;UA-2032506-1&quot;);
+	pageTracker._trackPageview();
 	&lt;/script&gt;
-
 &lt;/body&gt;
 &lt;/html&gt;</diff>
      <filename>views/emb_footer.php</filename>
    </modified>
    <modified>
      <diff>@@ -16,6 +16,14 @@
 			padding:0;
 			margin:0;
 		}
+		
+		
+		A:link, .clickable {
+			color: #00C;
+			text-decoration: underline;
+			cursor:pointer;
+		}
+		
 		#container {
 			margin-left:auto;
 			margin-right:auto;
@@ -83,6 +91,22 @@
 			
 			line-height:1.5em;
 		}
+
+		#adsense {
+			font-size:9pt;
+			padding:10px;
+			margin:10px;
+			background-color:#CCCCFF;
+			border-top:3px double #333333;
+			border-bottom:3px double #333333;
+			
+			color:#000000;
+			margin-left:auto;
+			margin-right:auto;
+			width:500px;
+			
+			line-height:1.5em;
+		}
 		
 		#ad {
 			font-size:9pt;
@@ -100,10 +124,31 @@
 			line-height:1.5em;
 		}
 		
+		#terms-links {
+			margin-left:auto;
+			margin-right:auto;
+			width:500px;
+		}
+		
 		#note p+p {
 /*			padding:5px 0 0 0 ;*/
 			margin: .5em 0 0 0;
 		}
+		
+		#comparisons {
+			line-height:1.5em;
+		}
+		label { margin-right: .2em;}
+		#compare {
+			width:300px;
+		}
+		#compare-notes {
+			font-style: italic;
+			font-size:10pt;
+			color:#666;
+		}
+
+		
 	&lt;/style&gt;
 	
 	&lt;style type=&quot;text/css&quot; media=&quot;screen&quot;&gt;</diff>
      <filename>views/emb_header.php</filename>
    </modified>
    <modified>
      <diff>@@ -24,6 +24,20 @@
 	
 		&lt;div id=&quot;chart&quot;&gt;&lt;img src=&quot;&lt;?php echo $chart_url?&gt;&quot; /&gt;&lt;/div&gt;
 
+		&lt;div id=&quot;adsense&quot;&gt;
+			&lt;script type=&quot;text/javascript&quot;&gt;&lt;!--
+			google_ad_client = &quot;pub-7378172068209306&quot;;
+			/* Twitter Stats 468x60 */
+			google_ad_slot = &quot;7756658761&quot;;
+			google_ad_width = 468;
+			google_ad_height = 60;
+			//--&gt;
+			&lt;/script&gt;
+			&lt;script type=&quot;text/javascript&quot;
+			src=&quot;http://pagead2.googlesyndication.com/pagead/show_ads.js&quot;&gt;
+			&lt;/script&gt;
+		&lt;/div&gt;
+
 		&lt;div id=&quot;note&quot;&gt;
 		This data is derived from 20-message slices taken every minute from the Twitter public timeline. It currently reports the overall usage since the data collection began (2008-07-31), as well as a few common time periods.&lt;p /&gt;
 </diff>
      <filename>views/main.php</filename>
    </modified>
    <modified>
      <diff>@@ -29,6 +29,44 @@
 		
 	
 		&lt;div id=&quot;chart&quot;&gt;&lt;img src=&quot;&lt;?php echo $chart_url?&gt;&quot; /&gt;&lt;/div&gt;
+		&lt;!--  --&gt;
+
+		&lt;div id=&quot;adsense&quot;&gt;
+			&lt;script type=&quot;text/javascript&quot;&gt;&lt;!--
+			google_ad_client = &quot;pub-7378172068209306&quot;;
+			/* Twitter Stats Topics 468x15, created 10/16/08 */
+			google_ad_slot = &quot;9098091585&quot;;
+			google_ad_width = 468;
+			google_ad_height = 15;
+			//--&gt;
+			&lt;/script&gt;
+			&lt;script type=&quot;text/javascript&quot;
+			src=&quot;http://pagead2.googlesyndication.com/pagead/show_ads.js&quot;&gt;
+			&lt;/script&gt;
+			&lt;script type=&quot;text/javascript&quot;&gt;&lt;!--
+			// google_ad_client = &quot;pub-7378172068209306&quot;;
+			// /* Twitter Stats 468x60 */
+			// google_ad_slot = &quot;7756658761&quot;;
+			// google_ad_width = 468;
+			// google_ad_height = 60;
+			//--&gt;
+			&lt;/script&gt;
+			&lt;!-- &lt;script type=&quot;text/javascript&quot;
+			src=&quot;http://pagead2.googlesyndication.com/pagead/show_ads.js&quot;&gt;
+			&lt;/script&gt; --&gt;
+		&lt;/div&gt;
+
+		&lt;?php if (isset($terms) &amp;&amp; is_array($terms) &amp;&amp; count($terms)&gt;0): ?&gt;
+			&lt;div id=&quot;terms-links&quot;&gt;
+				&lt;h3&gt;Search Twitter for&#8230;&lt;/h3&gt;
+				&lt;ul&gt;
+					&lt;?php foreach ($terms as $value): ?&gt;
+						&lt;li&gt;&lt;a href=&quot;http://search.twitter.com/search?q=&lt;?=urldecode($value)?&gt;&quot; target=&quot;_blank&quot; title=&quot;Search for post containing &amp;quot;&lt;?=urldecode($value)?&gt;&amp;quot;&quot;&gt;&lt;?=urldecode($value)?&gt;&lt;/a&gt;&lt;/li&gt;
+					&lt;?php endforeach ?&gt;
+				&lt;/ul&gt;
+
+			&lt;/div&gt;			
+		&lt;?php endif ?&gt;
 
 		&lt;div id=&quot;note&quot;&gt;
 		This data is derived from the Twitter Trends API method. Samples are taken every 5 minutes. Data collection began on 2008-09-23.&lt;p /&gt;
@@ -51,23 +89,99 @@
 		&lt;/div&gt;
 
 
-		&lt;table id=&quot;full-list&quot; class=&quot;tablesorter&quot;&gt;
-			&lt;thead&gt;
-			&lt;tr&gt;
-				&lt;th&gt;Term&lt;/th&gt;
-				&lt;th&gt;%&lt;/th&gt;
-			&lt;/tr&gt;
-			&lt;/thead&gt;
-			&lt;tbody&gt;
-			&lt;?foreach($source_counts as $key=&gt;$val):?&gt;
-			&lt;tr&gt;
-				&lt;td&gt;&lt;?=$key?&gt; [&lt;a title=&quot;Search for post containing &amp;quot;&lt;?=$key?&gt;&amp;quot;&quot; href=&quot;http://search.twitter.com/search?q=&lt;?=urlencode($key)?&gt;&quot; target=&quot;_blank&quot;&gt;search&lt;/a&gt;]&lt;/td&gt;
-				&lt;td&gt;&lt;?=number_format(($val/$sources_total)*100, 3)?&gt;%&lt;/td&gt;
-			&lt;/tr&gt;
-			&lt;?endforeach;?&gt;
-			&lt;/tbody&gt;
-		&lt;/table&gt;
+		&lt;?php if (isset($source_counts) &amp;&amp; is_array($source_counts) &amp;&amp; sizeof($source_counts)&gt;0): ?&gt;
+			&lt;script type=&quot;text/javascript&quot; charset=&quot;utf-8&quot;&gt;
+				$().ready( function() {
+				
+					$('input#compare').val(''); // blank this out to get rid of firefox &quot;memory&quot;
+				
+					$('A.compare-link').click( function(e) {
+						e.stopPropagation();
+						var id = e.target.id;
+						var term = id.replace(/--term-/, '');
+						if (term) {
+							showCompare();
+							addTerm(term);
+						} 
+					} );
+				
+					$('form#compare-form').bind('submit', function() {
+						compare();
+					});
+					$('input#submit').bind('click', function() {
+						compare();
+					});
+				} );
+			
+			
+				function showCompare() {
+					if ($('#comparisons:hidden').length&gt;0) {
+						$('#comparisons').fadeIn('500');
+					}
+				
+				}
+			
+				function addTerm(term) {
+					var current_terms = $('#compare').val();
+					if (current_terms.length &gt; 0) {
+						$('#compare').val(current_terms+','+term);
+					} else {
+						$('#compare').val(term);
+					}
+				
+				
+				}
+			
+				function compare() {
+					var terms = $('#compare').val();
+					terms = $.trim(terms);
+					document.location = '&lt;?=site_url()?&gt;/trends/last24hours/'+encodeURIComponent(terms)
+				}
+			
+			
+			&lt;/script&gt;
+
+			&lt;div id=&quot;comparisons&quot; style=&quot;display:none; text-align:center&quot;&gt;
+				&lt;form id=&quot;compare-form&quot;&gt;
+					&lt;label for=&quot;compare&quot;&gt;Terms&lt;/label&gt; &lt;input type=&quot;text&quot; name=&quot;compare&quot; value=&quot;&quot; id=&quot;compare&quot; /&gt;
+					&lt;input type=&quot;button&quot; name=&quot;submit&quot; value=&quot;Compare&quot; id=&quot;submit&quot; /&gt;
+				&lt;/form&gt;
+			
+				&lt;div id=&quot;compare-notes&quot;&gt;Separate terms with commas&lt;/div&gt;
+			&lt;/div&gt;
+
 
+		
+		
+			&lt;table id=&quot;full-list&quot; class=&quot;tablesorter&quot;&gt;
+				&lt;thead&gt;
+				&lt;tr&gt;
+					&lt;th&gt;Term&lt;/th&gt;
+					&lt;th&gt;%&lt;/th&gt;
+					&lt;th&gt;Actions&lt;/th&gt;
+				&lt;/tr&gt;
+				&lt;/thead&gt;
+				&lt;tbody&gt;
+				&lt;?foreach($source_counts as $key=&gt;$val):?&gt;
+				&lt;tr&gt;
+					&lt;td&gt;
+						&lt;a title=&quot;Search for post containing &amp;quot;&lt;?=$key?&gt;&amp;quot;&quot; href=&quot;http://search.twitter.com/search?q=&lt;?=urlencode($key)?&gt;&quot; target=&quot;_blank&quot;&gt;&lt;?=$key?&gt;&lt;/a&gt;
+					&lt;/td&gt;
+					&lt;td&gt;&lt;?=number_format(($val/$sources_total)*100, 3)?&gt;%&lt;/td&gt;
+					&lt;td&gt;
+						[&lt;a title=&quot;Examine popularity of &amp;quot;&lt;?=$key?&gt;&amp;quot; over the last 12 hours&quot; href=&quot;&lt;?=site_url()?&gt;/trends/last12hours/&lt;?=$key?&gt;&quot;&gt;Chart last 12hrs&lt;/a&gt;]
+						[&lt;a title=&quot;Examine popularity of &amp;quot;&lt;?=$key?&gt;&amp;quot; over the last 24 hours&quot; href=&quot;&lt;?=site_url()?&gt;/trends/last24hours/&lt;?=$key?&gt;&quot;&gt;Chart last 24hrs&lt;/a&gt;]
+						[&lt;a title=&quot;Compare term popularity&quot; id=&quot;--term-&lt;?=$key?&gt;&quot; class=&quot;clickable compare-link&quot;&gt;Compare&#8230;&lt;/a&gt;]
+					&lt;/td&gt;
+				&lt;/tr&gt;
+				&lt;?endforeach;?&gt;
+				&lt;/tbody&gt;
+			&lt;/table&gt;
+		
+		
+		&lt;?php endif ?&gt;
+		
+		
 	&lt;/div&gt;
 
 	&lt;?php $this-&gt;load-&gt;view('emb_footer')?&gt;
\ No newline at end of file</diff>
      <filename>views/trends.php</filename>
    </modified>
  </modified>
  <removed type="array">
    <removed>
      <filename>views/welcome_message.php</filename>
    </removed>
  </removed>
  <parents type="array">
    <parent>
      <id>f338403b532dfbcd68abe8a2ebd3c8360aa5cded</id>
    </parent>
  </parents>
  <author>
    <name>Ed</name>
    <email>coj@funkatron.com</email>
  </author>
  <url>http://github.com/funkatron/twitter-stats-tracker/commit/590437975eb0a64c9a145bad11ed1936665a7ebb</url>
  <id>590437975eb0a64c9a145bad11ed1936665a7ebb</id>
  <committed-date>2008-12-12T15:13:29-08:00</committed-date>
  <authored-date>2008-12-12T15:13:29-08:00</authored-date>
  <message>- added, well, a bunch of stuff. JSON serving on main. etc.</message>
  <tree>a1449a9bfeebe3d2bb85a8f8c6120f00287c7134</tree>
  <committer>
    <name>Ed</name>
    <email>coj@funkatron.com</email>
  </committer>
</commit>
