﻿var labelForLink = "Launch the broadcast";

/***************************************************************************/
/****                           DO NOT MODIFY AFTER                    *****/
/***************************************************************************/
var ua = navigator.userAgent.toLowerCase();
var brTyp = false;
if (ua.indexOf('safari/') != -1){
	brTyp = true;
}

function avsWritePlugin(bcid, bid, eid, avsBin, avsJs, mode) {
    if (mode == 1) {
        avsWritePluginFs(bcid, bid, eid, avsBin);
    } else {
        avsWritePluginNfs(bcid, bid, eid, avsBin, avsJs);
    }
}

function goToUrl(url) {
    window.open(url, url, config = "width=800, height=600, toolbar=yes, menubar=yes, scrollbars=yes, resizable=yes, location=yes, directories=yes, status=yes");
}

function test(bcid, bid, eid, avsBin) {
	alert(bcid+" - "+bid+" - "+eid+" - "+avsBin);
}

function avsWritePluginFs(bcid, bid, eid, avsBin) {
    var date = new Date();
    var timestamp = date.getTime();
    var url = avsBin + "/VPLive.xap?" + timestamp;
    var divPlugin = document.getElementById("avslivePlugIn");
    var slPlugin = document.getElementById("silverlightControlHost");

    var chaine = '<div id="silverlightControlHost" style="width: 180px; height: 50px; display: block; position: relative; top: 0px; left: 0px;"><object data="data:application/x-silverlight," id ="BDtest" type="application/x-silverlight-2" height="100%" width="100%"><param name="background" value="#00FFFFFF"/><param name="windowless" value="true"/><param name="source" value="' + url + '"/><param name="initParams" value="bcid=' + bcid + ',bid=' + bid + ',eid=' + eid + ',plgHeight=50,plgWidth=180,plgTop=0,plgLeft=0"/><a href="http://www.microsoft.com/silverlight/resources/install.aspx" style="text-decoration: none;"><img src="http://go.microsoft.com/fwlink/?LinkId=108181" alt="Get Microsoft Silverlight" style="border-style: none;"></a></object></div>';
    divPlugin.innerHTML = chaine;
}

function avsWritePluginNfs(bcid, bid, eid, avsBin, avsJs) {
    var avsBinPck = "'" + avsBin + "'";
    var avsJsPck = "'" + avsJs + "'";   
    var chaine;
    var divPlugin = document.getElementById("avslivePlugIn");
    if(brTyp){
    	chaine = '<a href = "javascript:openNfsBtnForS(' + bcid + ',' + bid + ',' + eid + ',' + avsBinPck + ',' + avsJsPck + ');" > ' + labelForLink + ' </a>';
    }
    else{
    	chaine = '<a href = "javascript:openNfsBtn(' + bcid + ',' + bid + ',' + eid + ',' + avsBinPck + ');" > ' + labelForLink + ' </a>';	
    }

	divPlugin.innerHTML = chaine;
}

function openNfsBtnForS(bcid, bid, eid, avsBin, avsJs) {
	var screenWdt = screen.availWidth;
    var screenHgt = screen.availHeight;
    
    var width = "180";
    var height = "80";
    var top = (screenHgt / 2) - (height / 2);
    var left = (screenWdt / 2) - (width / 2);

    var options = "menubar=no,scrollbars=no,statusbar=no,resizable=yes";
    
	var urlNfs = avsBin + '/nfs.html?bcid='+bcid+'&bid='+bid+'&eid='+eid+'&avsJs='+avsJs+'&avsBin='+avsBin;
	
	window.open(urlNfs, name, "top=" + top + ",left=" + left + ",width=" + width + ",height=" + height + "," + options);
}


