var SCENE_FILE = '/ClientBin/Scene.xaml';
var CANVAS_WIDTH = '500px';
var CANVAS_HEIGHT = '300px';
var BUTTON_PLAY = '/ClientBin/Icons/media_play.png';
var BUTTON_PAUSE = '/ClientBin/Icons/media_pause.png';
var REACHED_END = 0;
var RELOAD_MENU = 0;
var CURRENT_VIDEO = 0;
var CONTROL_TIMEOUT_ID = 0;

var VIDEO_OVERLAY_ID = 'prompt_overlay';
var VIDEO_PROMPT_BOX_ID = 'video_prompt_box';
var VIDEO_PROMPT_BOX_SHADOW_ID = 'video_prompt_box_shadow';
var VIDEO_PROMPT_MOUSE_X = 0;
var VIDEO_PROMPT_MOUSE_Y = 0;
var VIDEO_PROMPT_SHADOW_BORDER = 10;
var VIDEO_SCROLL_TIMEOUT_ID = 0;
var VIDEO_SCROLL_WATCH = false;

function createSilverlight()
{
	var scene = new SilverlightMediaPlayer.Page();
	Silverlight.createObjectEx({
		source: SCENE_FILE,
		parentElement: document.getElementById("SilverlightControlHost"),
		id: "SilverlightControl",
		properties: {
			width: "100%",
			height: "100%",
			version: "1.0"
		},
		events: {
			onLoad: Silverlight.createDelegate(scene, scene.handleLoad)
		}
	});
}

if (!window.Silverlight) 
	window.Silverlight = {};

Silverlight.createDelegate = function(instance, method) {
	return function() {
		return method.apply(instance, arguments);
	}
}

function PromptVideo_Show(file)
{
    var c = document.createElement('div');
    c.setAttribute('id', VIDEO_OVERLAY_ID);
    c.className = 'Redirect_Prompt_Overlay';
    c.onclick = function() { PromptVideo_Hide(); }
    document.body.appendChild(c);
    
    c = document.createElement('div');
    c.setAttribute('id', VIDEO_PROMPT_BOX_ID);
    c.style.width = CANVAS_WIDTH;
    c.style.height = CANVAS_HEIGHT;
    c.style.backgroundColor = 'white';
    c.style.position = 'absolute';
    
    var d = document.createElement('div');
    d.setAttribute('id', 'SilverlightControlHost');
    d.style.width = CANVAS_WIDTH;
    d.style.height = CANVAS_HEIGHT;
    c.appendChild(d);
    document.body.appendChild(c);
    createSilverlight();
    
    c = document.createElement('div');
    c.setAttribute('id', VIDEO_PROMPT_BOX_SHADOW_ID);
    c.className = 'Redirect_Prompt_Shadow';
    c.style.width = (PromptVideo_GetPromptBoxWidth() + (VIDEO_PROMPT_SHADOW_BORDER * 2)) + 'px';
    c.style.height = (PromptVideo_GetPromptBoxHeight() + (VIDEO_PROMPT_SHADOW_BORDER * 2)) + 'px';
    document.body.appendChild(c);

    VIDEO_SCROLL_WATCH = true;
    PromptVideo_AdjustScrollPosition();
    setTimeout("loadVid('" + file + "')",1000);
    window.document.onkeyup = function(e) { var kc = (window.event) ? event.keyCode : e.keyCode; if (kc == 27) { PromptVideo_Hide(); } };
}
function PromptVideo_Hide()
{
    VIDEO_SCROLL_WATCH = false;
    clearTimeout(VIDEO_SCROLL_TIMEOUT_ID);
    document.body.removeChild(document.getElementById(VIDEO_OVERLAY_ID));
    document.body.removeChild(document.getElementById(VIDEO_PROMPT_BOX_ID));
    document.body.removeChild(document.getElementById(VIDEO_PROMPT_BOX_SHADOW_ID));
    window.document.onkeyup = null;
}
function PromptVideo_GetTopPosition()
{
    return (PromptVideo_GetOverlayHeight() / 2) - ((PromptVideo_GetPromptBoxHeight() + (VIDEO_PROMPT_SHADOW_BORDER * 2)) / 2) + document.documentElement.scrollTop;
}
function PromptVideo_GetShadowTopPosition()
{
    return PromptVideo_GetTopPosition() - VIDEO_PROMPT_SHADOW_BORDER;
}
function PromptVideo_GetLeftPosition()
{
    return (PromptVideo_GetOverlayWidth() / 2) - ((PromptVideo_GetPromptBoxWidth() + (VIDEO_PROMPT_SHADOW_BORDER * 2)) / 2);
}
function PromptVideo_GetShadowLeftPosition()
{
    return PromptVideo_GetLeftPosition() - VIDEO_PROMPT_SHADOW_BORDER;
}
function PromptVideo_GetPromptBoxWidth()
{
    return document.getElementById(VIDEO_PROMPT_BOX_ID).offsetWidth;
}
function PromptVideo_GetPromptBoxHeight()
{
    return document.getElementById(VIDEO_PROMPT_BOX_ID).offsetHeight;
}
function PromptVideo_GetOverlayWidth()
{
    return document.getElementById(VIDEO_OVERLAY_ID).offsetWidth;
}
function PromptVideo_GetOverlayHeight()
{
    return document.getElementById(VIDEO_OVERLAY_ID).offsetHeight;
}
function PromptVideo_AdjustScrollPosition()
{
    PromptVideo_SetPosition(VIDEO_OVERLAY_ID, document.documentElement.scrollTop, 0);
    PromptVideo_SetPosition(VIDEO_PROMPT_BOX_ID, PromptVideo_GetTopPosition(), PromptVideo_GetLeftPosition());
    PromptVideo_SetPosition(VIDEO_PROMPT_BOX_SHADOW_ID, PromptVideo_GetShadowTopPosition(), PromptVideo_GetShadowLeftPosition());
    
    if(VIDEO_SCROLL_WATCH) { VIDEO_SCROLL_TIMEOUT_ID = setTimeout('PromptVideo_AdjustScrollPosition()',1); }
}
function PromptVideo_SetPosition(id, top, left)
{
    var d = document.getElementById(id);
    d.style.top = top + 'px';
    d.style.left = left + 'px';
}

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("cMain").Width = 500;
    theHost.content.findName("cMain").Height = 300;

    theHost.content.findName("btnPlay").Source = BUTTON_PLAY;

    showControls();
    RELOAD_MENU = 1;

    theHost.content.findName("media").Width = 300;
    theHost.content.findName("media").Height = 225;
}

