﻿
if (!window.overlayVid)
    overlayVid = {};

overlayVid.Page = function() {
}

overlayVid.Page.prototype =
{
    handleLoad: function(control, userContext, rootElement) {
        this.control = control;
        this.plugin;
        globalVars.set("callOverlayVidPlay", Silverlight.createDelegate(this, this.handleVidPlay));
        globalVars.set("callOverlayVidPause", Silverlight.createDelegate(this, this.handleVidPause));
        globalVars.set("callOverlayVidStop", Silverlight.createDelegate(this, this.handleVidStop));
        rootElement.findName("playBtn").addEventListener("MouseLeftButtonDown", Silverlight.createDelegate(this, this.handleVidPlay));

        rootElement.findName("vidMedia").addEventListener("DownloadProgressChanged", Silverlight.createDelegate(this, this.handleVidCurrentStateChanged));
        rootElement.findName("vidMedia").addEventListener("MediaOpened", Silverlight.createDelegate(this, this.handleVidLoaded));
        rootElement.findName("vidMedia").addEventListener("MediaEnded", Silverlight.createDelegate(this, this.handleMediaEnded));
        rootElement.findName("vidMedia").addEventListener("CurrentStateChanged", Silverlight.createDelegate(this, this.handleStateChanged));
        rootElement.findName("vidMedia").addEventListener("BufferingProgressChanged", Silverlight.createDelegate(this, this.handleBufferingProgressChanged));
        rootElement.findName("fullscreenBtn").addEventListener("MouseLeftButtonDown", Silverlight.createDelegate(this, this.handleSetFullscreen));
        rootElement.findName("soundBtn").addEventListener("MouseLeftButtonDown", Silverlight.createDelegate(this, this.handleVidSoundMute));
        this.totalDuration = "";

        rootElement.findName("vidMedia").Volume = 100;
    },
    handleVidLoaded: function(sender, eventArgs) {

        this.totalDuration = customConvertToTimeSpan(sender.findName("vidMedia").getValue("NaturalDuration").seconds);
        this.totalDurationSeconds = sender.findName("vidMedia").getValue("NaturalDuration").seconds;
        this.watchedStatus = 0;
        this.watched25 = false;
        this.watched50 = false;
        this.watched75 = false;
    },
    handleMediaEnded: function(sender, eventArgs) {
        for (i = 1; i < globalVars.get("NumberOfVideos") + 1; i++) {
            if (globalVars.get("vid" + i + "UrlSl") == sender.findName("vidMedia").Source) {
                globalVars.set("activeVidId", "vid" + i)
            }
        }
        new wtTrack(globalVars.get(globalVars.get("activeVidId") + "UrlSl") + globalVars.get("slVideoUrlSuffix100"), globalVars.get("slVideo100WtTitlePrefix") + globalVars.get(globalVars.get("activeVidId") + "Title"), globalVars.get("WebtrendsChannel"), 7);
        Effect.Pulsate('overlayMoreLink', { pulses: 2, duration: 1, from: 0 });
        this.handleVidPause();
    },
    handleVidCurrentStateChanged: function() {
        sender = document.getElementById("SilverlightControlOverlay").content;
        var media = sender.findName("vidMedia");
        sender.findName("vidTime").Foreground = "#FF9c9d9f";
        sender.findName("vidTime").FontSize = 10;
        sender.findName("vidTime").Text = customConvertToTimeSpan(sender.findName("vidMedia").getValue("Position").seconds) + "/" + this.totalDuration;
        if ((sender.findName("vidMedia").getValue("Position").seconds / this.totalDurationSeconds) > 0.25 && this.watchedStatus == 0) {
            new wtTrack(globalVars.get(globalVars.get("activeVidId") + "UrlSl") + globalVars.get("slVideoUrlSuffix25"), globalVars.get("slVideo25WtTitlePrefix") + globalVars.get(globalVars.get("activeVidId") + "Title"), globalVars.get("WebtrendsChannel"), 7);
            this.watchedStatus = 1;
        }
        if ((sender.findName("vidMedia").getValue("Position").seconds / this.totalDurationSeconds) > 0.50 && this.watchedStatus == 1) {
            new wtTrack(globalVars.get(globalVars.get("activeVidId") + "UrlSl") + globalVars.get("slVideoUrlSuffix50"), globalVars.get("slVideo50WtTitlePrefix") + globalVars.get(globalVars.get("activeVidId") + "Title"), globalVars.get("WebtrendsChannel"), 7);
            this.watchedStatus = 2;
        }
        if ((sender.findName("vidMedia").getValue("Position").seconds / this.totalDurationSeconds) > 0.75 && this.watchedStatus == 2) {
            new wtTrack(globalVars.get(globalVars.get("activeVidId") + "UrlSl") + globalVars.get("slVideoUrlSuffix75"), globalVars.get("slVideo75WtTitlePrefix") + globalVars.get(globalVars.get("activeVidId") + "Title"), globalVars.get("WebtrendsChannel"), 7);
            this.watchedStatus = 3;
        }
        var tmpPositionProgress = sender.findName("vidMedia").getValue("Position").seconds / sender.findName("vidMedia").getValue("NaturalDuration").seconds;
        sender.findName("positionTimeline").width = 225 * tmpPositionProgress;
    },
    handleVidPlay: function(sender, eventArgs) {
        this.control.content.findName("vidMedia").Autoplay = true;
        if (this.control.content.findName("vidMedia").getValue("Position").seconds == this.control.content.findName("vidMedia").getValue("NaturalDuration").seconds) {
            this.control.content.findName("vidMedia").Stop();
        }
        this.control.content.findName("vidMedia").Play();

        this.control.content.findName("playBtn").Source = "/germany/gradlinigerkurs/_global/component/overlayVid/assets/pauseBtn.png";
        this.control.content.findName("playBtn").addEventListener("MouseLeftButtonDown", Silverlight.createDelegate(this, this.handleVidPause));
        var dg = delegate(this, this.handleVidCurrentStateChanged);
        intervalID = window.setInterval(dg, 500);
    },
    handleVidPause: function(sender, eventArgs) {
        this.control.content.findName("vidMedia").Pause();
        this.control.content.findName("playBtn").Source = "/germany/gradlinigerkurs/_global/component/overlayVid/assets/playBtn.png";
        this.control.content.findName("playBtn").addEventListener("MouseLeftButtonDown", Silverlight.createDelegate(this, this.handleVidPlay));
        clearInterval(intervalID);
    },
    handleVidStop: function(sender, eventArgs) {
        this.control.content.findName("vidMedia").Stop();
        this.control.content.findName("playBtn").Source = "/germany/gradlinigerkurs/_global/component/overlayVid/assets/playBtn.png";
        this.control.content.findName("playBtn").addEventListener("MouseLeftButtonDown", Silverlight.createDelegate(this, this.handleVidPlay));
        clearInterval(intervalID);
    },
    handleVidSoundMute: function(sender, eventArgs) {
        sender.findName("soundBtn").Source = "/germany/gradlinigerkurs/_global/component/overlayVid/assets/soundMute.png";
        sender.findName("vidMedia").Volume = 0;
        sender.findName("soundBtn").addEventListener("MouseLeftButtonDown", Silverlight.createDelegate(this, this.handleVidSoundOn));
    },
    handleVidSoundOn: function(sender, eventArgs) {
        sender.findName("soundBtn").Source = "/germany/gradlinigerkurs/_global/component/overlayVid/assets/sound.png";
        sender.findName("vidMedia").Volume = 100;
        sender.findName("soundBtn").addEventListener("MouseLeftButtonDown", Silverlight.createDelegate(this, this.handleVidSoundMute));
    },
    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);
    },
    handleBufferingProgressChanged: function(sender, eventArgs) {

        sender.findName("bufferingStatusTxt").Text = "Buffering: " + String(Math.floor(sender.BufferingProgress * 100)) + "%";
    }
    , handleStateChanged: function(sender, eventArgs) {

    var media = sender.findName("vidMedia");
        if (media.CurrentState == "Buffering") {
            sender.findName("bufferingStatusTxt").Text = String(media.CurrentState);
            sender.findName("bufferingStatus").visibility = "Visible";

        } else if (media.CurrentState == "Opening") {
            sender.findName("bufferingStatusTxt").Text = "Öffnen";
            sender.findName("bufferingStatus").visibility = "Visible";
        }
        else {
            sender.findName("bufferingStatus").visibility = "Collapsed";
            sender.findName("bufferingStatusTxt").Text = "";
        }
    }
}


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 = "380";
        plugin.content.findName("Page").width = "450";
        plugin.content.findName("vidMedia").height = "343";
        plugin.content.findName("vidMedia").width = "450";
        plugin.content.findName("fullscreenBtn").visibility = "Visible";
        plugin.content.findName("playBtn").visibility = "Visible";
        plugin.content.findName("soundBtn").visibility = "Visible";
        plugin.content.findName("vidTime").visibility = "Visible";
        //plugin.content.findName("bufferingStatus").visibility = "Visible";
        plugin.content.findName("timelineCanvas").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("vidMedia").height = height;
    plugin.content.findName("vidMedia").width = width;
    plugin.content.findName("fullscreenBtn").visibility = "Collapsed";
    plugin.content.findName("playBtn").visibility = "Collapsed";
    plugin.content.findName("soundBtn").visibility = "Collapsed";
    plugin.content.findName("vidTime").visibility = "Collapsed";
    plugin.content.findName("bufferingStatus").visibility = "Collapsed";
    plugin.content.findName("timelineCanvas").visibility = "Collapsed";
}

