				// VideoPlayer
				// Requires swfobject.js and jquery.js
			
				var VideoPlayer = {
					width: "400",
					height: "250",
				
					params: {
						allowScriptAccess: "always",
						wmode: "opaque"
					},
					
					atts: {
						id: "ytplayer"
					},
					
					ytplayer: null,
					
					playlist: [],
					
					autostart: false,
					
					repeat: false,
					
					currentVideo: 0,
					
					errorCount: 0,
					
					updateDisplayInterval: null,
					
					// autostart: boolean.
					init: function(autostart) {
						VideoPlayer.autostart = autostart;
						
						swfobject.embedSWF("http://www.youtube.com/apiplayer?enablejsapi=1&playerapiid=ytplayer", "videoplayer", VideoPlayer.width, VideoPlayer.height, "8", null, null, VideoPlayer.params, VideoPlayer.atts);
					},
					
					ready: function() {
						VideoPlayer.ytplayer = document.getElementById(VideoPlayer.atts.id);
						VideoPlayer.ytplayer.addEventListener("onStateChange", "VideoPlayer.stateChange");
						VideoPlayer.ytplayer.addEventListener("onError", "VideoPlayer.error");
						
						var firstid = VideoPlayer.playlist[VideoPlayer.currentVideo].id;
						if (VideoPlayer.autostart)
							VideoPlayer.load(firstid);
						else
							VideoPlayer.cue(firstid);
							
						VideoPlayer.updateDisplayInterval = setInterval(VideoPlayer.updateDisplay, 500);
					},
					
					stateChange: function(newState) {
						switch (newState) {
							case -1: // Unstarted
								break;
						
							case 0: // Ended
								VideoPlayer.end();
								break;
							
							case 1: // Playing
								VideoPlayer.errorCount = 0;
								$("#videoplayer_play").attr("src", "/img/videoplayer/pause.png");
								break;
							
							case 2: // Paused
								$("#videoplayer_play").attr("src", "/img/videoplayer/play.png");
								break;
							
							case 3: // Buffering
								$("#videoplayer_play").attr("src", "/img/videoplayer/play.png");
								break;
							
							case 5: // Cued
								break;
						}
					},
					
					error: function(errorCode) {
						VideoPlayer.errorCount++;
						
						if (VideoPlayer.errorCount >= VideoPlayer.playlist.length) {
							// Stop trying to load videos if there are errors for each video in the playlist.
							VideoPlayer.stop();
							return;
						}
					
						switch (errorCode) {
							case 2: // Invalid param
							case 100: // Not Found
							case 101: // Embedding not allowed
							case 150: // Same as 101
								VideoPlayer.end();
								break;
						}
					},
					
					addToPlaylist: function(info) {
						VideoPlayer.playlist[VideoPlayer.playlist.length] = info;
					},
					
					getCurrentVideoInfo: function() {
						return VideoPlayer.playlist[VideoPlayer.currentVideo];
					},
					
					end: function() {
						VideoPlayer.currentVideo = VideoPlayer.currentVideo < VideoPlayer.playlist.length - 1 ? VideoPlayer.currentVideo + 1 : 0;
						VideoPlayer.load(VideoPlayer.playlist[VideoPlayer.currentVideo].id);
					},
					
					load: function(id) {
						VideoPlayer.ytplayer.loadVideoById(id, 0, "large");
					},
					
					cue: function(id) {
						VideoPlayer.ytplayer.cueVideoById(id, 0, "large");
					},
					
					play: function() {
						if (VideoPlayer.ytplayer.getPlayerState() == -1)
							VideoPlayer.load(VideoPlayer.playlist[VideoPlayer.currentVideo].id);
						else
							VideoPlayer.ytplayer.playVideo();
					},
					
					pause: function() {
						VideoPlayer.ytplayer.pauseVideo();
					},
					
					previous: function() {
						VideoPlayer.currentVideo = VideoPlayer.currentVideo == 0 ? VideoPlayer.playlist.length - 1 : VideoPlayer.currentVideo - 1;
						VideoPlayer.load(VideoPlayer.playlist[VideoPlayer.currentVideo].id);
					},
					
					next: function() {
						VideoPlayer.end();
					},
					
					togglePlayPause: function() {
						var state = VideoPlayer.ytplayer.getPlayerState();
					
						if (state === 1) // Playing
							VideoPlayer.pause();
						else // Paused
							VideoPlayer.play();
					},
					
					stop: function() {
						VideoPlayer.ytplayer.stopVideo();
						VideoPlayer.ytplayer.clearVideo();
					},
					
					// mute: boolean whether or not to mute.
					mute: function(mute) {
						if (mute)
							VideoPlayer.ytplayer.mute();
						else
							VideoPlayer.ytplayer.unMute();
					},
					
					updateDisplay: function() {
						var duration = Math.round(VideoPlayer.ytplayer.getDuration());
						var current = Math.round(VideoPlayer.ytplayer.getCurrentTime());
						
						var duration_mins = Math.floor(duration / 60);
						var duration_secs = duration - duration_mins * 60;
						
						if (duration_secs < 10)
							duration_secs = "0" + duration_secs;
						
						var current_mins = Math.floor(current / 60);
						var current_secs = current - current_mins * 60;
						
						if (current_secs < 10)
							current_secs = "0" + current_secs;
						
						var info = VideoPlayer.getCurrentVideoInfo();
						
						var titlehtml = "";
						if (typeof info.link != "undefined")
							titlehtml += "<a href=\"" + info.link + "\" target=\"_blank\">";
						titlehtml += info.title;
						if (typeof info.link != "undefined")
							titlehtml += "</a>";
						
						$("#videoplayer_title").html(titlehtml);
						$("#videoplayer_time").html(current_mins + ":" + current_secs + " / " + duration_mins + ":" + duration_secs);
					}
				}
				
				function onYouTubePlayerReady(p) {
					VideoPlayer.ready();
				}
