
// Settings
var twitter_update_interval = 30000;
var time_update_interval = 20000;
var update_interval = 4000;
var number_of_styles = 3;
var tweets_between_ads = 40;
var max_tweets = 50;
var terms = ["poop", "turd", "sharted", "poo", "shat", "fart", "toilet", "pooped", "pooping", "pee", "pee-pee", "poo-poo", "poopoo", "poops"];
var mp3_files = ["media/sounds/poop1.mp3", "media/sounds/poop2.mp3", "media/sounds/poop3.mp3", "media/sounds/poop4.mp3", "media/sounds/poop5.mp3", "media/sounds/poop6.mp3", "media/sounds/poop7.mp3", "media/sounds/poop8.mp3", "media/sounds/poop9.mp3", "media/sounds/poop10.mp3", "media/sounds/poop11.mp3"]

// Internals
var latest_id = null;
var tweets_since_last_ad = 0;
var update_queue = new Array();
var next_ad_id = 0;
var sounds = new Array();
var play_sounds = false;
var num_tweets = 0;
var update_timeout = null;
var query = "";
for (var i = 0; i < terms.length - 1; i++){
	query += terms[i] + "%20OR%20";	
}
query += terms[terms.length - 1]

// alert(query);

function display_data(data){
	if (play_sounds) {
		playRandomSound();
	}
	$('#tweets').css({paddingTop : 0});
	$('#tweets').prepend(get_tweet_html(data));
	$("#" + data.id).css({marginBottom: '-200px', opacity: 0.0});
	$('#tweets').animate({paddingTop: '20px'}, 2000);
	$("#" + data.id).animate({marginBottom: '-100px', opacity: 1.0}, 2000);	
	num_tweets++;
	if (num_tweets > max_tweets){
		$('#tweets > div:last').animate({'marginTop': '0px'}, 5000, "linear", function(){
			$(this).remove();			
		});		
	}
}

function get_tweet_html(data){
	var result = '<div id="' + data.id + '" class="tweet ' + get_random_style() + '"><img class="profile_image" src="';
	result += data.profile_image_url + '"/><div class="tweet_text_container">' + '<a href=http://twitter.com/';
	result += data.from_user + '>' + data.from_user + '</a> - ' + data.text;
	result += '</div><div class="tweet_time_container"><div class="tweet_time_iso">' + data.created_at;
	result += '</div><div class="tweet_time_text">' + get_fuzzy_elapsed_time(data.created_at) + '</div></div></div>';
	return result;
}

function get_fuzzy_elapsed_time(post_time){
	var result;
	var current_time = new Date();
	
	time = new Date();
	time.setTime(Date.parse(post_time));
	//current_time.setTime(current_time.getTime() + (current_time.getTimezoneOffset() * 60000)); // Adjust current time to UTC
	seconds = (current_time.getTime() - time.getTime()) / 1000;
	
	if (seconds < 15) {
		seconds = seconds | 0;
		if (seconds > 1 || seconds ==  0) {
			result = seconds + " seconds ago";
		}
		else {
			result = seconds + " second ago";
		}
	}
	else if (seconds < 30) {
		result = "Half a minute ago"
	}
	else if (seconds < 60) {
		result = "Less than a minute ago"
	}
	else if (seconds < 2700){ // 45 minutes
		minutes = seconds / 60 | 0;
		if (minutes > 1 || minutes ==  0){
			result = "about " + minutes + " minutes ago"
		}
		else{
			result = "about " + minutes + " minute ago"
		}
	}
	else if (seconds < 64800){ // 45 minutes
		hours = seconds / 60 / 60 | 0;
		if (hours > 1 || hours ==  0){
			result = "about " + hours + " hours ago"
		}
		else{
			result = "about " + hours + " hour ago"
		}
	}
	else { // 18 hours
		result = time.format("h:mm TT mmm ddd");
	}
	
	return result;
}

soundManager.debugMode = false;
soundManager.url = 'media/swf/';
soundManager.onload = function(){
	for (var i = 0; i < mp3_files.length; i++) {
		sounds[i] = soundManager.createSound({
    		id: 'poop' + i,
		    url: mp3_files[i],
		    volume: 50
		});		
	}
}

function playRandomSound(){
	i = (Math.random() * 100 | 0) % sounds.length;
	sounds[i].play();
}

function insert_ad(){
	// Probably load these from a config file eventually, perhaps with a small amount of php
	var now = new Date();
	var data = {
		'id': next_ad_id++, 
		'text': 'Follow Obtoose on Twitter, or just stay here watching poop all day. Sicko.', 
		'from_user': 'OBTOOSE', 
		'profile_image_url': 'media/images/obtooseLogo.gif', 
		'created_at': now.toUTCString()
		};
	display_data(data);
}

function queue_twitter_data(data){
	if (data.results.length > 0) {
		for (var i = data.results.length - 1; i >= 0; i--) {
			update_queue.push(data.results[i]);
		}
		latest_id = data.results[0].id;
		update_queue_size();
	}
}

function update_queue_size(){
	$('#queue_size').html(update_queue.length);
}

function update_times(){
	$('.tweet_time_container').each(function(){
		var created_at = $(this).children('.tweet_time_iso').text();		
		$(this).children('.tweet_time_text').text(get_fuzzy_elapsed_time(created_at));
	});	
	setTimeout(update_times, time_update_interval);
}

function update_twitter(){
	$.getJSON("http://search.twitter.com/search.json?q=" + query + "&since_id=" + latest_id + "&callback=?", null, function(data){queue_twitter_data(data);});
	setTimeout(update_twitter, twitter_update_interval);
}

function update(){
	if (tweets_since_last_ad >= tweets_between_ads){
		insert_ad();
		tweets_since_last_ad = 0;
	}
	else if (update_queue.length > 0) {
		display_data(update_queue.shift());
		tweets_since_last_ad++;
		update_queue_size();
	}	
	update_timeout = setTimeout(update, update_interval);
}

function get_random_style(){
	var result = 'tweetstyle';
	result += (Math.random() * 100 | 0) % number_of_styles;
	return result;
}

$(document).ready(function() {
	$('#speed').change(function(){
		update_interval = $('#speed option:selected').val();
	});
	
	$('#play_sounds').click(function(){
		play_sounds = !play_sounds;
		if (play_sounds){
			$(this).text("silent but deadly");
		}
		else {
			$(this).text("play sounds");
		}
	});
	
	
	$('.tweet').live('mouseover', function(){
			clearTimeout(update_timeout);			
		});
	$('.tweet').live('mouseout', function(){
			update_timeout = setTimeout(update, update_interval);
		}
	);
	
	update();
	update_twitter();
	update_times();
});