function format_currency(num)
{
	num = num.toString().replace(/\$|\,/g,'');
	if(isNaN(num))
		num = "0";
	sign = (num == (num = Math.abs(num)));
	num = Math.floor(num*100+0.50000000001);
	cents = num%100;
	num = Math.floor(num/100).toString();
	if(cents<10)
		cents = "0" + cents;
	for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
		num = num.substring(0,num.length-(4*i+3))+','+
	num.substring(num.length-(4*i+3));
	
	return (((sign)?'':'-') + '$' + num + '.' + cents);
}

function format_number(nStr)
{
	nStr += '';
	x = nStr.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;
}


var updateCount = 0;
function updateTotals()
{
	updateCount++;
	jQuery.ajax({
		      	//url: 'get_totals.php?up=' + updateCount,
                        url: 'get_totals/' + updateCount,
			success: function(JSONresponse){
					var totals = JSON.parse(JSONresponse);
					jQuery('#checkins').text(format_number(totals['checkins']));
					jQuery('#tweets').text(format_number(totals['tweets']));
					jQuery('#raised').text(format_currency(totals['raised']));
					setTimeout(updateTotals, 5000);
				}
		      });
}

var fsDelay = 5000;
var fsCount = 20;
var fsShowing = 10;
var fsInd = 0;

function fsMove(fsInd) {
	return function() {
	  $('#FeedItem'+fsInd).remove().css('display', 'none').prependTo('#FourSquareFeeds');
	}
}

function fsShift() {
	var toShow = (fsInd + fsShowing) % fsCount;
	if(toShow != fsInd)
	{
		$('#FeedItem'+toShow).slideDown(1000, fsMove(fsInd));
		$('#FeedItem'+fsInd).slideUp(1000, fsMove(fsInd));
	}
	fsInd = (fsInd + 1) % fsCount;

	setTimeout('fsShift()', fsDelay);
}

function initFSRotation()
{
	fsCount = jQuery('#FourSquareFeeds .FeedItem').size();
	fsShowing = Math.min(fsShowing, fsCount);
	
	/*jQuery('#FourSquareFeeds .FeedItem')
		.each(function(index)
			{
				var li = jQuery(this);
				var dif = fsCount - 1 - index;
				li.attr('id', 'FeedItem' + dif)
				if(dif > fsShowing - 1)
					li.css('display', 'none')
			});*/
	setTimeout('fsShift()', fsDelay);
}

var twDelay = 5000;
var twCount = 20;
var twShowing = 10;
var twInd = 0;

function initTWRotation()
{
	twCount = jQuery('#TwitterFeeds .FeedItem').size();
	twShowing = Math.min(twShowing, twCount);
	/*jQuery('#TwitterFeeds .FeedItem')
		.each(function(index)
			{
				var li = jQuery(this);
				var dif = twCount - 1 - index;
				li.attr('id', 'TwitterFeedItem' + dif)
				if(dif > fsShowing - 1)
					li.css('display', 'none')
			});*/
	setTimeout('twShift()', twDelay);
}

function twMove(twInd) {
	return function() {
	  $('#TwitterFeedItem'+twInd).remove().css('display', 'none').prependTo('#TwitterFeeds ul');
	}
}

function twShift() {
	var toShow = (twInd + twShowing) % twCount;
	if(toShow != twInd)
	{
		$('#TwitterFeedItem'+toShow).slideDown(1000, twMove(twInd));
		$('#TwitterFeedItem'+twInd).slideUp(1000, twMove(twInd));
	}
	twInd = (twInd + 1) % twCount;

	setTimeout('twShift()', twDelay);
} 

      
jQuery(function(){
	updateTotals();
	initFSRotation();
	initTWRotation();
})
