$(function()
{	
	var gBackground = ""; // global used for storing background image URL for swaps.
	// Setup player 
	$f("a.player", {src: "/common/flowplayer/flowplayer.commercial-3.1.1.swf", wmode: "transparent"},
	{
		clip:
		{
			scaling: "fit",
			onCuepoint: [[0, -500], function(clip, cuepoint)
			{
				if (cuepoint == 0)
				{
					$(this.getParent()).parent().find("div.webucation_end_html").hide();
				}
				else
				{
					if (this.isFullscreen())
					{ 
						this.toggleFullscreen();
					}
					$(this.getParent()).parent().find("div.webucation_end_html").show();
				}
			}]
		},
		onBeforeClick: function()
		{
			if (!DetectFlashVer(9, 0, 0))
			{
				alert("Sorry, this feature requires that you have Flash version 9 or greater.");
				return;
			}		
			$f().unload();
			var wrap = $(this.getParent());
			// Hide nested play button
			gBackground = wrap.css("background-image");
			wrap.css("background-image", "none");
			wrap.find("img").fadeOut();
			wrap.animate({width:630, height:420}, 500, function()
			{
				$f(this).load();
				wrap.parent().find("div.expanded_spacer").css("display", "block");
			});
			// Disable default click behaviour (player loading)
			return false; 
		}, 

		// Unload action resumes to original state	
		onUnload: function()
		{
			//revert beforeclick css
			$(this.getParent()).parent().find("div.webucation_end_html").hide();
			//animate
			var wrap = $(this.getParent());
			wrap.animate({width:134, height:100}, 500, function()
			{

				// Make play button visible again
				$(this).find("img").fadeIn();
				wrap.parent().find("div.expanded_spacer").css("display", "none");
				//wrap.parent().find("div.watch_url").css("display", "none");
			});
			wrap.css("background-image", gBackground);
		}, 

		// When playback finishes perform our custom unload action
		onFinish: function()
		{
			//this.unload();	
		},

		key: '#$f4b215d01eedeefe7ca'
	});

	$("a.player").click(function()
	{
		// Mark video as viewed on Google Analytics.
		var videoName = $(this).parent().find("div.header").html();
		pageTracker._trackPageview("/webucation_view/" + videoName);
		// Fix background color post expansion.
		$(this).parent().css("background-color", "#EFEFEF");
	});

	// Highlighting jquery
	$("a.player").bind("mouseenter", function()
	{
		if ($(this).width() <= 134)
		{
			$(this).parent().css("background-color", "#FFD");
		}
	}).bind("mouseleave", function()
	{
		$(this).parent().css("background-color", "#EFEFEF");
	});

	// webucations contents jquery
	$("a.expand").click(function(e)
	{
		e.preventDefault();
		$(this).parent().find("ul:first").toggle();
	});

	$("input.watchtext").click(function()
	{
		$(this).select();
	});
	
	$("a.webucation_play_again").click(function(e)
	{
		//$(this).parent()
		e.preventDefault();
		$(this).parent().parent().parent().hide();
		$f().play();
	});
});
