var f_page = "thestartupcentre"; // the page name for your fan page, e.g. the 'wvumountaineers' part of http://facebook.com/wvumountaineers
var t_page = "startupcentre"; // the account name for your main twitter account

var fb_count = 0;
var twit_count = 0;

function add_commas(number) {
	if (number.length > 3) {
		var mod = number.length % 3;
		var output = (mod > 0 ? (number.substring(0,mod)) : '');
		for (i=0 ; i < Math.floor(number.length / 3); i++) {
			if ((mod == 0) && (i == 0)) {
				output += number.substring(mod+ 3 * i, mod + 3 * i + 3);
			} else {
				output+= ',' + number.substring(mod + 3 * i, mod + 3 * i + 3);	
			}
		}
		return (output);
	} else {
		return number;
	}
}

jQuery(document).ready(function(){
	jQuery('#metrics').html();
	var fb_count;
	// grab from facebook
	$.getJSON('https://graph.facebook.com/'+f_page+'?callback=?', function(data) {
		fb_count = data['likes'];
		//$('#fb_count').html(fb_count);
	});

	// grab from twitter
	$.getJSON('http://api.twitter.com/1/users/show.json?screen_name='+t_page+'&callback=?', function(data) {
		twit_count = data['followers_count'];
		total = 0;
		var fail = 0;
		if (parseInt(fb_count))
		{
			total += parseInt(fb_count);
		} else {
			fail = 1;
		}
		if (parseInt(ga_uniquevisits))
		{
		total += parseInt(ga_uniquevisits);
		} else {
			fail = 1;
		}
		if (parseInt(mailchimp_members))
		{
			total += parseInt(mailchimp_members);
		} else {
			fail = 1;
		}
		if (parseInt(twit_count))
		{
			total += parseInt(twit_count);
		} else {
			fail = 1;
		}
		if (fail == 1)
		{
			var cmreachplus = "+";
		} else {
			var cmreachplus = "";
		}
		twit_count = add_commas(total);
		var cnt = 0;
        var counter = setInterval(function() {
            if (cnt <= total) {
                $('#counter').html(cnt);
                cnt+=42;
            }
            else {
                clearInterval(counter);
                $('#counter').html(twit_count+cmreachplus);
            }
        }, 10);
	});
	
});
