var gActiveTimer			= null;
var gCheckFlashSlideCompletedTimer	= null;

/*** MARK ***/
var gRetryCount				= 0;
var gLastErrorMessage			= "";
var gError				= false;

var gPauseWhenReadyForLoadingSlide	= false;
var gFlashSlide_Loading_StartTime	= null;

function onTimer_PlayerHousekeep()
{
	if (gError)
	{
		gError = false;

		++gRetryCount;

		display_buffering();

		var divProgress = document.all.item("divLoadingProgress");

//		if (player.player_GetVersion() != player.PLAYER_WM_6)
		if (true)
		{
			divProgress.innerText = gLastErrorMessage + " " + gRetryCount;
			playLastPlayedLink();
		}
		else
		{
			divProgress.innerText = gLastErrorMessage;
		}

		gError = false;
	}
}

function stop_flash_slides()
{
	stop_flash_slide_loading();
	stop_flash_slide_stopped();
	
	gFlashSlide_Loading_StartTime = null;
}

function stop_flash_slide_loading()
{
	try
	{
		slide_loading.Stop();
		gFlashSlide_Loading_StartTime = null;
	}
	catch(e)
	{
	}
}

function stop_flash_slide_stopped()
{
	try
	{
		slide_stopped.Stop();
	}
	catch(e)
	{
	}
}

function loadingFlashSlideCompletedPlaying()
{
	if (!is_flash_slide_loading_playing())
		return true;
		
	if (gFlashSlide_Loading_StartTime == null)
		return true;
	
	if (gFlashSlide_Loading_MinSeconds <= 0)
		return true;
		
	var now = new Date();
	
	var delta = (now.valueOf() - gFlashSlide_Loading_StartTime.valueOf());
	var deltaSeconds = Math.floor(delta / 1000);

	if (deltaSeconds >= gFlashSlide_Loading_MinSeconds)
		return true;
	else
		return false;
}

function is_flash_slide_loading_playing()
{
	try
	{
		if (slide_loading.IsPlaying())
			return true;
		else
			return false;
	}
	catch(e)
	{
		return false;
	}
}

function play_flash_slide_loading()
{
	try
	{
		if (!slide_loading.IsPlaying())
		{
			gFlashSlide_Loading_StartTime = new Date();

			slide_loading.Rewind();
			slide_loading.Play();
		}
	}
	catch(e)
	{
	}
}

function play_flash_slide_stopped()
{
	try
	{
		if (!slide_stopped.IsPlaying())
		{
			slide_stopped.Rewind();
			slide_stopped.Play();
		}
	}
	catch(e)
	{
	}
}

function display_bandwidth_selector()
{
	stop_flash_slides();
	
	player.player_Stop();

	document.all.item("divSelector").style.display = "block";
	document.all.item("divWatchVideo").style.display = "none";
	document.all.item("divPlayer").style.display = "none";
}

function display_watch_video_button()
{
	stop_flash_slides();
	
//	player.player_Stop();

	document.all.item("divWatchVideo").style.display = "block";
	document.all.item("divPlayer").style.display = "none";
}

function display_player()
{
//	stop_flash_slides();
	
	document.all.item("divWatchVideo").style.display = "none";
	document.all.item("divSelector").style.display = "none";
	document.all.item("divPlayer").style.display = "block";
}

function display_buffering()
{
	stop_flash_slide_stopped();
	play_flash_slide_loading();
	
	if (!gPauseWhenReady)
	{
		gShowVisualElements = true;
	}
	
	if (gShowVisualElements)
	{
		try
		{
			document.all.item("divGlobalOverlay").style.display = "none";
		}
		catch(e)
		{
		}

		document.all.item("divVideo").style.display = "none";
		document.all.item("divLoading").style.display = "block";
		document.all.item("divStopped").style.display = "none";
	}
}

function display_stopped()
{
	display_stopped_ex(true);
}

function display_stopped_ex(pStopSlides)
{
	if (pStopSlides)
	{
		stop_flash_slides();
		play_flash_slide_stopped();
	}

	if (gShowVisualElements)
	{
		try
		{
			document.all.item("divGlobalOverlay").style.display = "block";
		}
		catch(e)
		{
		}
		
		document.all.item("divVideo").style.display = "none";
		document.all.item("divLoading").style.display = "none";
		document.all.item("divStopped").style.display = "block";
	}

	if (gEmbedStyle == "RADIO" && gStreamType == "VIDEO")
		display_watch_video_button();
}