function delayPause(ctl)
{
    var theHost = document.getElementById("SilverlightControl");
    theHost.content.findName(ctl).Pause();
    theHost.content.findName("btnPlay").Source = BUTTON_PLAY;
}

function delayNoMute(ctl)
{
    var theHost = document.getElementById("SilverlightControl");
    theHost.content.findName(ctl).IsMuted = false;
}

function loadVid(file)
{
    var theHost = document.getElementById("SilverlightControl");
    if(theHost.content.findName('mainMedia').Visibility == "Collapsed") { theHost.content.findName('mainMedia').Visibility = "Visible"; }
	theHost.content.findName('media').Stop();
	theHost.content.findName('media').Source = 'media/' + file;
    theHost.content.findName("btnPlay").Source = BUTTON_PAUSE;
    REACHED_END = 0;
	CURRENT_VIDEO = i;

	if(theHost.content.findName('cBtns').Opacity == 0) { showControls(); }
}

function playPauseToggle()
{
    var theHost = document.getElementById("SilverlightControl");
    if (theHost.content.findName("btnPlay").Source == BUTTON_PLAY)
    {
        // Check if reached end so if we click play again it will play from start
        if (REACHED_END > 0) { theHost.content.findName("media").Stop(); REACHED_END = 0; }
        theHost.content.findName("media").Play();
        theHost.content.findName("btnPlay").Source = BUTTON_PAUSE;
    }
    else
    {
        theHost.content.findName("media").Pause();
        theHost.content.findName("btnPlay").Source = BUTTON_PLAY;
    }
}

function showControls()
{
    clearTimeout(CONTROL_TIMEOUT_ID);
    var theHost = document.getElementById("SilverlightControl");
    if(theHost.content.findName("cBtns").Opacity == 0)
    {
        if (RELOAD_MENU == 0) { theHost.content.findName("ShowControls").Begin(); }
        if(theHost.content.findName("cBtns").Visibility == "Collapsed") { theHost.content.findName("cBtns").Visibility = "Visible"; }
    }
}

function hideControls()
{
    clearTimeout(CONTROL_TIMEOUT_ID);
    var theHost = document.getElementById("SilverlightControl");
    theHost.content.findName("HideControls").Begin();
    RELOAD_MENU = 0;
}

function played()
{
    var theHost = document.getElementById("SilverlightControl");
    REACHED_END = 1;
    RELOAD_MENU = 1;
    theHost.content.findName("ShowControls").Begin();
    theHost.content.findName("btnPlay").Source = BUTTON_PLAY;
}

