var V = {
	
	// text on bobble when loading
	_loadText: 'Loadzing f0m teh internetz',
	// how many tweets should be fetched
	_count: 1,
	// search.twitter.com url base
	_searchBase: 'http://search.twitter.com/search.json?callback=?',
	
	// ids => usernames
	_feedCount: 0,
	_feeds: {
		christian: 'ranza',
		rasmus: 'zorp',
		mark: 'mguld',
		jonas: 'jonasmaaloe',
		harisn: 'neziric',
		pero: 'pravimali',
		harisf: 'superoperater',
		armin: 'krule'
	},
	
	// stores tweets
	_tweets: {},
	
	interval: null,
	timer: 7500,
	
	// run!
	init: function() {
		
		for(i in V._feeds) {
			V._feedCount++;
		}
		
		// run the binds
		V.B.init($('.guys .guy'));
		V.E.showRandom();
		V.B.startTimer(V.timer);
	},
	
	// functions
	F: {
		//fetches the feeds for a user. Runs a callback on complete
		fetchFeed: function(user, callback) {
			// create an array for storing the tweets
			var tweets = V._tweets[user] = [];
		
			// sets the paramater for what we want to search for
			var params = {
				'q': 'from:' + V._feeds[user],
				'rpp': V._count
			};
		
			// do the search
			$.ajax({
				type: 'GET',
				url: V._searchBase,
				data: params,
				dataType: 'json',
				success: function(data) {
					$.each(data.results, function(index, val) {
						// push the tweets to our array
						tweets.push(val.text);
					});
				},
				error: function() {
					alert('Somethings wrong with the internetz!')
				},
				complete: function() {
					callback();
				}
			})
			
		},
		format: {
			// replaces # with link to search.twitter.com
			replaceHash: function(tweet) {
				var regexp = /[\#]+([A-Za-z0-9-_]+)/gi;
				return tweet.replace(regexp, '<a href="http://search.twitter.com/search?q=&tag=$1&lang=all">#$1</a>');
			},
			// replaces urls with real links
			replaceUrl: function(tweet) {
				var regexp = /((ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?)/gi;
				return tweet.replace(regexp, '<a href="$1">$1</a>');
			},
			// replaces @ with link to twitter.com/[user]
			replaceAt: function(tweet) {
				var regexp = /[\@]+([A-Za-z0-9-_]+)/gi;
				return tweet.replace(regexp, '<a href="http://twitter.com/$1">@$1</a>');
			}
		}
	},
	
	/* BINDS */
	B: {
		// runs the binds
		init: function(selector) {
			selector.bind('click', function() {
				
				V.B.stopTimer();
				
				var user = $(this).attr('id');
				
				V.E.setBobbleText(user, V._loadText);
				V.E.showBobble(user);
				
				if(V._tweets[user] == undefined) {
					V.F.fetchFeed(user, function() {
						V.E.setBobbleText(user, V._tweets[user][0]);
					});
				} else {
					V.E.setBobbleText(user, V._tweets[user][0]);
				}
				
			});
		},
		startTimer: function(timer) {
			// kill excisting timer
			V.B.stopTimer();
			
			// load up a new timer
			V.interval = setInterval(function() {
				V.E.showRandom();
			}, timer);
		},
		stopTimer: function() {
			clearInterval(V.interval);
		}
	},
	
	
	/* EVENTS */
	E: {
		showRandom: function() {
			
			var random = Math.random() * V._feedCount;
			random = Math.ceil(random);
			
			var user = $('.guys .guy:eq(' + random + ')').attr('id');
			
			V.E.setBobbleText(user, V._loadText);
			V.E.showBobble(user);
			
			if(V._tweets[user] == undefined) {
				V.F.fetchFeed(user, function() {
					V.E.setBobbleText(user, V._tweets[user][0]);
				});
			} else {
				V.E.setBobbleText(user, V._tweets[user][0]);
			}
		},
		setBobbleText: function(user, text) {
			if(text == undefined) {
				text = 'No recent tweets';
			}
			
			text = V.F.format.replaceUrl(text);
			text = V.F.format.replaceHash(text);
			text = V.F.format.replaceAt(text);
			
			$('.bobble.' + user).empty().html(text);
			
			V.B.startTimer(V.timer);
			
		},
		showBobble: function(user) {
			V.E.hideBobble();
			$('.bobble.' + user).show('bounce', {
				distance: 50,
				times: 4,
				speed: 75
			});
		},
		hideBobble: function(user) {
			if(user == undefined) {
				$('.bobble').hide();
			} else {
				$('.bobble.' + user).hide();
			}
		}
	}
}

jQuery(document).ready(function($) {
	V.init();
});
