if (!window.SilverlightMediaPlayer)
	window.SilverlightMediaPlayer = {};

SilverlightMediaPlayer.Page = function() 
{
}

SilverlightMediaPlayer.Page.prototype =
{
	handleLoad: function(control, userContext, rootElement) 
	{
		this.control = control;
		
		// Sample event hookup:	
		rootElement.addEventListener("MouseLeftButtonDown", Silverlight.createDelegate(this, this.handleMouseDown));
	},
	
	// Sample event handler
	handleMouseDown: function(sender, eventArgs) 
	{
		// The following line of code shows how to find an element by name and call a method on it.
		// this.control.content.findName("Timeline1").Begin();
	}
}

function canvasLoaded(sender, args)
{
    var theHost = document.getElementById("SilverlightControl");
    theHost.content.findName("bgWhite").Visibility = "Collapsed";
     theHost.content.findName("bgWhite").Height = movieHeight;
     theHost.content.findName("bgWhite").Width = movieWidth;
     
     theHost.content.findName("bg").Height = movieHeight;
     theHost.content.findName("bg").Width = movieWidth;
   //theHost.content.findName("cMain").Height = 800;  
    // Load Icons
    theHost.content.findName("btnPlay").Source = btnPlay;
    
    showControls();
    reloadMenu = 1;
        
    // Load Text
    theHost.content.findName("txtMainTitle").Text = mainTitle;
    loadTxt(0);
	
	// Determine length of playlist and hide unnecessary media buttons
	theHost.content.findName("vid2").Visibility = "Collapsed";
	theHost.content.findName("vid2Ref").Visibility = "Collapsed";
	theHost.content.findName("bg2").Visibility = "Collapsed";
	theHost.content.findName("vid3").Visibility = "Collapsed";
	theHost.content.findName("vid3Ref").Visibility = "Collapsed";
	theHost.content.findName("bg3").Visibility = "Collapsed";
	theHost.content.findName("vid4").Visibility = "Collapsed";
	theHost.content.findName("vid4Ref").Visibility = "Collapsed";
	theHost.content.findName("bg4").Visibility = "Collapsed";
	theHost.content.findName("vid5").Visibility = "Collapsed";
	theHost.content.findName("vid5Ref").Visibility = "Collapsed";
	theHost.content.findName("bg5").Visibility = "Collapsed";
    
    // Load Main Clip
    theHost.content.findName("media").IsMuted = true;
    theHost.content.findName("media").Width = movieWidth *2/ 5;
    theHost.content.findName("media").Height = movieHeight*2/ 5;
    
   theHost.content.findName("mediaBG").Width =  theHost.content.findName("media").Width + 12;
    theHost.content.findName("mediaBG").Height = theHost.content.findName("media").Height + 12;
    theHost.content.findName("media").Source = playList[0];
    theHost.content.findName("mediaReflection").Source = playList[0];
    // Preload main media, stop on 1 sec to display image of clip
    setTimeout("delayPause('media')",1500);
    setTimeout("delayPause('mediaReflection')",1500);
    // At first we want to mute on our pre load, now un-mute
    setTimeout("delayNoMute('media')",1500);
	// Opening animation for main media
	theHost.content.findName("StartVid").Begin();
	
	playMediaButtons();
}

function delayPause(ctl){
    var theHost = document.getElementById("SilverlightControl");
    theHost.content.findName(ctl).Pause();
}

function delayNoMute(ctl){
    var theHost = document.getElementById("SilverlightControl");
    theHost.content.findName(ctl).IsMuted = false;
}

