// JScript source code

//contains calls to silverlight.js, example below loads vegas.xaml
function createSilverlight()
{
	Silverlight.createObjectEx({
		source: "Page.xaml",
		parentElement: document.getElementById("SilverlightControlHost"),
		id: "SilverlightControl",
		properties: {
			width: "1024",
			height: "768",
            inplaceInstallPrompt:false,     // Determines whether to display in-place install prompt if invalid version is detected.
            background:'white',             // Background color of plug-in.
            isWindowless:'false',            // Determines whether to display plug-in in windowless mode.
            framerate:'24',                 // MaxFrameRate property value.
            version:'1.0'
		},
		events: { 
			onError:onMovieError,
			onLoad:onMovieLoad 
		}
	});
}
			

var userAgent = navigator.userAgent.toLowerCase();
var isIE = (userAgent.indexOf("msie") >= 0);
var isFireFox=document.getElementById&&!document.all;
var isGecko = (userAgent.indexOf("gecko") != -1);
var isSafari = (userAgent.indexOf("safari") != -1);
var isKonqueror = (userAgent.indexOf("konqueror") != -1);

var playerWidth = 936;
var playerHeight = 533;
var installerWindow = null;
var installerTimer = -1;
var sizerTimer = -1;

var MS_HEADER_HEIGHT = 38;
var MS_HEADER_WIDTH = 1000;