function display_video()
{
	stop_flash_slides();
	
	gShowVisualElements = true;
	
	try
	{
		document.all.item("divGlobalOverlay").style.display = "none";
	}
	catch(e)
	{
	}

	document.all.item("divVideo").style.display = "block";
	document.all.item("divLoading").style.display = "none";
	document.all.item("divStopped").style.display = "none";
}

function onTimer_checkFlashLoadinSlideCompletedPlaying()
{
	if (loadingFlashSlideCompletedPlaying())
	{
		clear_Check_Loading_Flash_Slide_Completed_Timer();
		
		if (gPauseWhenReadyForLoadingSlide == false)
		{
			if (player.player_GetState() != player.PLAYER_STOPPED)
			{
				display_video();
				
				setTimeout("player.player_Play(); clear_Check_Loading_Flash_Slide_Completed_Timer();", 1);
				
				return;
			}
		}
		
		toolbar_UpdatePlayerButtonsState(player.PLAYER_PAUSED);
	}
}

function onTimer_Playing()
{
	document.recalc();

	try
	{
		if (player.player_GetClipCurrentPosition() > 0)
			display_video();

		toolbar_UpdateClock();
	}
	catch(e)
	{
		alert(e.description);
	}
}

function onTimer_Buffering()
{
	toolbar_UpdateClock();

	var divProgress = document.all.item("divLoadingProgress");

	var progress = player.player_GetBufferingProgress();

	divProgress.innerHTML = "<SPAN DIR=\"" + gLanguageDirection + "\">" + getPlayerExtendedStateMessage(progress) + "</SPAN>";
//	if (progress < 100)
//		divProgress.innerText = "" + progress + " %";
//	else
//		divProgress.innerText = getPlayerExtendedStateMessage();
//		divProgress.innerText = "Please wait";

//	display_buffering();
}

function getPlayerExtendedStateMessage(pBufferingProgress)
{
	switch(player.player_GetState())
	{
		case player.PLAYER_PLAYLIST_OPENING:
			return getMessageText(gLanguage, MSG_PLAYER_PLAYLIST_OPENING);
		case player.PLAYER_PLAYLIST_OPENED:
			return getMessageText(gLanguage, MSG_PLAYER_PLAYLIST_OPENED);
		case player.PLAYER_MEDIA_LOCATING:
			return getMessageText(gLanguage, MSG_PLAYER_MEDIA_LOCATING);
		case player.PLAYER_MEDIA_CONNECTING:
			return getMessageText(gLanguage, MSG_PLAYER_MEDIA_CONNECTING);
		case player.PLAYER_MEDIA_WAITING:
			return getMessageText(gLanguage, MSG_PLAYER_MEDIA_WAITING);
		case player.PLAYER_MEDIA_LOADING:
			return getMessageText(gLanguage, MSG_PLAYER_MEDIA_LOADING);
		case player.PLAYER_MEDIA_OPENED:
			return getMessageText(gLanguage, MSG_PLAYER_MEDIA_OPENED);
		case player.PLAYER_CODEC_DOWNLOAD_START:
			return getMessageText(gLanguage, MSG_PLAYER_CODEC_DOWNLOAD_START);
		case player.PLAYER_CODEC_DOWNLOAD_END:
			return getMessageText(gLanguage, MSG_PLAYER_CODEC_DOWNLOAD_END);
		case player.PLAYER_LICENSE_DOWNLOAD_START:
			return getMessageText(gLanguage, MSG_PLAYER_LICENSE_DOWNLOAD_START);
		case player.PLAYER_LICENSE_DOWNLOAD_END:
			return getMessageText(gLanguage, MSG_PLAYER_LICENSE_DOWNLOAD_END);

		default:
			if (pBufferingProgress < 100)
				return "" + pBufferingProgress + " %";
			else
				return getMessageText(gLanguage, MSG_PLEASE_WAIT);
	}

	return "(Please wait)";		// The brackets are intentional - the code should NOT reach till this point
}

function onPlayerError(pURL, pMessage)
{
	if (gPlayerRetry /* && (player.player_GetVersion() != player.PLAYER_WM_6) */)
	{
		gError = true;
		gLastErrorMessage = pMessage;
	}
	else
	{
		alert("Player error: " + pMessage + "\nURL: " + pURL);
	}
}