function playMediaButtons()
{
    var theHost = document.getElementById("SilverlightControl");
	// Load media btns
    theHost.content.findName("ShowMediaButtons").Begin();
    
    // 1
    if (imgList[0] == "")
	{
	    playVid1Btn();
	}
	else
	{
	    theHost.content.findName("img1").Source = imgList[0];
	    theHost.content.findName("bg1").Visibility = "Visible";
	}
	
	// 2
	if (playList.length > 1)
    {
        if (imgList[1] == "")
	    {
	        setTimeout("playVid2Btn()", 500);
	    }
	    else
	    {
	        theHost.content.findName("img2").Source = imgList[1];
	        theHost.content.findName("bg2").Visibility = "Visible";
	    }
    }
    
    // 3
    if (playList.length > 2)
    {
	    if (imgList[2] == "")
	    {
	        setTimeout("playVid3Btn()", 1000);
	    }
	    else
	    {
	        theHost.content.findName("img3").Source = imgList[2];
	        theHost.content.findName("bg3").Visibility = "Visible";
	    }
    }
    
    // 4
    if (playList.length > 3)
    {
        if (imgList[3] == "")
	    {
	        setTimeout("playVid4Btn()", 1500);
	    }
	    else
	    {
	        theHost.content.findName("img4").Source = imgList[3];
	        theHost.content.findName("bg4").Visibility = "Visible";
	    }
    }
    
    // 5
    if (playList.length > 4)
    {
        if (imgList[4] == "")
	    {
	        setTimeout("playVid5Btn()", 2000);
	    }
	    else
	    {
	        theHost.content.findName("img5").Source = imgList[4];
	        theHost.content.findName("bg5").Visibility = "Visible";
	    }
    }		
}

function playVid1Btn()
{
    var theHost = document.getElementById("SilverlightControl");
	if (theHost.content.findName('media').downloadProgress == 1)
	{
		// load next vid
		loadVid(0);
	}
	else
	{
		setTimeout("playVid1Btn()", 1000);
	}
}
function playVid2Btn()
{
    var theHost = document.getElementById("SilverlightControl");
	if (theHost.content.findName('media').downloadProgress == 1)
	{
		// load next vid
		loadVid(1);
	}
	else
	{
		setTimeout("playVid2Btn()", 1000);
	}
}
function playVid3Btn()
{
    var theHost = document.getElementById("SilverlightControl");    
	if (theHost.content.findName('media').downloadProgress == 1)
	{
		// load next vid
		loadVid(2);
	}
	else
	{
		setTimeout("playVid3Btn()", 1000);
	}
}
function playVid4Btn()
{
    var theHost = document.getElementById("SilverlightControl");    
	if (theHost.content.findName('media').downloadProgress == 1)
	{
		// load next vid
		loadVid(3);
	}
	else
	{
		setTimeout("playVid4Btn()", 1000);
	}
}
function playVid5Btn()
{
    var theHost = document.getElementById("SilverlightControl");    
	if (theHost.content.findName('media').downloadProgress == 1)
	{
		// load next vid
		loadVid(4);
	}
	else
	{
		setTimeout("playVid5Btn()", 1000);
	}
}

function loadVid(i)
{
	tmpi = i
    var theHost = document.getElementById("SilverlightControl");
	vid = ('vid' + (tmpi + 1));
	vidRef = ('vid' + (tmpi + 1) + 'Ref');
	theHost.content.findName(vid).Source = playList[tmpi];
	theHost.content.findName(vidRef).Source = playList[tmpi];
	
	theHost.content.findName(vid).Visibility = "Visible";
    theHost.content.findName(vidRef).Visibility = "Visible";
    theHost.content.findName("bg" + (tmpi + 1)).Visibility = "Visible";
	
	if (tmpi == 0)
    {
		setTimeout("delayPause('vid1')",1000);
		setTimeout("delayPause('vid1Ref')",1000);
    }
	if (tmpi == 1)
    {
		setTimeout("delayPause('vid2')",1000);
		setTimeout("delayPause('vid2Ref')",1000);    }
    if (tmpi == 2)
    {
		setTimeout("delayPause('vid3')",1000);
		setTimeout("delayPause('vid3Ref')",1000);
    }
    if (tmpi == 3)
    {
		setTimeout("delayPause('vid4')",1000);
		setTimeout("delayPause('vid4Ref')",1000);
    }
    if (tmpi == 4)
    {
		setTimeout("delayPause('vid5')",1000);
		setTimeout("delayPause('vid5Ref')",1000);
    }
}

