var params = { allowScriptAccess: "always", allowFullScreen: "true", wmode: "transparent" };
var atts = { id: "youtubePlayer" };
swfobject.embedSWF("http://www.youtube.com/v/JTnJ5tgB7HU&hl=en_US&fs=1&rel=0&hd=1&autoplay=1&enablejsapi=1", 
                   "flash-player", "640", "385", "8", null, null, params, atts);

function onYouTubePlayerReady(playerId) {
	var player = $('#youtubePlayer')[0];
	global_player = player; // IE can't handle the original player var being global ... go figure
	
	// handle event for all browsers and then IE!
	if (player.addEventListener) {
		player.addEventListener('onStateChange', 'youtubePlayerChange');
	} else {
		player.attachEvent('onStateChange', 'youtubePlayerChange');
	}
}

function youtubePlayerChange(state) {
	if (state === 0) {
		if ($('#player-thumbs a').first().is('.active')) {
			$('#player-thumbs a:eq(1)').click();
		}
	}
}

$(document).ready(function() {			
	var playerThumbs = $('#player-thumbs a');
	
	playerThumbs.click(function(event) {
		event.preventDefault();
		
		playerThumbs.removeClass('active');
		$(this).addClass('active');
		
		if (global_player) { // check the player has initialised
			global_player.loadVideoByUrl($(this).attr('href'));
		}
	});
});