function openNfsBtn(bcid, bid, eid, avsBin) {
	//alert("TEST 1: " + fileLocation);
    var date = new Date();
    var timestamp = date.getTime();
    var url = avsBin + "/VPLive.xap?" + timestamp;
    var name = "avsnfs";
    var screenWdt = screen.availWidth;
    var screenHgt = screen.availHeight;
    
    var width = "180";
    var height = "80";
    var top = (screenHgt / 2) - (height / 2);
    var left = (screenWdt / 2) - (width / 2);

    var options = "menubar=no,scrollbars=no,statusbar=no,resizable=yes";
    var chainePlugin = '<div id="silverlightControlHost" style="width: 180px; height: 50px; display: block; position: relative; top: 0px; left: 0px;"><object data="data:application/x-silverlight," id ="BDtest" type="application/x-silverlight-2" height="100%" width="100%"><param name="background" value="#00FFFFFF"/><param name="windowless" value="true"/><param name="source" value="' + url + '"/><param name="initParams" value="bcid=' + bcid + ',bid=' + bid + ',eid=' + eid + ',plgHeight=50,plgWidth=180,plgTop=0,plgLeft=0"/><a href="http://www.microsoft.com/silverlight/resources/install.aspx" style="text-decoration: none;"><img src="http://go.microsoft.com/fwlink/?LinkId=108181" alt="Get Microsoft Silverlight" style="border-style: none;"></a></object></div>';
    var drawCSImageTagStr = 'function drawCSImageTag(fileLocation, fileSize, imgTagProperties) { start = (new Date()).getTime(); loc = "http://avshttp.atdi.fr/flash/2044.jpg" + "?t=" + escape(start); document.getElementById("imgLoad").innerHTML = "<img src=\'" + loc + "\' " + imgTagProperties + " onload=\'computeConnectionSpeed(" + start + "," + fileSize + ");\'>";return;}';
    var computeConnectionSpeedStr = 'function computeConnectionSpeed(start, fileSize) {end = (new Date()).getTime();downloadtime = end - start;linespeed = fileSize / downloadtime;connectSpeed = (Math.round((linespeed * 8) * 10 * 1.02)) / 10;testSL(connectSpeed);}';
    var testSLStr = 'function testSL(connectSpeed) {var xaml = document.getElementById("BDtest");xaml.Content.Page.retBDW(connectSpeed);}';

    avsnfs = window.open('', name, "top=" + top + ",left=" + left + ",width=" + width + ",height=" + height + "," + options);
    avsnfs.document.open();
    
    avsnfs.document.write('<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">');
    avsnfs.document.write('<html>');
    avsnfs.document.write('<head>');
    avsnfs.document.write('<title></title>');
    avsnfs.document.write('<script language="javascript">');
    avsnfs.document.write('function resizeOnScreen(fsValue) {var slPlugin = document.getElementById("silverlightControlHost"); slPlugin.style.width = "100%";slPlugin.style.height = "100%";resizeFull();}');
    avsnfs.document.write('function resizeFull() {self.moveTo(0, 0);self.resizeTo(screen.availWidth, screen.availHeight);}');
    avsnfs.document.write('function resizeOffScreen(fsValue, height, width, top, left){self.close();}');
    avsnfs.document.write('function goToUrl(url){window.open(url, url, config="width=800, height=600, toolbar=yes, menubar=yes, scrollbars=yes, resizable=yes, location=yes, directories=yes, status=yes");}');
    avsnfs.document.write(drawCSImageTagStr);
    
    avsnfs.document.write(computeConnectionSpeedStr);
    avsnfs.document.write(testSLStr);
    avsnfs.document.write('</script>');
    avsnfs.document.write('</head>');
    avsnfs.document.write('<body BGCOLOR="#000000" TOPMARGIN=0 LEFTMARGIN=0 MARGINHEIGHT=0 MARGINWIDTH=0>');
    avsnfs.document.write('<center>');
    avsnfs.document.write(chainePlugin);
    avsnfs.document.write('<div id="imgLoad"></div>');
    avsnfs.document.write('</center>');
    avsnfs.document.write('</body>');
    avsnfs.document.write('</html>');
    
    avsnfs.document.close();
}


function resizeOnScreen(fsValue) {
    var slPlugin = document.getElementById("silverlightControlHost"); slPlugin.style.width = "100%";
    slPlugin.style.height = "100%";
	if(brTyp){
		resizeFull();
	}
}