function playPauseToggle() {
    var theHost = document.getElementById("SilverlightControl");
    if (togglePause == 0)
    {
        // Check if reached end so if we click play again it will play from start
        if (reachedEnd > 0)
        {
            theHost.content.findName("media").Stop();
            theHost.content.findName("mediaReflection").Stop();
        }
        theHost.content.findName("media").Play();
        theHost.content.findName("mediaReflection").Play();
        theHost.content.findName("media").IsMuted = false;
        theHost.content.findName("btnPlay").Source = btnPause;
        togglePause = 1;
    }
    else
    {
        pause();
        theHost.content.findName("btnPlay").Source = btnPlay;
        togglePause = 0;
    }
}

function pause() {
    var theHost = document.getElementById("SilverlightControl");
    theHost.content.findName("media").Pause();
    theHost.content.findName("mediaReflection").Pause();
}

function showControls() {
    var theHost = document.getElementById("SilverlightControl");
    if (reloadMenu == 0)
    {
        theHost.content.findName("ShowControls").Begin();
    }
    if (theHost.content.findName("cBtns").Visibility == "Collapsed")
    {
        theHost.content.findName("cBtns").Visibility = "Visible";
    }
}

function hideControls() {
    var theHost = document.getElementById("SilverlightControl");
    theHost.content.findName("HideControls").Begin();
    reloadMenu = 0;
}

function played() {
    var theHost = document.getElementById("SilverlightControl");
    reachedEnd = 1;
    reloadMenu = 1;
    theHost.content.findName("ShowControls").Begin();
    theHost.content.findName("btnPlay").Source = btnPlay;
    togglePause = 0;
}

function hideImg1() {
    var theHost = document.getElementById("SilverlightControl");
    if (theHost.content.findName("vid1").Source.length == 0)
    {
        theHost.content.findName("vid1").Source = playList[0];
	    theHost.content.findName("vid1Ref").Source = playList[0];
	    theHost.content.findName("vid1").Visibility = "Visible";
        theHost.content.findName("vid1Ref").Visibility = "Visible";
    }
    theHost.content.findName("img1").Visibility = "Collapsed"
}
function hideImg2() {
    var theHost = document.getElementById("SilverlightControl");
    if (theHost.content.findName("vid2").Source.length == 0)
    {
        theHost.content.findName("vid2").Source = playList[1];
	    theHost.content.findName("vid2Ref").Source = playList[1];
	    theHost.content.findName("vid2").Visibility = "Visible";
        theHost.content.findName("vid2Ref").Visibility = "Visible";
    }
    theHost.content.findName("img2").Visibility = "Collapsed"
}
function hideImg3() {
    var theHost = document.getElementById("SilverlightControl");
    if (theHost.content.findName("vid3").Source.length == 0)
    {
        theHost.content.findName("vid3").Source = playList[2];
	    theHost.content.findName("vid3Ref").Source = playList[2];
	    theHost.content.findName("vid3").Visibility = "Visible";
        theHost.content.findName("vid3Ref").Visibility = "Visible";
    }
    theHost.content.findName("img3").Visibility = "Collapsed"
}
function hideImg4() {
    var theHost = document.getElementById("SilverlightControl");
    if (theHost.content.findName("vid4").Source.length == 0)
    {
        theHost.content.findName("vid4").Source = playList[3];
	    theHost.content.findName("vid4Ref").Source = playList[3];
	    theHost.content.findName("vid4").Visibility = "Visible";
        theHost.content.findName("vid4Ref").Visibility = "Visible";
    }
    theHost.content.findName("img4").Visibility = "Collapsed"
}
function hideImg5() {
    var theHost = document.getElementById("SilverlightControl");
    if (theHost.content.findName("vid5").Source.length == 0)
    {
        theHost.content.findName("vid5").Source = playList[4];
	    theHost.content.findName("vid5Ref").Source = playList[4];
	    theHost.content.findName("vid5").Visibility = "Visible";
        theHost.content.findName("vid5Ref").Visibility = "Visible";
    }
    theHost.content.findName("img5").Visibility = "Collapsed"
}

