		$(document).ready(function(){
			/* Deal with voting for games */
			var votedgame = $.cookie('gamesvote');
			if (votedgame) //The user has voted for game already
			{
				$("#blockTrailer-voted").show();
				$("#blockTrailer-voted li:contains('" + votedgame + "')").css('font-weight', 'bold');
			}
			else
			{
				$("#blockTrailer-unvoted").show();
				$("#button-submitVote").click(function(){
					var votedgame = $("#poll input:checked").attr('value');
					$.post("/vote.php", {'gamesvote':true, 'votedgame':votedgame}, function(data){
						if (data.result != 'ok')
							return;
						$.cookie("gamesvote", votedgame, {path:'/', domain:'spicyroulette.com', expires:5});
						$("#blockTrailer-unvoted").hide('slow');
						$("#blockTrailer-voted").show('slow');
						$("#blockTrailer-voted li:contains('" + votedgame + "')").css('font-weight', 'bold');
					}, "json");
				});
			}
			/* Insert flash videos into page */
			$(".flashvideo").each(function(){
				var width = $(this).attr('width');
				var height = $(this).attr('height');
				var file = $(this).attr('file');
				var streamer = $(this).attr('streamer');
				var image = $(this).attr('image');
				$(this).flash({'src':'player.swf', 'width':width, 'height':height,
					'flashvars': {'controlbar':'over', 'file':file, 'image':image, 'streamer':streamer}});
			});
			$(".episode-rating").each(function(){
				var set_id = $(this).attr('set_id');
				var rating = $.cookie('voted_'+set_id);
				if (rating)
				{
					$("li", this).css('cursor', 'default').addClass('voted');
					$("li", this).eq(rating-1).css('background-position', '0 -64px');
				}
				else
					$(this).addClass('unvoted');
			});
			$(".unvoted li").click(function(){
				var rating = $(this).prevAll("li").length + 1;
				var set_id = $(this).parent().attr('set_id');
				$(this).parent().next().next().html('voting...');
				$(this).css('background-position', '0 -64px');
				$(this).parent().children("li").css('cursor', 'default').addClass('voted').unbind();
				$.post("/vote.php", {'rating':rating, 'set_id':set_id}, function(data){
					var rating = Math.round(data.amount/data.votes*100)/100;
					var votes = data.votes;
					var set_id = data.set_id;
					var yourVote = data.yourVote;
					$('.episode-rating[set_id="' + data.set_id + '"]').next().next().html('Av. rating ' 
						+ rating + ' (' + votes + ' votes)');
					$.cookie('voted_'+set_id, yourVote, {path:'/', domain:'spicyroulette.com', expires:5});
				}, "json");
			});
		});

