function YouTubePlayer(videoID,videoName,videoWidth,videoHeight) {
	this.videoID = videoID;
	this.playerID = videoID;
	this.autoPlay = true;
	if (videoName.length == 0)	{
		this.videoName = "untitled_video";
	}
	else	{
		this.videoName = videoName;
	}
	this.videoWidth = videoWidth;
	this.videoHeight = videoHeight;
	this.playDetected = false;
	this.handlers = {};
	this.embed();
	YouTubePlayer.register(this);
	var callbackVideoID = this.videoID;
	var callbackVideoName = this.videoName;
	this.on("playing", function()	{
		if (!this.playDetected)	{
			this.playDetected = true;
			TrackVideoViewInGA(callbackVideoID,callbackVideoName);
			TrackVideoViewInWT(callbackVideoID,callbackVideoName);
		}
	});
}

function TrackVideoViewInGA(videoID,videoName)	{
	var trackingID = "/youtube_videos/" + videoID + "/" + videoName;
	pageTracker._trackPageview(trackingID);
}

function TrackVideoViewInWT(videoID,videoName)	{
	var videoFauxPath = "/youtube_videos/" + videoID + "/" + videoName;
	var videoPage = window.location.pathname;
	var videoGroup = "YouTube Videos";
	var videoTitle = "YouTube Video: " + videoName + " (id=" + videoID +")";
	dcsMultiTrack("DCS.dcsuri",String(videoFauxPath),"DCS.dcsref",String(videoPage),"WT.ti",String(videoTitle),"WT.cg_n",String(videoGroup),"WT.cg_s",String(videoTitle));
}

YouTubePlayer.prototype.embed = function() {
	var videoWidth = this.videoWidth;
	var videoHeight = this.videoHeight;
	var minFlashVer = "8";
	var swfParams = {allowScriptAccess:"always",allowFullScreen:"true"};
	var swfExpressInstallURL = null;
	var swfFlashVars = null;
	var videoID = this.videoID;
	var videoDiv = this.playerID;
	var videoDivString = "#" + videoDiv;
	if (this.autoPlay)	{
		var videoURL = "http://www.youtube.com/v/" + videoID + "?enablejsapi=1&fs=1&rel=0&playerapiid=" + videoDiv + "&autoplay=1";
	}
	else	{
		var videoURL = "http://www.youtube.com/v/" + videoID + "?enablejsapi=1&fs=1&rel=0&playerapiid=" + videoDiv;
	}
	var swfAttr = {id:videoDiv};
	try	{
	swfobject.embedSWF(videoURL,videoDiv,videoWidth,videoHeight,minFlashVer,swfExpressInstallURL,swfFlashVars,swfParams,swfAttr);
	}
	catch(err)	{
		$(videoDivString).html("Unable to load video.");
	}
};

YouTubePlayer.prototype.on = function(eventName,handler) {
	this.handlers[eventName] = this.handlers[eventName] || [];
	this.handlers[eventName].push(handler);
};

YouTubePlayer.prototype.onReady = function() {
	var thisPlayer = document.getElementById(this.playerID);
	var callbackString = 'YouTubePlayer.dispatchEvent("{id}")';
	var callbackFunction = callbackString.replace("{id}", this.playerID);
	thisPlayer.addEventListener('onStateChange', callbackFunction);
};

YouTubePlayer.prototype.notifyEvent = function(eventID) {
	var states = YouTubePlayer.STATES;
	for (var eventName in states) {
		if (states[eventName] == eventID) {
			this.fireEvent(eventName);
		}
	}
};

YouTubePlayer.prototype.fireEvent = function(eventName) {
	var handlers = this.handlers[eventName];
	if(!handlers) {
		return;
	}
	for (var i=0; i<handlers.length; i++) {
		handlers[i](eventName);
	}
};

YouTubePlayer.instances = [];

YouTubePlayer.register = function(playerInstance) {
	this.instances.push(playerInstance);
};

YouTubePlayer.findPlayerInstanceByID = function(playerID) {
	var playerInstance = null;
	for (var i=0; i<this.instances.length; i++) {
		if (this.instances[i].playerID == playerID) {
			playerInstance = this.instances[i];
		}
	}
	return playerInstance;
};

YouTubePlayer.dispatchEvent = function(playerID) {
	var playerInstance = YouTubePlayer.findPlayerInstanceByID(playerID);
	return function(eventID) {
		playerInstance.notifyEvent(eventID);
	};
};

YouTubePlayer.STATES = {unstarted:-1, ended:0, playing:1, paused:2, buffering:3, cued:5};

window.YouTubePlayer = YouTubePlayer;

window.onYouTubePlayerReady = function(playerID) {
	var unescapedPlayerID = unescape(playerID);
	YouTubePlayer.findPlayerInstanceByID(unescapedPlayerID).onReady();
};
