
var pleaseWaitPrompt = "Changing Diabetes Barometer is determening the speed of your Internet connection"
var speedTestThreshold = 1000; // Threshold in miliseconds of when a test-file was downloaded too fast
var loadingIndicatorRate = 200;
var loadingIndicatorIntervalId
var lowBandwidthTimeoutId

function doConnectionTest() {
    // Setup loading prompt elements
    $('#content').html("<div class='speedTestPrompt'><div id='prompt'></div><br /><div id='loader'></div></div>");

    // Populate the promt and give a loading indicator
    $('#prompt').text(pleaseWaitPrompt);
    loadingIndicatorIntervalId = setInterval(updateLoadingIndicator, loadingIndicatorRate);

    // Go to low bandwidth site in given time if no redirect happens
    lowBandwidthTimeoutId = setTimeout(goToLowBandWidth, speedTestThreshold);

    $.get(
		'res/96.bin', {},
		function(data) {
			goToHighBandWidth();
		},
		'text'
	);
}

function doSpeedTest() {
    doConnectionTest();
}

function goToLowBandWidth() {
    clearInterval(loadingIndicatorIntervalId);
    clearTimeout(lowBandwidthTimeoutId);
	speedTestCompleted("slim");
}

function goToHighBandWidth() {
    clearInterval(loadingIndicatorIntervalId);
    clearTimeout(lowBandwidthTimeoutId);
    speedTestCompleted("full");
}

function updateLoadingIndicator() {
    var currentContent = $('#loader').text();
    var appendSnip = "";
    
    switch (currentContent) {
        case "":
            appendSnip = ".";
            break;
        case ".":
            appendSnip = ".."
            break;
        case "..":
            appendSnip = "..."
            break;
        default:
            break;
    }

    $('#loader').text(appendSnip);
}

$(document).ready(function() {
    doSpeedTest();
});