﻿if (!window.myplayer)
    myplayer = {};

myplayer.Page = function() {
}

myplayer.Page.prototype =
{
    handleLoad: function(control, userContext, rootElement) {
        this.control = control;
        this.plugin;
        rootElement.findName("playBtn").addEventListener("MouseLeftButtonDown", Silverlight.createDelegate(this, this.handlePlayBtn));
        rootElement.findName("closeBtn").addEventListener("MouseLeftButtonDown", Silverlight.createDelegate(this, this.handleStopBtn));
        rootElement.findName("setFullScreen").addEventListener("MouseLeftButtonDown", Silverlight.createDelegate(this, this.handleSetFullscreen));
        rootElement.findName("vid").addEventListener("CurrentStateChanged", Silverlight.createDelegate(this, this.handleStateChanged));
        rootElement.findName("vid").addEventListener("MediaEnded", Silverlight.createDelegate(this, this.handleMediaEnded));

        rootElement.findName("vid").addEventListener("BufferingProgressChanged", Silverlight.createDelegate(this, this.handleBufferingProgressChanged));
    },
    handleBufferingProgressChanged: function(sender, eventArgs) {
        sender.findName("bufferingStatus").Text = String(Math.floor(sender.BufferingProgress * 100)) + "%";
    },
    handleMediaEnded: function(sender, eventArgs) {
        $("silverlightControlHost").setStyle({ left: "-1500px" });
    },
    handleStateChanged: function(sender, eventArgs) {

    var media = sender.findName("vid");
    if (media.CurrentState == "Buffering") {
        sender.findName("VideoState").Text = String(media.CurrentState);
        sender.findName("BufferingStatusCanvas").visibility = "Visible";

    } else if (media.CurrentState == "Opening") {
        sender.findName("VideoState").Text = "Öffnen";
        sender.findName("BufferingStatusCanvas").visibility = "Visible";
    }   
    else {
        sender.findName("BufferingStatusCanvas").visibility = "Collapsed";
        sender.findName("VideoState").Text = "";
        sender.findName("bufferingStatus").Text = "";

    }
        
    },
    handleRootMouseOver: function(sender, eventArgs) {
        sender.findName("setFullScreen").visibility = "Visible";
        sender.findName("closeBtn").visibility = "Visible";
    },
    handleRootMouseOut: function(sender, eventArgs) {
        sender.findName("setFullScreen").visibility = "Collapsed";
        sender.findName("closeBtn").visibility = "Collapsed";
    },
    handlePlayBtnExt: function(sender, eventArgs) {
        //rootElement.findName("vid").play();
        playVid();
    },
    handlePlayBtn: function(sender, eventArgs) {
        sender.findName("vid").play();
    },
    handleStopBtn: function(sender, eventArgs) {
        sender.findName("vid").stop();
        $("silverlightControlHost").setStyle({ left: "-1500px" });
    },
    handleSetFullscreen: function(sender, eventArgs) {
        plugin = sender.getHost();
        plugin.content.fullScreen = true;
        plugin.content.onFullScreenChange = onFullScreenChanged;

        // Do initial layout of the app based on initial size.
        updateLayout(plugin.content.actualWidth, plugin.content.actualHeight);
    }
}
function onFullScreenChanged(sender, eventArgs) {
    // Do layout resizing of the app whenever the FullScreen property changes.

    if (plugin.content.FullScreen == false) {
        plugin.content.findName("Page").height = "190";
        plugin.content.findName("Page").width = "332";
        plugin.content.findName("vid").height = "182";
        plugin.content.findName("vid").width = "242";
        plugin.content.findName("vid")["Canvas.Left"] = "43";
        plugin.content.findName("setFullScreen").visibility = "Visible";
        plugin.content.findName("closeBtn").visibility = "Visible";
    } else {
    updateLayout(plugin.content.actualWidth, plugin.content.actualHeight);
    }
}
function updateLayout(width, height) {
    plugin.content.findName("BufferingStatusCanvas")["Canvas.Left"] = (width / 2) - plugin.content.findName("BufferingStatusCanvas").width / 2;
    plugin.content.findName("BufferingStatusCanvas")["Canvas.Top"] = 20;
    plugin.content.findName("Page").height = height;
    plugin.content.findName("Page").width = width;
    plugin.content.findName("vid").height = height;
    plugin.content.findName("vid").width = width;
    plugin.content.findName("vid")["Canvas.Left"] = "0";
    plugin.content.findName("setFullScreen").visibility = "Collapsed";
    plugin.content.findName("closeBtn").visibility = "Collapsed";
}