﻿/**
*	@class This class measures the user's bandwidth.
*	@author Erik 12/24/2008
*/
Sureflix.Vod.BandwidthTest = {

	resetTest: function() {
		if (this.element) {
			this.element.remove();
			this.element = null;
		}

		this.imageBytes = [285, 19957, 40393, 79328, 145092, 342006, 771376];
		this.avgBandwidth = 0;
		this.avgBandwidthCount = 0;
		this.maxBandwidthCount = 5;
		this.statusElement = null;
		this.progressElement = null;
		this.imageParentElement = null;
		this.progressWidth = null;
		this.imageSize = 0;
		this.startTime = null;
		this.latency = 0;
		this.latencyCount = 0;
	},


	/**
	* Starts the bandwidth test
	* @param {Element} HTML element (or its ID) where to display the progress bar
	* @param {Function} onComplete A function to call when the test is done ie: onComplete("OK", 1350)
	*/
	beginTest: function(parent, onComplete) {
		this.resetTest();
		this.parent = $(parent);
		this.onComplete = onComplete;

		//var dimensions = this.parent.getInnerDimensions();

		var html = this.getTestTemplate();

		//var myTemplate = new Sureflix.Utilities.Template(html);
		//var myValues = { width: dimensions.width,
		//	height: dimensions.height
		//};

		new Insertion.Bottom(this.parent, html /*myTemplate.evaluate(myValues)*/);
		this.element = $("bwtest");
		this.element.cloneInnerPosition(this.parent);
		var bg = this.element.getParentBackground();
		this.element.setStyle({ backgroundColor: bg });
		var progressBar = $("bwprogressbar");
		var margin = Math.floor((this.element.getHeight() - progressBar.getHeight()) / 2);
		progressBar.setStyle({ marginTop: margin + "px" });
		var margin = Math.floor((this.element.getWidth() - progressBar.getWidth()) / 2);
		progressBar.setStyle({ marginLeft: margin + "px" });
		this.updateProgress(Sureflix.Localization.getPhrase("Loading+") + "...");
		this.imageParentElement = $("bwimageparent");
		this.beginATest();
	},


	getTestTemplate: function() {
		var str = '' +
			'<style>' +
			'	#bwtest { cursor: pointer; position: absolute; overflow: hidden; padding: 0px; margin: 0px;}' +
			'	#bwteststatus { cursor: pointer; position: absolute; z-index: 2; text-align: center; width: 100%; color: Black; font-family: Verdana, sans-serif; font-size: 10px; padding-top: 3px; }' +
			'	#bwprogressbar { position: absolute; overflow: hidden; width: 90%; text-align: left; height: 20px; border: solid 1px black; background-color: White; }' +
			'	#bwprogress { position: absolute; left: 0px; top: 0px; width: 0px; height: 20px; z-index: 1; background-color: #EFCF42; }' +
			'	#bwimageparent { position: absolute; left: 0px; top: -1000px; }' +
			'</style>' +
			'<div id="bwtest" onclick="Sureflix.Vod.BandwidthTest.resetTest();">\n' +
			'	<div id="bwprogressbar"><div id="bwprogress"></div><div id="bwteststatus"></div></div>\n' +
			'	<div id="bwimageparent"></div>\n' +
			'</div>\n';
		return str;
	},

	beginATest: function() {
		this.startTime = (new Date()).getTime();
		var imageHTML = '<img src="http://sureflix.com/pc/images/BandwidthTest/TestImage' + this.imageSize + '.jpg?StartTime=' + this.startTime + '" width="10" height="10" onload="Sureflix.Vod.BandwidthTest.endATest()">';
		this.imageParentElement.innerHTML = imageHTML;
	},


	endATest: function() {
		if (this.avgBandwidthCount >= this.maxBandwidthCount) {
			var currentAverage = Math.round(this.avgBandwidth / this.avgBandwidthCount);
			this.resetTest();
			this.onComplete("OK", currentAverage);
			return;
		}

		var endTime = (new Date()).getTime();
		var elapsedTime = endTime - this.startTime;

		if (this.imageSize == 0) {
			//measuring latency; take the lowest of 5 attempts
			if (this.latency == 0 || this.latency > elapsedTime) {
				this.latency = elapsedTime;
			}
			SxEr.consoleLog("latency: " + this.latency);

			this.latencyCount++;
			if (this.latencyCount >= 5) {
				this.imageSize++;
			}
			this.beginATest();
		} else if (elapsedTime < 500) {
			//if it takes less than 500 milliseconds, increase the size of the test file
			this.imageSize++;
			this.beginATest();
		} else {
			var bandwidth = Math.round((this.imageBytes[this.imageSize]-this.imageBytes[0]) * 8 / ((elapsedTime - this.latency) / 1000) / 1024);
			this.avgBandwidth += bandwidth;
			this.avgBandwidthCount++;
			SxEr.consoleLog("image " + this.imageSize + ": " + this.imageBytes[this.imageSize] + " bytes in " + elapsedTime / 1000 + "s = " + bandwidth + "Kbps");

			this.updateProgress(Math.round(this.avgBandwidth / this.avgBandwidthCount) + " Kbps");
			this.beginATest();
		}
	},


	updateProgress: function(text) {
		if (!this.progressElement) {
			this.progressElement = $("bwprogress");
		}
		if (!this.progressWidth) {
			this.progressWidth = $("bwprogressbar").getInnerDimensions().width;
		}
		this.progressElement.sizeTo(Math.round(this.avgBandwidthCount / this.maxBandwidthCount * this.progressWidth), null);
		if (!this.statusElement) {
			this.statusElement = $("bwteststatus");
		}
		this.statusElement.innerHTML = text;
	}

}