function customConvertToTimeSpan(timeinseconds) {
    if (timeinseconds < 0) {
        return ("00:00");
    }
    else
        if (timeinseconds < 60) {
        return ("00:" + (Math.floor(timeinseconds)).toPaddedString(2));
    }
    else
        if (timeinseconds < 3600) {
        var mins = Math.floor(timeinseconds / 60);
        var seconds = Math.floor(timeinseconds - (mins * 60));
        return (mins + ":" + seconds.toPaddedString(2));
    }
}


function delegate(instance, method) {
    return function() {
        return method.apply(instance, arguments);
    }
}

function ConvertToTimeSpan(timeinseconds) {
    if (timeinseconds < 0) {
        return ("00:00:00");
    }
    else
        if (timeinseconds < 60) {
        alert(Math.floor(timeinseconds));
        return ("00:00:" + Math.floor(timeinseconds));
    }
    else
        if (timeinseconds < 3600) {
        var mins = Math.floor(timeinseconds / 60);
        var seconds = Math.floor(timeinseconds - (mins * 60));
        return ("00:" + mins + ":" + seconds);
    }
    else {
        var hrs = Math.floor(timeinseconds / 3600);
        var mins = timeinseconds - (hrs * 3600)
        var seconds = Math.floor(timeinseconds - (hrs * 3600) - (mins * 60));
        return (hrs + mins + ":" + seconds);
    }
}
function two(x) { return ((x > 9) ? "" : "0") + x }
function three(x) { return ((x > 99) ? "" : "0") + ((x > 9) ? "" : "0") + x }

