////////////////////////////////////////////////////////////////////////////////
// Speed test script
////////////////////////////////////////////////////////////////////////////////
// By: Serge Rivest <serge.rivest@cnet.com.au>
// Date: 2004-09-27
////////////////////////////////////////////////////////////////////////////////
//
// This script use an hidden img element to load a certain amount of data
// by changing its src 'on the fly'. If the download is too fast, it performs
// another download with a bigger chunk of data.
//
///////////////////////////////////////////////////////////////////////////////

var CHUNK_SIZE_SMALL  = 0;
var CHUNK_SIZE_MEDIUM = 1;
var CHUNK_SIZE_LARGE  = 2;
var dataSrc  = new Array('data_small.jpg', 'data_medium.jpg', 'data_large.jpg'); // Data image chunks
var dataSize = new Array(102,501,977); // Corresponding image size in kiloBytes
var testChunkSize; // Current test chunk size, automatically set by test_start(), used as index for dataSrc and dataSize
var timerStatus = 'stopped'; // Guess? Timer status ;)
var timerStartVal, timerStopVal; // Values when we start and stop the timer
var URL = 'http://www.cnet.co.uk/i/c/bst/';

// Animated images preload

var tmp = new Image;
tmp.src = URL + "network_active.gif?";
tmp.src = URL + "network_inactive.gif?";

//
// onLoad event of the image, triggered when the data img has finished loading.
//
function img_onLoad()
{
	// Is the test running ?
	if(timerStatus == 'started')
	{
		timer_stop();

		// Calculate the length in sec
		var length_sec = (timerStopVal - timerStartVal) / 1000;
		
		// Verify if the test was too fast
		if(length_sec == 0)
		{
			result_show(0); // Mmm.. strange.. cache problem maybe.
		} 
		else if((testChunkSize == CHUNK_SIZE_SMALL) && (length_sec <= 3))
		{
			test_start(CHUNK_SIZE_LARGE);
		} 
		else if((testChunkSize == CHUNK_SIZE_SMALL) && (length_sec <= 8))
		{
			test_start(CHUNK_SIZE_MEDIUM);
		} else {
			// Display the result
			result_show(length_sec);
		}
	}
}

//
// result_show: Shows the result of the test on the webpage.
//
function result_show(length_sec)
{
	// Calculate the kilobits per second rate
	var size = dataSize[testChunkSize] * 8;
	var kbps = Math.round(size / length_sec);
	var mykbps = document.getElementById('mykbps');
	var myspeed = document.getElementById('myspeed');
	var button = document.getElementById('mybutton');
	var statustext = document.getElementById('statustext');
	var statusgif  = document.getElementById('statusgif');
	
	if(kbps == 0)
	{
		mykbps.innerHTML = "Undef";
	} else {
		mykbps.innerHTML = kbps;
		bar_width = kbps / 26;

		if (bar_width > 470) {
			myspeed.width=467;
		} else {
			myspeed.width = bar_width;
		}

	}

	myspeed.style.display='inline';

	// Reset the interface

	button.disabled = false;
	//statustext.innerHTML = size + ' kbits downloaded in ' + length_sec + ' sec';
	statustext.innerHTML = "";
	statusgif.src = URL + 'network_inactive.gif?';
}

//
// timer_start: Starts the timer.
//
function timer_start()
{
	var myDate = new Date();
	timerStartVal = myDate.getTime();
	timerStatus = 'started';
}

//
// timer_stop: Stops the timer.
//
function timer_stop()
{
	var myDate = new Date();
	timerStopVal = myDate.getTime();
	timerStatus = 'stopped';
}

//
// test_start Starts the test with a specified chunk size.
//
function test_start(chunkSize)
{
	var dataimg = document.getElementById('dataimg');

	// Set the global chunk size
	testChunkSize = chunkSize;	

	// Interface initialisation only on the first download
	if(chunkSize == CHUNK_SIZE_SMALL) interface_init();

	// Start the timer
	timer_start();

	// Start downloading the data without caching..
	dataimg.src = URL + dataSrc[testChunkSize] + '?' + Math.random(); 
}

//
// interface_init: Initialize the interface of the download test
//
function interface_init()
{
	var button = document.getElementById('mybutton');
	var statustext = document.getElementById('statustext');
	var statusgif = document.getElementById('statusgif');
	var myspeed = document.getElementById('myspeed');
	var mykbps = document.getElementById('mykbps');

	button.disabled = true;
	myspeed.style.display='none';
	statustext.innerHTML = 'Testing, please wait ...';
	statusgif.src = URL + 'network_active.gif?';
	mykbps.innerHTML = '?';

}