function onPlayerStateChange(pNewState, pOldState)
{
	var isFirstItem = player.player_IsCurrentItemFirstInPlaylist();
	var playlistEntryType = player.player_GetASXParameterValue("EntryType");
	
	try
	{
		flash_toolbar_setPlaylistEntryType(playlistEntryType);
	}
	catch(e)
	{
	}
	
	clear_Active_Timer();
	toolbar_ResetClock();
	toolbar_UpdatePlayerButtonsState(pNewState);

	switch(pNewState)
	{
		case player.PLAYER_PLAYING:
		case player.PLAYER_FASTFORWARDING:
		case player.PLAYER_REWINDING:
			gm_actions_check(pNewState);

			if (gPauseWhenReady)
			{
				gPauseWhenReady = false;
				gPauseWhenReadyForLoadingSlide = true;
				
				if (gFlashSlide_Loading_MinSeconds > 0)
					if (gFlashSlide_Loading_StartTime == null)
						gFlashSlide_Loading_StartTime = new Date();

				clear_Check_Loading_Flash_Slide_Completed_Timer();
				gCheckFlashSlideCompletedTimer = setInterval("onTimer_checkFlashLoadinSlideCompletedPlaying();", 500);

				setTimeout("player.player_Pause()", 1);
				return;
			}
			else
			{
				if (gFlashSlide_Loading_MinSeconds > 0)
					if (gFlashSlide_Loading_StartTime == null)
						gFlashSlide_Loading_StartTime = new Date();
					
				if (!loadingFlashSlideCompletedPlaying())
				{
					setTimeout("player.player_Pause()", 1);
					clear_Check_Loading_Flash_Slide_Completed_Timer();
					gCheckFlashSlideCompletedTimer = setInterval("onTimer_checkFlashLoadinSlideCompletedPlaying();", 500);
					return;
				}
			}
			
			toolbar_UpdateClock();
			display_video();
			gActiveTimer = setInterval("onTimer_Playing();", 1000);
			break;

		case player.PLAYER_PAUSED:
			try
			{
				var divProgress = document.all.item("divLoadingProgress");
				divProgress.innerHTML = "<SPAN DIR=\"" + gLanguageDirection + "\">" + getMessageText(gLanguage, MSG_PLAYER_READY_TO_PLAY) + "</SPAN>";

				if (!loadingFlashSlideCompletedPlaying())
				{
//					if (gCheckFlashSlideCompletedTimer != null)
//					{
//						if (gFlashSlide_Loading_StartTime != null)
//						{
							toolbar_UpdatePlayerButtonsState(player.PLAYER_BUFFERING);
//						}
//					}
				}
			}
			catch(e)
			{
			}
			break;
			
		case player.PLAYER_BUFFERING:
			gm_actions_check(pNewState);

			toolbar_UpdateClock();

			if (pOldState != player.PLAYER_MEDIA_LOCATING)
			{
//				alert("start");

				if (isFirstItem)
					display_buffering();
				else
					display_video();
			}
			else
			{
				stop_flash_slide_loading();
				//setTimeout("display_stopped();", 1);
			}

			onTimer_Buffering();

			gActiveTimer = setInterval("onTimer_Buffering();", 500);
			break;

		case player.PLAYER_PLAYLIST_OPENED: /* Media location acquired, maybe PLAYER_READY not sent by WMP8 (xp) */
		case player.PLAYER_READY:
			if (gEmbedStyle != 'RADIO' ||
				gStreamType != "VIDEO")
			{
				// 5, 22, 21
				
				var doStopSlides = true;
				
				if (pOldState == player.PLAYER_BUFFERING || pOldState == player.PLAYER_PLAYLIST_OPENING ||
					pOldState == player.PLAYER_PLAYLIST_OPENED)
					doStopSlides = false;
					
				display_stopped_ex(doStopSlides);				
			}
			break;

		case player.PLAYER_STOPPED:
			gm_actions_check(pNewState);

			fire_onPlayerStop();
			
			if (!gPlayerAllowStop)
			{
				setTimeout("playLastPlayedLink();", 1);
			}
			else
			{
				setTimeout("if (player.player_GetState() == player.PLAYER_STOPPED) display_stopped();", 1000);
			}
			break;

		default:
			gm_actions_check(pNewState);

			if (isFirstItem)
			{
				if (player.player_IsExtendedState(pNewState))
				{
					toolbar_UpdateClock();

					display_buffering();

					onTimer_Buffering();

					gActiveTimer = setInterval("onTimer_Buffering();", 500);
				}
			}
			else
			{
				display_video();
			}
			break;
	}
}

function clear_Active_Timer()
{
	if (gActiveTimer != null)
	{
		clearInterval(gActiveTimer);

		gActiveTimer = null;
	}
}

function clear_Check_Loading_Flash_Slide_Completed_Timer()
{
	if (gCheckFlashSlideCompletedTimer != null)
	{
		clearInterval(gCheckFlashSlideCompletedTimer);
		
		gCheckFlashSlideCompletedTimer = null;	
	}
}
