
// Constructor for a test object
function Test (file, size, number)
{
    this.file = file;
    this.size = size;
    this.start = 0;
    this.stop = 0;
    this.speed = 0;
    this.image = new Image();
    return this;
}

// Create a test object to store results
var tests = new Array();
var search = (new Date()).getTime();
var szTotal = (3*12495) + (3*885296);

/* tests[0] = new Test("http://media.pennatlantic.com/lib/graphics/12495.jpg?"+(search), 12495, 1);
tests[1] = new Test("http://media.pennatlantic.com/lib/graphics/12495.jpg?",  12495,   1);
tests[2] = new Test("http://media.pennatlantic.com/lib/graphics/325212.jpg?", 325212,  2);
tests[3] = new Test("http://media.pennatlantic.com/lib/graphics/12495.jpg?",  12495,   3);
tests[4] = new Test("http://media.pennatlantic.com/lib/graphics/325212.jpg?", 325212,  4);
tests[5] = new Test("http://media.pennatlantic.com/lib/graphics/12495.jpg?",  12495,   5);
tests[6] = new Test("http://media.pennatlantic.com/lib/graphics/325212.jpg?", 325212,  6);
tests[7] = new Test("http://media.pennatlantic.com/lib/graphics/12495.jpg?",  12495,   7);
tests[8] = new Test("http://media.pennatlantic.com/lib/graphics/325212.jpg?", 325212,  8);
tests[9] = new Test("http://media.pennatlantic.com/lib/graphics/12495.jpg?",  12495,   9);
tests[10] = new Test("http://media.pennatlantic.com/lib/graphics/325212.jpg?", 325212, 10);
tests[11] = new Test("http://media.pennatlantic.com/lib/graphics/12495.jpg?",  12495,  11);
tests[12] = new Test("http://media.pennatlantic.com/lib/graphics/325212.jpg?", 325212, 12);
*/

tests[0] = new Test("http://media.pennatlantic.com/lib/graphics/12495.jpg?"+(search), 12495, 1);
tests[1] = new Test("http://media.pennatlantic.com/lib/graphics/12495.jpg?",  12495,   1);
tests[2] = new Test("http://media.pennatlantic.com/lib/graphics/885296.jpg?", 885296,  2);
tests[3] = new Test("http://media.pennatlantic.com/lib/graphics/12495.jpg?",  12495,   3);
tests[4] = new Test("http://media.pennatlantic.com/lib/graphics/885296.jpg?", 885296,  4);
tests[5] = new Test("http://media.pennatlantic.com/lib/graphics/12495.jpg?",  12495,   5);
tests[6] = new Test("http://media.pennatlantic.com/lib/graphics/885296.jpg?", 885296,  6);
tests[7] = new Test("http://media.pennatlantic.com/lib/graphics/12495.jpg?",  12495,   7);
tests[8] = new Test("http://media.pennatlantic.com/lib/graphics/885296.jpg?", 885296,  8);
tests[9] = new Test("http://media.pennatlantic.com/lib/graphics/12495.jpg?",  12495,   9);
tests[10] = new Test("http://media.pennatlantic.com/lib/graphics/885296.jpg?", 885296, 10);
tests[11] = new Test("http://media.pennatlantic.com/lib/graphics/12495.jpg?",  12495,  11);
tests[12] =new Test("http://media.pennatlantic.com/lib/graphics/885296.jpg?", 885296, 12);



// Counter for which test we're doing
var maxNum = 6;
var testNum = 0;
var sofar = 0;

function initTest() {
	testNum = 0;
	sofar = 0;
	startTest();
}
// Function to start a test
function startTest ()
{
    setIdHTML("bandwidth", "Calculating Bandwidth");
    
    var date = new Date();
    tests[testNum].start = date.getTime();
    tests[testNum].stop = 0;
    tests[testNum].image.onload = stopTest;
    tests[testNum].image.src = tests[testNum].file+(new Date()).getTime();
}

