function getTweets() {
	$.ajax({url: 'http://twitter.nyjapps.com/Filter/Index/6', type: "GET", dataType: "jsonp"})
	.success(function (data) { writeTweets(data) });
}
	
function getCount() {
	$.ajax({url: 'http://twitter.nyjapps.com/Filter/GetTweetCount/6', type: "GET", dataType: "jsonp"})
	.success(function (data) { writeTweetCount(data) });
}

function writeTweets(tweetsJSON) {
		$("#tweets").empty();
        $.each(tweetsJSON["tweets"], function (i, tweet) {
			var htmlToInsert = '<div class="tweet">';
			htmlToInsert += '<div id="tweetText">';
			htmlToInsert += tweet.tweet_text;
			htmlToInsert += '</div>';
			htmlToInsert += '<div id="tweeter">';
			htmlToInsert += '<img class="tweeterPic" src="' + tweet.profile_image_url +'" alt="' + tweet.screen_name +'" />';
			htmlToInsert += '<span id="tweeterScreenName">@' + tweet.screen_name + '</span>';
			htmlToInsert += '</div>\n</div>';
					
            $("#tweets").append(htmlToInsert);
        });
        loopTweets();
}

function loopTweets() {
		$("div.tweet").each(function(index) {
            $(this).delay(5000*index).fadeIn(500).delay(4250).fadeOut(250);
        });
}
	
function writeTweetCount(countJSON) {
	var count = countJSON["count"];
	var currentCount = $("#count").html();
	if(currentCount == "&nbsp;") {		
		$("#count").hide().html(count).fadeIn(500);
	} else {
		if(count == currentCount) {
			return;
		} else {
			$("#count").fadeOut(500).html(count).fadeIn(500);
		}
	}
}

function getAndRunJustCount() {
	getCount();
	setTimeout(function() { getAndRunJustCount(); }, 20000);
}

function getAndRunBoth() {
	getCount();
	getTweets();
	setTimeout(function() { getAndRunBoth(); }, 25000);
}
// remap jQuery to $
(function($){})(window.jQuery);





/* optional triggers

$(window).load(function() {
	
});

$(window).resize(function() {
	
});

*/