function previewVid1() {
    var theHost = document.getElementById("SilverlightControl");
    theHost.content.findName("vid1").Play();
    theHost.content.findName("vid1Ref").Play();
    theHost.content.findName("media").IsMuted = false;
}
function stopPreviewVid1() {
    var theHost = document.getElementById("SilverlightControl");
    theHost.content.findName("vid1").Pause();
    theHost.content.findName("vid1Ref").Pause();
}
function playVid1() {
    var theHost = document.getElementById("SilverlightControl");
    theHost.content.findName("media").Source = playList[0];
    theHost.content.findName("mediaReflection").Source = playList[0];
    loadTxt(0);
    theHost.content.findName("cBtns").Visibility = "Collapsed";
    theHost.content.findName("btnPlay").Source = btnPause;
    togglePause = 1;
}

function previewVid2() {
    var theHost = document.getElementById("SilverlightControl");
    theHost.content.findName("vid2").Play();
    theHost.content.findName("vid2Ref").Play();
}
function stopPreviewVid2() {
    var theHost = document.getElementById("SilverlightControl");
    theHost.content.findName("vid2").Pause();
    theHost.content.findName("vid2Ref").Pause();
}
function playVid2() {
    var theHost = document.getElementById("SilverlightControl");
    theHost.content.findName("media").Source = playList[1];
    theHost.content.findName("mediaReflection").Source = playList[1];
    loadTxt(1);
    theHost.content.findName("cBtns").Visibility = "Collapsed";
    theHost.content.findName("btnPlay").Source = btnPause;
    togglePause = 1;
}

function previewVid3() {
    var theHost = document.getElementById("SilverlightControl");
    theHost.content.findName("vid3").Play();
    theHost.content.findName("vid3Ref").Play();
}
function stopPreviewVid3() {
    var theHost = document.getElementById("SilverlightControl");
    theHost.content.findName("vid3").Pause();
    theHost.content.findName("vid3Ref").Pause();
}
function playVid3() {
    var theHost = document.getElementById("SilverlightControl");
    theHost.content.findName("media").Source = playList[2];
    theHost.content.findName("mediaReflection").Source = playList[2];
    loadTxt(2);
    theHost.content.findName("cBtns").Visibility = "Collapsed";
    theHost.content.findName("btnPlay").Source = btnPause;
    togglePause = 1;
}

function previewVid4() {
    var theHost = document.getElementById("SilverlightControl");
    theHost.content.findName("vid4").Play();
    theHost.content.findName("vid4Ref").Play();
}
function stopPreviewVid4() {
    var theHost = document.getElementById("SilverlightControl");
    theHost.content.findName("vid4").Pause();
    theHost.content.findName("vid4Ref").Pause();
}
function playVid4() {
    var theHost = document.getElementById("SilverlightControl");
    theHost.content.findName("media").Source = playList[3];
    theHost.content.findName("mediaReflection").Source = playList[3];
    loadTxt(3);
    theHost.content.findName("cBtns").Visibility = "Collapsed";
    theHost.content.findName("btnPlay").Source = btnPause;
    togglePause = 1;
}

function previewVid5() {
    var theHost = document.getElementById("SilverlightControl");
    theHost.content.findName("vid5").Play();
    theHost.content.findName("vid5Ref").Play();
}
function stopPreviewVid5() {
    var theHost = document.getElementById("SilverlightControl");
    theHost.content.findName("vid5").Pause();
    theHost.content.findName("vid5Ref").Pause();
}
function playVid5() {
    var theHost = document.getElementById("SilverlightControl");
    theHost.content.findName("media").Source = playList[4];
    theHost.content.findName("mediaReflection").Source = playList[4];
    loadTxt(4);
    theHost.content.findName("cBtns").Visibility = "Collapsed";
    theHost.content.findName("btnPlay").Source = btnPause;
    togglePause = 1;
}

function loadTxt(index) {
    var theHost = document.getElementById("SilverlightControl");
    theHost.content.findName("txtSubTitle").Text = titleList[index];
    theHost.content.findName("txtBody").Text = txtList[index];

    theHost.content.findName("txtSubTitle")['Canvas.Left'] = movieWidth / 2 + 20;
    theHost.content.findName("txtBody")['Canvas.Left'] = movieWidth / 2 + 20;
    
     theHost.content.findName("mediaButtons")['Canvas.Top'] = movieHeight / 2 + 60;
}