function convertTime(ms) {
    var sec = Math.floor(ms / 1000)
    ms = ms % 1000
    t = three(ms)

    var min = Math.floor(sec / 60)
    sec = sec % 60
    //t = two(sec) + ":" + t
    t = two(sec)

    var hr = Math.floor(min / 60)
    min = min % 60
    t = two(min) + ":" + t

    var day = Math.floor(hr / 60)
    hr = hr % 60
    t = two(hr) + ":" + t
    t = day + ":" + t
    var tmpArray = t.split(":");
    var tmpOutput = "";
    //alert(tmpArray.length);
    for (var i = tmpArray.length - 1; i > -1; i--) {
        alert(tmpArray[i]);
        if (tmpArray[i] == 0 && tmpArray[i] == undefined) {

        } else {
            if (i != 0) {
                tmpOutput = tmpOutput + tmpArray[i] + ":";
            } else {
                tmpOutput = tmpOutput + tmpArray[i];
            }
        }
        //alert(i);
    }
    tmpOutput.split(":").reverse;

    alert(t);
    t = tmpOutput;
    return t
}

function timesimplejustminutesandseconds() {
    secVar0 = 480;                            // The initial data, in seconds
    minVar = Math.floor(secVar0 / 60);  // The minutes
    secVar = secVar0 % 60;              // The balance of seconds
    alert(minVar + ":" + secVar);
}
