//20100409 GEK Update counters on GraffitiTracker.com.
//20100427 GEK Made change to allow cross-site data access.  See the update_counters function.

	$(document).ready(function() {
		timeout = 5000; //Milliseconds between remote updates
		
		update_counters();
		
		//For testing force a update.  Remove when not needed.
		$('#update').click(function() {
			update_counters();
		});
	});

	//Access the remote URI and update the three counter areas on the page.
	function update_counters() {
		//Due to cross-site (Marketing site living on a different domain than the app) we must utilize JSONP for data encapsulation.
		//The below gt_remote_data_url var *must* end with "callback=?" and return JSON as in: $GET['callback']( jsonGoesHere )
		
		//Examples with static data:
		//If you're returning the data via PHP you would use the following:
		//<?php echo $_GET['callback']; ?>({"photo_counter":33100,"arrest_counter":976300,"restitution_counter":8300})
		
		//In a rails render action output:
		//params['callback'] + '(' + '{"photo_counter":33100,"arrest_counter":976300,"restitution_counter":8300}' + ')'
		
		// change this to relative address. reading from a cached local file.
	  gt_remote_data_url = '/gt_counter_get.php?callback=?';
		$.getJSON(gt_remote_data_url, function(data) {
				if ($('#photo_counter').html() != addCommas(addPhotosFromBuzzard(data.photo_counter))) {
					$('#photo_counter').fadeOut('fast',function() {
						$('#photo_counter').html(addCommas(addPhotosFromBuzzard(data.photo_counter))); //Update photo counter ID
						$('#photo_counter').fadeIn();
					});
				}
				
				if ($('#arrest_counter').html() != addCommas(addArrestsFromBuzzard(data.arrest_counter))) {
					$('#arrest_counter').fadeOut('fast',function() {
						$('#arrest_counter').html(addCommas(addArrestsFromBuzzard(data.arrest_counter))); //Update arrest counter ID
						$('#arrest_counter').fadeIn();
					});
				}
				
				if ($('#restitution_counter').html() != addCommas(addRestitutionFromBuzzard(data.restitution_counter))) {
					$('#restitution_counter').fadeOut('fast',function() {
						$('#restitution_counter').html(addCommas(addRestitutionFromBuzzard(data.restitution_counter))); //Update restitution counter ID
						$('#restitution_counter').fadeIn();
					});
				}
			});
		//});

		//Set the timer to reload it again
		timer=setTimeout("update_counters()",timeout);
	}
	
	function addPhotosFromBuzzard(number){
	  return number + 565870;  //Add the 565,870 photos that were analysed in the Buzzard system
	}
	
	function addArrestsFromBuzzard(number){
	  return number + 771;  //Add the 771 arrests tracked in the Buzzard system
	}
	
	function addRestitutionFromBuzzard(number){
	  totalRestitution = number + 423318.85;
	  return totalRestitution.toFixed(2);  //Add the total restitution tracked by Buzzard	
	}
	
	//Formats numbers with commas to make it prettier
	function addCommas(number) {
		number += '';
		x = number.split('.');
		x1 = x[0];
		x2 = x.length > 1 ? '.' + x[1] : '';
		var rgx = /(\d+)(\d{3})/;
		while (rgx.test(x1)) {
			x1 = x1.replace(rgx, '$1' + ',' + '$2');
		}
		return x1 + x2;
	}
