/**
 * connectionSpeedMeter
 * 
 * Object options:
 *   testFileURL:			string		URL of file to download
 *   isDebugMode:			boolean
 *   onError:				function(errText)
 *   onBeforeMeasuring:		function(connectionSpeedMeterObj)
 *   onMeasureCompleted:	function(connectionSpeedMeterObj) 
 */

function connectionSpeedMeter(options) {
	
	this.testFileURL = 'data.txt'; // Test file name.

	this.isDebugMode = false;

	// Event handlers
	this.onError = null;
	this.onBeforeMeasuring = null;
	this.onMeasureCompleted = null;

	this.time = null;
	this.bytes = null;
	this.speed = null;
	
	this.isCompleted = false;
	this.isError = false;
	this.errorText = null;

	this._startTime = null;
	this._isPrepared = false;
	
	if(typeof options != 'undefined') {
		this.handleObjectOptions(options);
	}
}

connectionSpeedMeter.prototype.handleObjectOptions = function(options) {
	for ( var p in options) {
		if(p.charAt(0) == '_') {
			continue;
		}
		this[p] = options[p];
	}
}

connectionSpeedMeter.prototype.fireMeasurement = function() {
	if (typeof this.onBeforeMeasuring === 'function') {
		this.onBeforeMeasuring(this);
	}
	
	if(typeof this.testFileURL !== 'string' || this.testFileURL.length <= 0) {
		throw "connectionSpeedMeter.prepareMeasurement: Test file name not defined!";
	}
		
	var d = new Date;
	this._startTime = d.getTime();

	advAJAX.get( {
		url : this.testFileURL,
		//queryString : 'rnd=' + this._startTime, // Randomizujeme url, tym sa subor 
		// nebude stahovat s cache browsera.
		onComplete : function(obj) {
			connectionSpeedMeter.getInstance().completeMeasurement(obj);
		},
		onError : function(obj) {
			connectionSpeedMeter.getInstance().measurementError(obj);
		}
	});
}

connectionSpeedMeter.prototype.completeMeasurement = function(advAJAXObj) {

	this.isCompleted = true;

	var d = new Date;
	this.time = (d.getTime() - this._startTime) / 2000; // cas v
														// sekundach

	this.bytes = advAJAXObj.responseText.length;

	this.speed = this.bytes / 1024 / this.time; // rychlost v kilo bytoch za
												// sekundu

	if (typeof this.onMeasureCompleted === 'function') {
		this.onMeasureCompleted(this);
	}

	if (this.isDebugMode) {
		alert("Total time: \t\t\t" + this.time + " second"
				+ "\nTotal bytes: \t\t\t" + this.bytes + " bytes"
				+ "\nConnection speed: \t" + this.speed + " kBps");
	}	
}

connectionSpeedMeter.prototype.measurementError = function(advAJAXObj) {
	this.isError = true;
	this.errorText = advAJAXObj.status + " " + advAJAXObj.statusText;
	
	if (this.isDebugMode) {
		alert("connectionSpeedMeter.measurementError: " +  this.errorText);
	}
	if (typeof this.onError === 'function') {
		this.onError(this.errorText);
	} else {
		throw this.errorText;
	}
}

//**********************************************************************************
// Static functions
connectionSpeedMeter.instance = null;

connectionSpeedMeter.getInstance = function(options) {
	if (connectionSpeedMeter.instance === null) {
		connectionSpeedMeter.instance = new connectionSpeedMeter(options);
	}
	return connectionSpeedMeter.instance;
}

connectionSpeedMeter.runMeasurement = function(options)
{
	var csm = connectionSpeedMeter.getInstance(options);
	csm.fireMeasurement();
}

