/**
 * ydrunspeed class
 * @description tested FF3
 * @subpackage prototype
 * @author basil suter
 * @date 28-04-2008
 * @version v1.0b
 */

// ydrunspeed class creation
var ydrunspeed = Class.create ({
	
	// init method
	initialize : function(file_size,container_div){
		this.file_size 		= file_size;
		this.container_div 	= container_div;
		this.time_start 	= new Date();
		// run at
		this.start();
	},
	
	// start grabing content length
	start : function(){
		new Ajax.Request('/cache.zip?'+new Date().getTime(), {
			method		: 'get',
			onSuccess : function(transport){
				this.lengther = (transport.getResponseHeader('content-length')/1024);
				// start success method
				this.success();
			}.bind(this),
			onLoading : function(){
				$(this.container_div).update('loading..');
			}
		});
	},
	
	// request success method - prepare values for calculation
	success : function(){
		this.time_end = new Date();
		time_needed = (this.time_end - this.time_start) / 1000;
		speed = this.lengther / time_needed;
		this.calc(speed.toFixed(1),this.file_size);
	},
	
	// calculate now the time
	calc : function(speed,size){
		kb = size * 1024;
		se = kb/speed;
		re = (se/60)+1;
    	res = Math.round(re);
    	this.get(res);
	},
	
	// write time in container_div
	get : function(result){
		$(this.container_div).update('~'+result+' min');
	}
	
});

// init class example:
/*
	<script type="text/javascript" language="javascript">
		var speedtest = new ydrunspeed(85,'calc_div');
	</script>
*/
