/*
 *	This code has been sourced from 
 *	http://www.kingcontent.com/2009/11/tracking-youtube-embedded-player-plays.html
 *	and is used for tracking youtube videos through google analytics.
 *	
 *	A.	Please be sure to embed the video using the following code block instead 
 *		of the one offered by youtube (which is embedded in an iframe as opposed to
 *		an object tag).
 *	
 *	B.	please be sure to note that the link (the src attribute) to the video given by youtube is formatted:
 *		http://www.youtube.com/embed/Rf11V0dY1Tg"
 *		And to embed it in an object > embed tag it will need a "v" where the link has "embed" in it.
 *		http://www.youtube.com/v/Rf11V0dY1Tg"
 *	
 *	C.	Also note that the link to the video is in two places:
 *		1. in a param tag
 *		2. in the embed tag as the src attribute
 *	
 *	D.	Finally, in order for this tracking code to work, the URL in 
 *		the src attribute as well as the param have a GET variable attached:
 *		&enablejsapi=1
 *		so that the link looks like this:
 *		http://www.youtube.com/v/Rf11V0dY1Tg&enablejsapi=1
 *	
 ~~~ CODE Start ~~~
	<object type="application/x-shockwave-flash" style="width:309px;height:251px"
			data="http://www.youtube.com/v/Rf11V0dY1Tg&enablejsapi=1"
			value="http://www.youtube.com/v/Rf11V0dY1Tg&enablejsapi=1" id='ytvideo'>
		<param name="movie" value="http://www.youtube.com/v/Rf11V0dY1Tg&enablejsapi=1"></param>
		<param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param>
	</object>
 ~~~ CODE End ~~~
 *	
 *	Instructions from original author regarding
 *	the events triggered using the code below:
 *
 *	1.	'play' is an event everytime the player starts playing. 
 *		A play event is also when someone pauses and starts playing 
 *		or scrolls to a different part of the video. 
 *		This is why counting number of 'plays' can be misleading.
 *	
 *	2.	'unique_play_per_page' counts 'play' events, but only one per page load.
 *	
 *	3.	'ended' counts videos that have been played to the end.
 *	
 */

var playedOnce = false;

function onYouTubePlayerReady(playerid) {
	player = document.getElementById('ytvideo');
	player.addEventListener('onStateChange', 'youtubeEvent');
}

function youtubeEvent(state) {
	console.log("State No.: "+state);
	console.log(_gaq);
	if (state == 1) {
		console.log("a");
		if (!playedOnce) {
			playedOnce = true;
			console.log("b");
			//pageTracker._trackEvent('video', 'unique_play_per_page');
			_gaq.push(['_trackPageview', '/video/youtube/Rf11V0dY1Tg/unique_play_per_page']);
		}
		console.log("c");
		//pageTracker._trackEvent('video', 'play');
		_gaq.push(['_trackEvent', 'Video', 'Play', '/video/youtube/Rf11V0dY1Tg/play']);
		return;
	}

	if (state == 0) {
		console.log("d");
		//pageTracker._trackEvent('video', 'ended');
		_gaq.push(['_trackEvent', 'Video', 'Ended', '/video/youtube/Rf11V0dY1Tg/ended']);
		return;
	}
}