function resizeOffScreen(fsValue, height, width, top, left) {
    var slPlugin = document.getElementById("silverlightControlHost"); slPlugin.style.width = width + "px";
    slPlugin.style.height = height + "px";
    var divPlugin = document.getElementById("avslivePlugIn");
}

function resizeFull() {
    self.moveTo(0, 0);
    self.resizeTo(screen.availWidth, screen.availHeight);
}

function relocatePlugins() {
    var divPlugin = document.getElementById("avslivePlugIn");
    var slPlugin = document.getElementById("silverlightControlHost");
    var offsetLeft = getLeft(divPlugin);
    var offsetTop = getTop(divPlugin);

    slPlugin.style.top = offsetTop;
    slPlugin.style.left = offsetLeft;
}

function getLeft(MyObject) {
    if (MyObject.offsetParent)
        return (MyObject.offsetLeft + getLeft(MyObject.offsetParent));
    else
        return (MyObject.offsetLeft);
}

function getTop(MyObject) {
    if (MyObject.offsetParent)
        return (MyObject.offsetTop + getTop(MyObject.offsetParent));
    else
        return (MyObject.offsetTop);
}

function onSilverlightError(sender, args) {
    var appSource = "";
    if (sender != null && sender != 0) {
        appSource = sender.getHost().Source;
    }
    var errorType = args.ErrorType;
    var iErrorCode = args.ErrorCode;
    var errMsg = "Unhandled Error in Silverlight 2 Application " + appSource + "\n";
    errMsg += "Code: " + iErrorCode + " \n";
    errMsg += "Category: " + errorType + " \n";
    errMsg += "Message: " + args.ErrorMessage + " \n";
    if (errorType == "ParserError") {
        errMsg += "File: " + args.xamlFile + " \n";
        errMsg += "Line: " + args.lineNumber + " \n";
        errMsg += "Position: " + args.charPosition + " \n";
    }
    else if (errorType == "RuntimeError") {
        if (args.lineNumber != 0) {
            errMsg += "Line: " + args.lineNumber + " \n";
            errMsg += "Position: " + args.charPosition + " \n";
        }
        errMsg += "MethodName: " + args.methodName + " \n";
    }
    throw new Error(errMsg);
}


connectionSpeed = 0;
// The variable where connection speed information
// will be stored when it is available.
function drawCSImageTag(fileLocation, fileSize, imgTagProperties) {
    // This function draws the image tag required to run this process.
    // It needs to be passed:
    //     1.  (String)   The location of the file to be loaded
    //     2.  (Integer)  The size of the image file in bytes
    //     3.  (String)   The tag properties to be included in the <img> tag
    // Place a call to this function inside the <body> of your file
    // in place of a static <img> tag.
	fileLocation = "http://avshttp.atdi.fr/flash/2044.jpg";
    start = (new Date()).getTime();
    // Record Start time of <img> load.

    loc = fileLocation + '?t=' + escape(start);
    // Append the Start time to the image url
    // to ensure the image is not in disk cache.
    document.getElementById('imgLoad').innerHTML = '<img src="' + loc + '" ' + imgTagProperties + ' onload="computeConnectionSpeed(' + start + ',' + fileSize + ');">';

    return;
}

function computeConnectionSpeed(start, fileSize) {
    // This function returns the speed in kbps of the user's connection,
    // based upon the loading of a single image.  It is called via onload 
    // by the image drawn by drawCSImageTag() and is not meant to be called
    // in any other way.  You shouldn't ever need to call it explicitly.

    end = (new Date()).getTime();
    //connectSpeed = (Math.floor((((fileSize * 8) / ((end - start) / 1000)) / 1024) * 10) / 10);
    downloadtime = end - start
    linespeed = fileSize / downloadtime;
    connectSpeed = (Math.round((linespeed * 8) * 10 * 1.02)) / 10;
    testSL(connectSpeed);
    return;
}

function testSL(connectSpeed) {
    var xaml = document.getElementById("BDtest");
    xaml.Content.Page.retBDW(connectSpeed);
}