var gLastPlayedLink = null;

function onClick_Play()
{
	if (loadingFlashSlideCompletedPlaying())
	{
		gPauseWhenReadyForLoadingSlide = false;
		
		if (player.player_GetState() == player.PLAYER_STOPPED)
			playLastPlayedLink();
		else
			player.player_Play();
	}
}

function playLastPlayedLink()
{
	if (gLastPlayedLink)
	{
		playLink(gLastPlayedLink);
	}
	else
	{
		if (getBroadbandCookie() && gBroadband != "")
			playBroadband();
		else if (gNarrowband != "")
			playNarrowband();
	}
}

function setClipURL(pLink)
{
	gLastPlayedLink = pLink;
	player.player_SetClipURL(gLastPlayedLink);
}

function playLink(pLink)
{
	gLastPlayedLink = pLink;

	display_player();

	player.player_Play(pLink);
}

function loadThumbnail(pURL)
{
	try
	{
		if (pURL != "")
		{
			var objImage	= document.getElementById("imgStoppedThumbnail");
//			var objLoading	= document.getElementById("divLoadingThumbnail");
			
			objImage.src = pURL;

//			objLoading.style.backgroundPosition	= "center center";
//			objLoading.style.backgroundRepeat	= "no-repeat";
//			objLoading.style.backgroundAttachment	= "fixed";
//			objLoading.style.backgroundImage	= "url(" + pURL + ")";
		}
		else
		{
			document.getElementById("imgStoppedThumbnail").style.visibility = 'hidden';
		}
	}
	catch(e)
	{
	}
}

function playBroadband()
{
	setBroadbandCookie(true);

	loadThumbnail(gThumbnailForBroadband);
	playLink(gBroadband);
}

function playNarrowband()
{
	setBroadbandCookie(false);

	loadThumbnail(gThumbnailForNarrowband);
	playLink(gNarrowband);
}

function playAudio()
{
	playLink(gAudio);
}

function playVideo()
{
	playLink(gVideo);
}

function onBodyLoad()
{
	gm_actions_init();
	player_click_init();

	var allowed_to_play = true; // optimistic

	if (gVideoWindowsLimitCookieName != "")
		allowed_to_play = initialize_video_windows_limit(gVideoWindowsLimitCookieName, gVideoWindowsLimitCookieInitVal);
	else
		allowed_to_play = true;

	if (allowed_to_play)
	{
		switch(gEmbedStyle)
		{
			case "PLAYER":
				if (gAutoStart)
				{
					if (gNarrowband == "" || gBroadband == "")
					{
						if (gNarrowband != "")
						{
							loadThumbnail(gThumbnailForNarrowband);
							playLink(gNarrowband);
						}
						else
						{
							loadThumbnail(gThumbnailForBroadband);
							playLink(gBroadband);
						}
					}
					else
					{
						switch(getBroadbandCookie())
						{
							case null:
								// Cookie not set - display selector if there is content to play
								if (gNarrowband != "" || gBroadband != "")
									display_bandwidth_selector();
								break;

							case true:
								loadThumbnail(gThumbnailForBroadband);
								playBroadband();
								break;

							case false:
								loadThumbnail(gThumbnailForNarrowband);
								playNarrowband();
								break;
						}
					}
				}
				else // gAutoStart == false
				{
					if (gNarrowband == "" || gBroadband == "")
					{
						if (gNarrowband == "")
						{
							setClipURL(gBroadband);
							loadThumbnail(gThumbnailForBroadband);
						}
						else
						{
							setClipURL(gNarrowband);
							loadThumbnail(gThumbnailForNarrowband);
						}


						display_stopped();
					}
					else
						display_bandwidth_selector();
				}
				break;

			case "RADIO":
				if (gStreamType == "VIDEO")
					display_watch_video_button();

				if (gAutoStart)
				{
					if (gStreamType == "VIDEO")
						playVideo();
					else
						playAudio();
				}
				break;
		}

//	if (player.player_GetVersion() != player.PLAYER_WM_6)
		setInterval("onTimer_PlayerHousekeep();", 3000);

		CallHook(HOOK_INITIALIZED);
	}
	else /* NOT allowed_to_play */
	{
		location.reload(true);	// The ASP will show the "access denied" message
	}
}

function onBodyUnload()
{
	remove_video_windows_limit_cookie();
	gm_actions_shutdown();
	player_click_shutdown();
	
	clear_Active_Timer();
	clear_Check_Loading_Flash_Slide_Completed_Timer();
}

function onContextMenu()
{
	try
	{
		if (event.altKey && event.ctrlKey)
		{
			if (!gDiagnosticsEnabled)
			{
				gDiagnosticsEnabled = true;

				player.player_enableDiagnostics();

				event.cancelBubble = true;
				event.returnValue = false;
			}
		}
		else
		{
			if (!gDiagnosticsEnabled)
			{
				event.cancelBubble = true;
				event.returnValue = false;
			}
			else
			{
				event.returnValue = true;
			}
		}
	}
	catch(e)
	{
		alert(e.description);
	}

	return gDiagnosticsEnabled;
}