function PlayPrev()
{
    played();
    var i = 0;
    if(CURRENT_VIDEO == 0) { i = (playList.length - 1) }
    else { i = (CURRENT_VIDEO - 1); }
    
    loadVid(i);
}

function PlayNext()
{
    played();
    var i = 0;
    if(CURRENT_VIDEO == (playList.length - 1)) { i = 0; }
    else { i = (CURRENT_VIDEO + 1); }
    
    loadVid(i);
}

function HideMedia()
{
    var theHost = document.getElementById("SilverlightControl");
    theHost.content.findName("mainMedia").Visibility = "Collapsed";
}

function ShowMedia()
{
    var theHost = document.getElementById("SilverlightControl");
    theHost.content.findName("mainMedia").Visibility = "Visible";
}

function PlayMedia()
{
    var theHost = document.getElementById("SilverlightControl");
    if(theHost.content.findName('mainMedia').Visibility == "Collapsed") { loadVid(CURRENT_VIDEO); }
    else if(theHost.content.findName("media").CurrentState != "Playing")
    {
        if(REACHED_END == 1)
        {
            theHost.content.findName("media").Stop();
            REACHED_END = 0;
        }
        theHost.content.findName("media").Play();
        theHost.content.findName("btnPlay").Source = BUTTON_PAUSE;
    }
    else { playPauseToggle(); }
}

function StopMedia()
{
    var theHost = document.getElementById("SilverlightControl");
    theHost.content.findName("media").Stop();
    theHost.content.findName("btnPlay").Source = BUTTON_PLAY;
}

function PauseMedia()
{
    var theHost = document.getElementById("SilverlightControl");
    theHost.content.findName("media").Pause();
    theHost.content.findName("btnPlay").Source = BUTTON_PLAY;
    showControls();
}

function PlayVideo(index)
{
    clearTimeout(CONTROL_TIMEOUT_ID);
    loadVid(index);
}

function HideAllDescriptions()
{
    var array = new Array();
    array = document.getElementById("descDiv").getElementsByTagName("p");
    for(var i = 0; i < array.length; i++)
    {
        array[i].style.display = 'none';
    }
}

function onStateChanged(sender, eventArgs)
{
    var media = sender.findName("media");
	if(media.CurrentState == "Playing") { onPlayProgress(sender);  }
	printTime(sender, media.position.seconds);
}
function onPlayProgress(sender)
{
    var animation = sender.findName("playAnimation");
    animation.stop();
    var media = sender.findName("media");
    var background = sender.findName("ProgressBarBackground");
    var fill = sender.findName("ProgressBarFill");
    
    fill.setValue("Width", ((media.position.seconds / media.NaturalDuration.seconds) * (background.Width - 2)));
    printTime(sender, media.position.seconds);
    if(sender.findName("media").CurrentState == "Playing") { animation.begin(); }
}
function printTime(sender, current)
{ 
    var time = sender.findName("ProgressTime");
    var hrs = Math.floor(current / 3600);
    var mins = Math.floor((current % 3600) / 60);
    var secs = Math.floor(current % 60);
    var str = "";
    
    if(hrs > 0) { str += hrs + ":"; }
    if(mins > 0) { str += mins + ":"; }
    else { str += "0:"; }
    if(secs < 10) { str += "0"; }
    str += secs;
    
    time.Text = str;
}
function ProgressBarLeftMouseUp(sender, eventArgs)
{
    var background = sender.findName("ProgressBarBackground");
    var backfill = sender.findName("ProgressBarFill");
    var media = sender.findName("media");
    var position = media.position;
    var clickPos = eventArgs.getPosition(background).x;
    
    if(clickPos > (background.Width - 2)) { clickPos = (background.Width - 2); }
    position.seconds = ((media.NaturalDuration.seconds / background.Width) * clickPos);
    
    backfill.setValue("Width", clickPos);
    media.position = position;
    media.Play();
    sender.findName("BUTTON_PLAY").Source = BUTTON_PAUSE;
    onPlayProgress(sender);
}
function FillBarLeftMouseUp(sender, eventArgs)
{
    var fill = sender.findName("ProgressBarFill");
    var media = sender.findName("media");
    var position = media.position;
    var clickPos = eventArgs.getPosition(fill).x;
    
    position.seconds = ((position.seconds / fill.Width) * clickPos);
    fill.setValue("Width", clickPos);
    media.position = position;
    media.Play();
    sender.findName("BUTTON_PLAY").Source = BUTTON_PAUSE;
    onPlayProgress(sender);
}