// Callback function to record the results
function stopTest ()
{
    var date = new Date();
    sofar += tests[testNum].size;
	prct = Math.round((sofar / szTotal) * 100);
	if (prct > 0) {
		setPct(prct);
	}
    tests[testNum].stop = date.getTime();
    tests[testNum].speed
	= Math.floor((1000 / 1024) * (8 * tests[testNum].size)
		     / (tests[testNum].stop - tests[testNum].start));
    
    setIdHTML("bandwidth", "Calculating Bandwidth <img border=0 src=\"/images/progress.gif\">");
    setIdHTML("size", (tests[testNum].size) + " Bytes");
    setIdHTML("time",
	      ((tests[testNum].stop - tests[testNum].start) / 1000)
	      + " Sec");

    //Status info
    setIdHTML("size-"+testNum, (tests[testNum].size) + " Bytes");
    setIdHTML("time-"+testNum, ((tests[testNum].stop - tests[testNum].start)
                                / 1000) + " Sec");
    
    // See if we should start another test
    if (testNum < maxNum) {
	testNum++;
	startTest();
    } else {
	calculateSpeed();
    }
}


function omitOutliers (init)
{
    // Set the initial minimum pair
    //min = Math.abs((tests[init].stop - tests[init].start)
    //               - (tests[init + 2].stop - tests[init + 2].start));
    //var t1 = init;
    //var t2 = init + 2;

    min = 999999;
    var t1 = 0;
    var t2 = 0;

    // Look for a closer pair
    for (i = init; i <= maxNum - 2; i += 2) {
	for (j = i + 2; j <= maxNum; j += 2) {
	    if (min > Math.abs((tests[i].stop - tests[i].start)
                               - (tests[j].stop - tests[j].start))) {
                min = Math.abs((tests[i].stop - tests[i].start)
                               - (tests[j].stop - tests[j].start));
                t1 = i;
                t2 = j;
            }
        }
    }

    // Replace the first pair with the minimum pair
    tests[init].start = tests[t1].start;
    tests[init].stop  = tests[t1].stop;
    tests[init].speed = tests[t1].speed;
    tests[init + 2].start = tests[t2].start;
    tests[init + 2].stop  = tests[t2].stop;
    tests[init + 2].speed = tests[t2].speed;

    setIdHTML("t"+init+"-1", t1);
    setIdHTML("t"+init+"-2", t2);
}

// Calculate the upload speed using y1 = m * x1 + b; y2 = m * x2 + b 
function calculateSpeed ()
{
    // The number of values to use in the average
    var maxVals = 4;

    // Look for the closest pair of measurements
    omitOutliers(1);
    omitOutliers(2);

    var x1 = 0;
    for (i = 1; i <= maxVals; i += 2) {
	x1 += tests[i].size;
    }
    x1 = 8 * x1 / (maxVals / 2);
    
    var y1 = 0;
    for (i = 1; i <= maxVals; i += 2) {
	y1 += tests[i].stop - tests[i].start;
    }
    y1 = y1 / (maxVals / 2);

    var x2 = 0;
    for (i = 2; i <= maxVals; i += 2) {
	x2 += tests[i].size;
    }
    x2 = 8 * x2 / (maxVals / 2);
    
    var y2 = 0;
    for (i = 2; i <= maxVals; i += 2) {
	y2 += tests[i].stop - tests[i].start;
    }
    y2 = y2 / (maxVals / 2);
    
    var m = (y2 - y1) / (x2 - x1);
    var b = ((y1 * x2) - (y2 * x1)) / (x2 - x1);

    var speed = Math.floor(1000 / (m * 1024));
    
    setIdHTML("status", "Complete ");
    if (tests[testNum].speed <600) {
		setIdHTML("bandwidth", "Your download speed is <B>"+tests[testNum].speed+" Kbps</B>, Penn Atlantic games are streamed at 650 Kbps, you may have trouble viewing games");
	} else {
		setIdHTML("bandwidth", tests[testNum].speed+" Kbps</B>");
	}
	if (setIdValue("bdwidth", tests[testNum].speed)) {
		setBW();
	}
	setIdHTML("size", (x1 / 8 + x2 / 8) * (maxVals / 2) + " Bytes");
    setIdHTML("time", (y1 + y2) * (maxVals / 2) / 1000 + " Sec");

    setIdHTML("x1", x1);
    setIdHTML("x2", x2);

    setIdHTML("y1", y1);
    setIdHTML("y2", y2);
}

// Set some results
function setIdHTML (id, html)
{
    if (document.getElementById(id)) {
		document.getElementById(id).innerHTML = html;
		return true;
    } else {
		return false;
	}
}

function setIdValue (id, txt)
{
	if (document.getElementById(id)) {
		document.getElementById(id).value = txt;
		return true;
    } else {
		return false;
	}
}
