$(function() {
	$('div.code, pre.terminal').hover(function() {
		$(this).animate({
			width: 960,
		}, 200);
	}, function() {
		$(this).animate({
			width: 640,
		}, 200);
	});

	$('#bug').rssBug();

	var $videos = $('a.flowplayer');
	$videos.each(function() {
		var $video = $(this);
		var player = flowplayer(this, '/js/vendors/flowplayer/flowplayer-3.1.3.swf', {
			playlist: [
				{
					url: $video.attr('rel')
				},
				{
					url: '/img/transloadit-splash.jpg',
					scaling: 'orig',
				}
			],
			canvas:  { 
				backgroundColor: '#ffffff',
				backgroundGradient: 'none',
			}
		}); 

		// if ($videos.length == 1) {
		// 	player.play();
		// }
	});
});

$.fn.rssBug = function(action, options) {
	if (!action && !this.data('init')) {
		action = 'init';
	}

	if (!action) {
		return;
	}

	var $this = $(this), $message = $this.next('.rss').children('.bug-message');
	var actions = {
		bounce: function() {
			$this.effect('bounce', {times: 2}, 400);
		},
		say: function() {
			$message
				.show()
				.text(options);

			setTimeout(function() {
				$message.hide();
			}, 3000);
		},
		init: function() {
			$this.data('init', true);
			$this.hover(function() {
				if ($this.is(':animated') || ($this.data('nextBounce') > +(new Date())) || $message.is(':visible')) {
					return;
				}

				var bounceCount = $this.data('bounceCount') || 1;
				$this
					.data('nextBounce', (+(new Date()) + 1000))
					.data('bounceCount', bounceCount + 1)

				if (bounceCount == 3) {
					return $this.rssBug('say', 'Stop bugging me. Sign up for the RSS feed already!');
				} else if (bounceCount == 5) {
					return $this.rssBug('say', 'Seriously, next time you do this I will bite you!');
				} else if (bounceCount == 6) {
					$this.rssBug('say', 'Ok, you did this to yourself. Have fun!');
					setTimeout(function() {
						window.location.href = 'http://www.youtube.com/watch?v=oHg5SJYRHA0';
					}, 1300);
					return;
				}
				$this.rssBug('bounce');
			});
		}
	};

	actions[action]();
}