// Array containing the players
var ytplayers = new Array();

// event handler, gets called when the player is fully loaded
function onYouTubePlayerReady(playerId) {
	var player = document.getElementById("myytplayer" + playerId);
	ytplayers[playerId].player = player;
	if( player.isMuted() )
		player.unMute();
	if( ytplayers[playerId].startwhenloaded )
		ytplayers[playerId].loadNewVideo(ytplayers[playerId].id ,0);
}

// object containing all of the api calls to the youtube player
function YouTubePlayerObject(id, startwhenloaded) {
	this.id = id;
	this.player = 0;
	this.startwhenloaded = startwhenloaded;

	// functions for the api calls
	this.loadNewVideo = function(id, startSeconds) {
		if (this.player) {
			this.player.loadVideoById(id, parseInt(startSeconds));
		}
	}

	this.cueNewVideo = function(id, startSeconds) {
		if (this.player) {
			this.player.cueVideoById(id, startSeconds);
		}
	}
	
	this.tryPlay = function() {
		if(this.getPlayerState() <= 0)
			this.loadNewVideo(this.id, 0);
		else
			this.play();
	}

	this.play = function() {
		if (this.player) {
			this.player.playVideo();
		}
	}

	this.pause = function() {
		if (this.player) {
			this.player.pauseVideo();
		}
	}

	this.stop = function() {
		if (this.player) {
			this.player.stopVideo();
		}
	}

	this.getPlayerState = function() {
		if (this.player) {
			return this.player.getPlayerState();
		}
	}

	this.seekTo = function(seconds) {
		if (this.player) {
			this.player.seekTo(seconds, true);
		}
	}

	this.getBytesLoaded = function() {
		if (this.player) {
			return this.player.getVideoBytesLoaded();
		}
	}

	this.getBytesTotal = function() {
		if (this.player) {
			return this.player.getVideoBytesTotal();
		}
	}

	this.getCurrentTime = function() {
		if (this.player) {
			return this.player.getCurrentTime();
		}
	}

	this.getDuration = function() {
		if (this.player) {
			return this.player.getDuration();
		}
	}

	this.getStartBytes = function() {
		if (this.player) {
			return this.player.getVideoStartBytes();
		}
	}

	this.mute = function() {
		if (this.player && !this.player.isMuted()) {
			this.player.mute();
		}
	}

	this.unMute = function() {
		if (this.player && this.player.isMuted()) {
			this.player.unMute();
		}
	}
	
	this.changeMute = function() {
		if( this.player ) {
			if (this.player.isMuted()) {
				this.player.unMute();
			}
			else {
				this.player.mute();
			}
		}
	}

	this.getEmbedCode = function() {
		alert(this.player.getVideoEmbedCode());
	}

	this.getVideoUrl = function() {
		alert(this.player.getVideoUrl());
	}

	this.setVolume = function(newVolume) {
		if (this.player) {
			this.player.setVolume(newVolume);
		}
	}

	this.getVolume = function() {
		if (this.player) {
			return this.player.getVolume();
		}
	}

	this.clearVideo = function() {
		if (this.player) {
			this.player.clearVideo();
		}
	}
}
