
// Settings
var twitter_update_interval = 30000;
var time_update_interval = 20000;
var update_interval = 4000;
var number_of_styles = 3;
var tweets_between_ads = 100;
var max_tweets = 50;
var terms = ["420", "bong", "blunt", "cannabis", "fourtwitty", "kush", "hash", "hemp", "hightimes", "marijuana", "norml", "stoner", "stoned", "weed"];

// Internals
var latest_id = null;
var tweets_since_last_ad = 0;
var update_queue = new Array();
var next_ad_id = 0;
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]

function display_data(data){
	
	$('#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: '-10px', opacity: 1.0}, 2000);	
	num_tweets++;
	if (num_tweets > max_tweets){
		$('#tweets > div:last').animate({'marginBottom': '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 + ' target=_blank>' + 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 EST
	seconds = (current_time.getTime() - time.getTime()) / 1000;
	
	if (seconds < 15) {
		seconds = seconds | 0;
		if (seconds > 1 || seconds ==  0) {
			result = "blazed " + seconds + " seconds ago";
		}
		else {
			result = "blazed " + seconds + " second ago";
		}
	}
	else if (seconds < 30) {
		result = "blazed a couple seconds ago"
	}
	else if (seconds < 60) {
		result = "blazed a minute ago"
	}
	else if (seconds < 2700){ // 45 minutes
		minutes = seconds / 60 | 0;
		if (minutes > 1 || minutes ==  0){
			result = "blazed " + minutes + " minutes ago"
		}
		else{
			result = "blazed " + minutes + " minute ago"
		}
	}
	else if (seconds < 64800){ // 45 minutes
		hours = seconds / 60 / 60 | 0;
		if (hours > 1 || hours ==  0){
			result = "blazed " + hours + " hours ago"
		}
		else{
			result = "blazed " + hours + " hour ago"
		}
	}
	else { // 18 hours
		result = time.format("h:mm TT mmm ddd");
	}
	
	return result;
}

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++, 
		'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 +"&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();
	});
	
	
	
	$('.tweet').live('mouseover', function(){
			clearTimeout(update_timeout);			
		});
	$('.tweet').live('mouseout', function(){
			update_timeout = setTimeout(update, update_interval);
		}
	);
	
	update();
	update_twitter();
	update_times();
});
