function init(){
	
	Player.playlist.list = Playlist;
	Player.playlist.Element = document.getElementById("playlist");

	Player.controls.Element = document.getElementById("controls");

	var params = { allowScriptAccess: "always" };
	var atts = { id: "player" };

	swfobject.embedSWF("http://www.youtube.com/apiplayer?" +
						"&enablejsapi=1&playeriid=player1",
						"player", "480", "295", "8", null, null, params, atts);
}

function onYouTubePlayerReady(playerId){
	var ytPlayer = document.getElementById("player");

	Player.player = ytPlayer;
	ytPlayer.addEventListener("onStateChange", "Player.onStateChange");
	ytPlayer.addEventListener("onError", "Player.onError");

	ytPlayer.cueVideoById(Player.playlist.list[0]);

	Player.init();
}

var Player = new function(){
	this.params = ({
		autoplay: false,
		autonext: true
	});
	
	this.playlist = ({
		list: new Array(),
		Element: null
	});
	
	this.controls = ({
		muted: false,
		volume: 70,
		playing: false,
		current: 0,
		Element: null
	});
	
	this.player = null;
	
	this.init = function(){
		if(this.params.autoplay)
			this.play();
		
		this.updatePlaylist(this.controls.current, this.controls.current);
		this.player.setVolume(this.controls.volume);
	};
	
	this.play = function(){
		this.controls.playing = true;
		this.updateControls();
		
		this.player.playVideo();
	};
	
	this.pause = function(){
		this.controls.playing = false;
		this.updateControls();
		
		this.player.pauseVideo();
	};
	
	this.onStateChange = function(state){
		if(state === 0)
			if(this.params.autonext)
				if(this.controls.current !== this.playlist.list.length)
					this.next();
	};
	
	this.updatePlaylist = function(previous, current){
		document.getElementById("item"+previous).setAttribute("class", "");
		document.getElementById("item"+current).setAttribute("class", "active");
	};
	
	this.onError = function(error){
		this.next();
	};
	
	this.updateControls = function(){
		if(this.controls.playing){
			document.getElementById("play").innerHTML = "pause";
			document.getElementById("play").setAttribute("href", "javascript:Player.pause();");
			document.getElementById("play").setAttribute("class", "playing");
		}
		else{
			document.getElementById("play").innerHTML = "play";
			document.getElementById("play").setAttribute("href", "javascript:Player.play();");
			document.getElementById("play").setAttribute("class", "paused");
		}
		
		if(this.controls.muted){
			document.getElementById("mute").innerHTML = "muted";
			document.getElementById("mute").setAttribute("class", "muted");
		}
		else{
			document.getElementById("mute").innerHTML = "mute";
			document.getElementById("mute").setAttribute("class", "unmuted");
		}
	};
	
	this.previous = function(){
		this.updatePlaylist(this.controls.current, (this.controls.current = this.controls.current - 1));
		
		this.player.loadVideoById(this.playlist.list[this.controls.current]);
		this.play();
	};
	
	this.seek = function(to){
		this.updatePlaylist(this.controls.current, (this.controls.current = to));
		
		this.player.loadVideoById(this.playlist.list[this.controls.current]);
		this.play();
	};
	
	this.next = function(){
		this.updatePlaylist(this.controls.current, (this.controls.current = this.controls.current + 1));
		
		this.player.loadVideoById(this.playlist.list[this.controls.current]);
		this.play();
	};
	
	this.volumeUp = function(){
		this.controls.volume = this.controls.volume + 10;
		
		if(this.controls.volume > 100)
			this.controls.volume = 100;
		
		this.player.setVolume(this.controls.volume);
		this.updateControls();
	};
	
	this.volumeDown = function(){
		this.controls.volume = this.controls.volume - 10;
		
		if(this.controls.volume < 0)
			this.controls.volume = 0;
		
		this.player.setVolume(this.controls.volume);
		this.updateControls();
	};
	
	this.toggleMute = function(){
		if(this.controls.muted){
			this.player.unMute();
			this.controls.muted = false;
			this.updateControls();
		}
		else{
			this.player.mute();
			this.controls.muted = true;
			this.updateControls();
		}
	};	
};

