﻿/* VERSION 3 */

var contentURI = ""; //Localvariable for URL to playback after clienttest
var assetId = "";
var samiUri = "";
var wmpId = -1; //Stores wmpid used in wmpobjekt
var MaxBandWidth = 3000;
var MinBandWidth = 0;
var trailer = false;
var BitRateTestLocation = "rtmp://seli.le.tsicmds.com/vod";
var _actionsArray = null;
var _wait = null;
var _streamLocations = null;

function checkWaitToolbox(func) {
    if (toolbox.loaded) return true;
    if (_wait && _wait.waiting && _wait.func == func) {
        return false;
    }
    if (_wait && _wait.waiting) {
        window.clearTimeout(_wait.ID);
        _wait = null;
    }
    _wait = new object();
    _wait.func = func;
    _wait.waiting = true;
    _wait.ID = window.setTimeout(endWait, 333);
}

function endWait() {
    if (!_wait) return;
    var func = _wait.func;
    _wait = null;
    if (func) func();
}


function playTrailer() {
    if (!_streamLocations || _streamLocations.length == 0) {
        return;
    }

    MaxBandWidth = MP_MAXBANDWIDTH;
    MinBandWidth = MP_MINTRAILERBANDWITH;
    
    contentURI = _streamLocations[0].URI;

    var callback = handleCheckBandWidthResult;
        
    try 
    {
        verify.checkBandWidth(BitRateTestLocation, callback, true);
        
    }catch(err) 
    {
        setTimeout('playTrailer()', 300);
    }
}

function playMovie() {
    if (!_streamLocations || _streamLocations.length == 0) {
        return;
    }
    MaxBandWidth = MP_MAXBANDWIDTH;
    MinBandWidth = MP_MINBANDWIDTH;

    contentURI = _streamLocations[0].URI;

    var callback = handleCheckBandWidthResult;
    
    try 
    {
        verify.checkBandWidth(BitRateTestLocation, callback, true);
        
    }catch(err) 
    {
      setTimeout('playMovie()', 300);
    }
}

function PerformTest() {

    var requiredVersion = "3.0.40624.0";

    var silverlightInstalled = Silverlight.isInstalled(requiredVersion);

    if (!silverlightInstalled) {
        
         _actionsArray = new Array();

        _actionsArray.push({ name: 'sil', type: '', result: 0, text: { title: silverlightText, text: silverlightHelpText }, getActionText: function() { return new object(); } });
        Element.show('PageMask');
        Element.show('PageDialog');
        renderErrors(_actionsArray);
        return;
    }

    if (trailer) 
    {
        playTrailer();
    }
    else
    {
        playMovie();
    }
}

function handleCheckBandWidthResult(maxBandWidth) {
    MaxBandWidth = maxBandWidth;

    selectBitRateAndPlay();
}

function renderErrors(actionsArray) {
    var text = "";
    var actionToDisplay = -1;
    for (i = 0; i < actionsArray.length; i++) {
        var action = actionsArray[i];
        if (action.result == 0) {
            text += '<div class="TestingIconRed" onclick="showItem(' + i + ');">' + (i + 1) + '</div>';
            if (actionToDisplay < 0) actionToDisplay = i;
        }
        else if (action.result > 0) {
            text += '<div class="TestingIconGreen">' + (i + 1) + '</div>';
        }
    }
    Element.update('verify_result', text);
    _actionsArray = actionsArray;
    if (actionToDisplay >= 0) showItem(actionToDisplay);
    
    InitFlash();
}

function showItem(n) {
    if (!_actionsArray || _actionsArray.length <= n) {
        return;
    }
    var action = _actionsArray[n];
    var bodyText = "";
    if (TEXTS[action.name]) {
        Element.update('currentFaultHeader', TEXTS[action.name].heading);
        bodyText = TEXTS[action.name].body;
    }
    else {
        Element.update('currentFaultHeader', action.text.title);
        bodyText = action.text.text;
    }
    if (action.type == "software") {
        bodyText += "<br /><br /><a class='Red' href='" + action.source + "' target='_new'>" + actions.getActionText("software").title + "</a><br/>";
    }
    else if (action.type == "script") {
        bodyText += "<br /><br /><a class='Red' href='javascript:" + action.source + "();'>" + actions.getActionText("script").title + "</a><br/>";
    }
    Element.update('currentFaultText', bodyText);
}

function selectBitRateAndPlay() {
    var bestBitRate = 0;
    var worstBitRate = 0;
    var worstContentUri = "";
    var worstStreamLocation = -1;

    // alert('MaxBandWith: ' + MaxBandWidth);

    for (var i = 0; i < _streamLocations.length; i++) {
        var adjustedBitrate = _streamLocations[i].BitRate;
        if (MP_GETMARGIN) adjustedBitrate = MP_GETMARGIN(adjustedBitrate);

        // alert('_streamLocations I:' + i + " Bitrate: " + _streamLocations[i].BitRate);
        // alert('MP_STREAMLOCAT_streamLocationsION I:' + i +  " Adjusted bitrate: " + adjustedBitrate);

        if (_streamLocations[i].BitRate > bestBitRate && (adjustedBitrate < MaxBandWidth)) {
            // alert("new best bitate I :" + i);
            bestBitRate = _streamLocations[i].BitRate;
            contentURI = _streamLocations[i].URI;
            assetId = _streamLocations[i].AssetId;
            samiUri = _streamLocations[i].SamiUri;
        }
        if (i == 0 || _streamLocations[i].BitRate < worstBitRate) {
            // alert("new worst bitate I :" + i);
            worstContentURI = _streamLocations[i].URI;
            worstBitRate = _streamLocations[i].BitRate;
            worstStreamLocation = i;
        }
    }

    // alert('best bit rate: ' + bestBitRate);

    //if it is a trailer and the client maxbandwith where lower than the worst trailer bitrate, take that one anyway.
    // If we are playing a bough content and we get over the worstBitRate we should be allowed to play the content.
    if (trailer && bestBitRate == 0) {
        contentURI = worstContentURI;
    }
    else if (bestBitRate == 0 && MaxBandWidth >= worstBitRate && worstStreamLocation != -1) {
        contentURI = _streamLocations[worstStreamLocation].URI;
        assetId = _streamLocations[worstStreamLocation].AssetId;
        samiUri = _streamLocations[worstStreamLocation].SamiUri;
    }

    startPlayContent();
}

function switchToTrailer() {
    _streamLocations = MP_TRAILERSTREAMLOCATIONS;
    trailer = true;
    PerformTest();
}

function switchToMovie() {
    _streamLocations = MP_STREAMLOCATIONS;
    trailer = false;
    PerformTest();
}