function truebody()
{
	return (document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body
}

function initPage()
{
	initLanguage();

	centerPlayer();

	//Show Language Selector
	var noPlayerContent = document.getElementById("noPlayerContent");	
	if (null != noPlayerContent) noPlayerContent.style.display = "none";

	var LanguagePanel = document.getElementById("LanguagePanel");
	if (null != LanguagePanel) LanguagePanel.style.display = "block";

	if (isSafari) sizerTimer = window.setInterval(centerPlayer, 500);

	launchTrailer();
}

function launchTrailer()
{
	//Detect Silverlight and Version
	var noPlayerContent = document.getElementById("noPlayerContent");	
	var LanguagePanel = document.getElementById("LanguagePanel");

	if (!Silverlight.isInstalled("1.0"))
	{
		if (null != noPlayerContent) noPlayerContent.style.display = "block";
		if (null != LanguagePanel) LanguagePanel.style.display="none";
		document.getElementById('container').style.display = "none";
	}
	else
	{
		if (null != noPlayerContent) noPlayerContent.style.display = "none";
		if (null != LanguagePanel) LanguagePanel.style.display="none";
		document.getElementById('container').style.display = "block";
		document.getElementById('SilverlightControlHost').focus();
	}

	setSilverlightGuidance();
}

function setSilverlightGuidance()
{
	var PostInstallGuidance = document.getElementById("lblPostInstallGuidance");

	if (PostInstallGuidance != null)
	{
		if ((typeof(window.Silverlight) != 'undefined') && (typeof(Silverlight.isBrowserRestartRequired) != 'undefined') && Silverlight.isBrowserRestartRequired)
		{
			PostInstallGuidance.innerHTML = language.INSTALL_GUIDANCE;
		}
	}
}

function installSilverlight()
{
	var pageWidth = truebody().clientWidth;
	var pageHeight = truebody().clientHeight;

	var features = "left=" + ((pageWidth - 800) / 2) + "px,top=" + ((pageHeight - 600) / 2) + "px,width=800,height=600";
	installerWindow = window.open("http://www.microsoft.com/silverlight/resources/install.aspx", "_new", features);
	installerTimer = window.setInterval(checkInstallProcess, 500);
}

function checkInstallProcess()
{
	if (Silverlight.isInstalled("1.0"))
	{
		window.clearInterval(installerTimer);
		window.location.reload();
	}
}

function centerNoPlayerContent()
{
	var pageWidth = truebody().clientWidth;
	var pageHeight = truebody().clientHeight;
	var coreWidth = 936;
	var coreHeight = 536;

	var noPlayerContent = document.getElementById("noPlayerContent");
	if (noPlayerContent != null)
	{
		noPlayerContent.style.position = "absolute";
		noPlayerContent.style.left = ((pageWidth - coreWidth) / 2) + "px";
		noPlayerContent.style.top = "0px";
	}

	var LanguagePanel = document.getElementById("LanguagePanel");
	if (LanguagePanel != null)
	{
		LanguagePanel.style.position = "absolute";
		LanguagePanel.style.left = ((pageWidth - coreWidth) / 2) + "px";
		LanguagePanel.style.top = ((pageHeight - coreHeight) / 2) + "px";
	}

	var MAX_BACKDROP_POSX = 92;
	var posX = Math.max(posX = (pageWidth / 2) - 600, -66);
	document.body.style.backgroundPosition = posX + "px -66px";
}

function onPlayerResize(sender, eventArgs)
{
	try
	{
		centerPlayer();
	}
	catch(e)
	{

	}
}

function sizeSilverlight()
{
	try
	{
		var pageWidth = truebody().clientWidth;
		var pageHeight = truebody().clientHeight;

		var silverlightControl = document.getElementById("SilverlightControl");
		if (silverlightControl != null)
		{
			silverlightControl.width = pageWidth;
			silverlightControl.height = (isFullScreen ? pageHeight : pageHeight - MS_HEADER_HEIGHT);
		}
	}
	catch(e)
	{

	}
}

function centerPlayer()
{
	sizeSilverlight();	//This is done twice in this function on purpose - this is a hack to get Windows FireFox to paint right

	var container = document.getElementById("globalContainer");
	var footer = document.getElementById("footer");
	var pageWidth = truebody().clientWidth;
	var pageHeight = truebody().clientHeight;

	if (container != null)
	{
		container.style.left = "0px";
		container.style.top = "0px";
		container.style.width = pageWidth + "px";
		container.style.height = (pageHeight - MS_HEADER_HEIGHT) + "px";
	}

	if (footer != null)
	{
		footer.style.left = ((pageWidth - MS_HEADER_WIDTH) / 2) + "px";
		footer.style.top = (pageHeight - MS_HEADER_HEIGHT) + "px";
		footer.style.width = MS_HEADER_WIDTH + "px";
		footer.style.height = MS_HEADER_HEIGHT + "px";
		if (!isFullScreen) footer.style.display = "block";
	}

	//No Player Layout
	centerNoPlayerContent();

	if (movieStage != null)
	{
		setBounds(movieStage, 0, 0, pageWidth, pageHeight - MS_HEADER_HEIGHT);
	}

	if (outerCanvas != null)
	{
		var x = Math.max(200, (pageWidth - outerCanvas.Width) / 2);
		var y = 0;

		setBounds(outerCanvas, x,  y, outerCanvas.Width, outerCanvas.Height);
		
		//Locate Back drop image
		var backDrop = outerCanvas.FindName("BackDrop");
		if (backDrop != null) setBounds(backDrop, x- 78 - 200, -66, backDrop.Width, backDrop.Height);

		var trailerCanvas = outerCanvas.FindName("TrailerCanvas");
		var trailerPlayer = outerCanvas.FindName("TrailerPlayer");
		var TrailerFullScreen = outerCanvas.FindName("TrailerFullScreen");
		var MessageBox = outerCanvas.FindName("MessageBox");

		if (null != trailerCanvas && null != trailerPlayer && null != fullScreenZoomer && null != TrailerFullScreen)
		{
			if (isFullScreen)
			{
				TrailerFullScreen.Width = fullScreenZoomer.Width = pageWidth;
				TrailerFullScreen.Height = fullScreenZoomer.Height = pageHeight;
			}
			else
			{
				var x = (GetElementX(outerCanvas) + GetElementX(trailerPlayer) + GetElementX(trailerCanvas) + 20);
				var y = (GetElementY(outerCanvas) + GetElementY(trailerPlayer) + GetElementY(trailerCanvas) + 29);

				TrailerFullScreen.SetValue("Canvas.Left", 0);
				TrailerFullScreen.SetValue("Canvas.Top", 0);

				fullScreenZoomer.SetValue("Canvas.Left", x);
				fullScreenZoomer.SetValue("Canvas.Top", y);
			}
		}

		if (MessageBox != null)
		{
			setBounds(MessageBox, (pageWidth - MessageBox.Width) / 2, (pageHeight - MessageBox.Height) / 2, MessageBox.Width, MessageBox.Height);
		}

		var FullScreenBar = outerCanvas.FindName("FullScreenBar");
		if (null != FullScreenBar)
		{
			FullScreenBar.SetValue("Canvas.Left", (pageWidth - FullScreenBar.Width) / 2);
			FullScreenBar.SetValue("Canvas.Top", (pageHeight - FullScreenBar.Height - 32));
		}

		var FullScreenPercent = outerCanvas.FindName("FullScreenPercent");
		if (null != FullScreenPercent)
		{
			FullScreenPercent.SetValue("Canvas.Left", (pageWidth - FullScreenPercent.ActualWidth) / 2);
		}
	}

	sizeSilverlight();
}

function initLanguage()
{
	setElementText("lblPlayDate", language.PLAY_DATE);
	setElementText("lblPlayDate2", language.PLAY_DATE);
	setElementText("lblPoweredBy", language.CLICK_TO_VIEW);
}

function setElementText(id, text)
{
	var elem = document.getElementById(id);
	if (elem != null) elem.innerHTML = text;
}

function setBounds(element, x, y, width, height)
{
	if (element != null)
	{
		element.SetValue("Canvas.Left", x);
		element.SetValue("Canvas.Top", y);
		element.Width = width;
		element.Height = height;
	}
}

//No Player Content - Play Button
var playButtonImages = new Array();
playButtonImages[0] = new Image();
playButtonImages[0].src = "images/PlayButtonNorm.png";
playButtonImages[1] = new Image();
playButtonImages[1].src = "images/PlayButtonHover.png";
playButtonImages[2] = new Image();
playButtonImages[2].src = "images/PlayButtonDown.png";

function onPlayButtonMouseOver(button)
{
	if (button != null) button.src = playButtonImages[1].src;
}

function onPlayButtonMouseOut(button)
{
	if (button != null) button.src = playButtonImages[0].src;
}

function onPlayButtonMouseDown(button)
{
	if (button != null) button.src = playButtonImages[2].src;
}

function onPlayButtonMouseUp(button)
{
	if (button != null) button.src = playButtonImages[1].src;
	installSilverlight();
}