function calcBandwidth(duration, bytes) {
    // convert from msecs to secs
    var duration = duration / 1000;
    // convert from bytes to kbits
    var kbits = (bytes * 8) / 1024;
    // calculate bandwidth (incl. packet header overhead ~7%)
    var bandwidth = Math.floor(kbits / duration * .93);
    return bandwidth;
}

function detectBandwidth(uri, callback) 
{
    var bandwidth = 0;
    var startTime = new Date().getTime();
    jQuery.getJSON(uri + "?callback=?",
        function(msg) {
            var endTime = new Date().getTime();
            var bytes = msg;
            bandwidth = calcBandwidth((endTime - startTime), bytes);
            callback(bandwidth);
        }
    );
}
