﻿var _startedAt=null;var _finishedAt=null;var _chunkSize=0;var _chunkCount=0;var _results=[];var _downloadedCount=0;String.prototype.format=function(){var s=this;for(var i=0;i<arguments.length;i++){var re=new RegExp('\\{'+(i)+'\\}','gm');s=s.replace(re,arguments[i].toString());}
return s;}
var $=function(selector){return document.querySelector(selector);}
var $debug=function(msg){window.console.log(msg);}
var $dispatch=function(code,delay){return window.setTimeout(code,delay);}
var $repeat=function(code,delay){return window.setInterval(code,delay);}
var $cancel=function(id){window.clearInterval(id);window.clearTimeout(id);}
var $create=function(tag){return document.createElement(tag);}
var $createText=function(text){return document.createTextNode(text);}
function harvestOptionValues(){_chunkCount=parseInt($("#selChunkCount").options[$("#selChunkCount").selectedIndex].value);_chunkSize=parseInt($("#selChunkSize").options[$("#selChunkSize").selectedIndex].value);$debug("Options saved: _chunkCount = {0}, _chunkSize = {1}".format(_chunkCount,_chunkSize));}
function downloadComplete(request,mode){if(request.readyState!=4)return;_finishedAt=new Date();$debug("Finished downloading chunk from server!");var isLatency=(mode=="latency");_downloadedCount++;_results.push(_finishedAt.getTime()-_startedAt.getTime());if(_downloadedCount<_chunkCount){if(isLatency){var s=$create("span");$("#speedTest .latency").appendChild(s).appendChild($createText("."));}
else{var lastTime=_results[_downloadedCount-1];var mbps=(_chunkSize/(lastTime/1000))/1024;var rotate=45*mbps;rotate=rotate>180?180:rotate;$("#speedTest .needle").style.webkitTransform="rotate({0}deg)".format(rotate);}
download(mode);}
else{var totalTime=0;var min=0;var max=0;var r,i;if(isLatency){for(i=0;i<_results.length;i++){r=_results[i];totalTime+=r;max=r>max?r:max;min=r<min||min==0?r:min;$debug("Latency result: {0}ms".format(r));}
$debug("Latency total: {0}ms".format(totalTime));$debug("Latency results count: {0}".format(_results.length));$debug("Latency results min/max: {0}/{1}".format(min,max));$debug("Latency result: {0}ms".format(totalTime/(_results.length-2)));$debug("Latency result (rounded): {0}ms".format(Math.round(totalTime/(_results.length-2))));totalTime=totalTime-max-min;var latencyResult="{0}ms".format(Math.round(totalTime/(_results.length-2)));var s=$create("span");s.className="result";$("#speedTest .latency").appendChild(s).appendChild($createText(latencyResult));s.style.opacity=1;s.style.webkitTransform="scale(1)";$dispatch(runSpeedTest,1000);}
else{for(i=0;i<_results.length;i++){r=_results[i];totalTime+=r;max=r>max?r:max;min=r<min||min==0?r:min;$debug("Download result: {0}ms".format(r));}
totalTime=totalTime-max-min;var mbps=((_chunkSize*(_chunkCount-2))/(totalTime/1000))/1024;$debug("Total download test time: {0}ms".format(totalTime));$debug("Download test rate: {0}Mbps".format(mbps));var rotate=45*mbps;rotate=rotate>180?180:rotate;var rate=(mbps>=1)?mbps:mbps*1024;var unit=(mbps>=1?"Mbps":"Kbps");$dispatch(function(){$("#speedTest .needle").style.webkitTransform="rotate({0}deg)".format(rotate);$("#speedTest .download .message").innerText="Speed Check finished!";$("#speedTest .download .result").innerText="{0} {1}".format(rate.toFixed(2),unit);$("#speedTest .download .result").style.opacity=1;$("#speedTest .download .result").style.webkitTransform="scale(1)";$("#speedTest .download .dial").style.opacity=0.2;$("#speedTest .download .needle").style.opacity=0.2;$("#speedTest .download .ad").style.opacity=1;},750)}}}
function download(mode){var isLatency=(mode=="latency");var url="Chunks/Data{0}{1}b.txt?{2}".format(isLatency?"10":_chunkSize,isLatency?"":"K",Math.random().toString().replace(".",""));if(!isLatency){$("#speedTest .download .message").innerText="Testing download... {0}/{1}".format(_downloadedCount+1,_chunkCount);if(_downloadedCount==0)$("#speedTest .download .message").style.opacity=1;}
var wr=new XMLHttpRequest();wr.onreadystatechange=function(){downloadComplete(wr,mode);};wr.open('GET',url,true);_startedAt=new Date();wr.send(null);$debug("Started downloading chunk from server: "+url);}
function showOptions(){$("#blackOut").style.left=0;$("#blackOut").style.opacity=0.4;$("#options").style.webkitTransform="translate(0, 0)";}
function saveOptions(){harvestOptionValues();$("#options").style.webkitTransform=null;$("#blackOut").style.opacity=null;$dispatch(function(){$("#blackOut").style.left=null},350);}
function runSpeedTest(){$debug("Running speed test...");$("#speedTest .download").style.opacity=1;$("#speedTest .download").style.webkitTransform="scale(1)";harvestOptionValues();_downloadedCount=0;_results=[];$dispatch(function(){download("speed")},350);}
function runLatencyTest(){$debug("Running latency test...");$("#speedTest .latency").style.opacity=1;_chunkCount=10;_chunkSize=10;download("latency");}
function showSpeedCheck(){$("#home").style.opacity=0;$("#home").style.webkitTransform="scale(.5)";$("#speedTest").style.opacity=1;$("#speedTest").style.webkitTransform="scale(1)";$dispatch(runLatencyTest,500);}