/**
 */
function BytesUploaded(
		phpFile, 	// Contructor needs php or server filename to read informations
		latency 	// Milliseconds for each request during upload, default 1000, min value 50
	) { 
	
	/**
	 * Public method
         * 	Starts this application, set div or generic html id to write
         *      informations while uploading.
         *
         *      this.start( htmlid:String ):Boolean
         *
         * @Param	String		valid div, span or other page unique id to show information
         * @Return	Boolean		True to submit the form
	 */
	function start(htmlid) {
		__filemonitor.htmlid = htmlid;
		__fileloaderInterval = setTimeout(__readFileSize, 10);
		return true;
	}
	
	/** LIST OF ALL PRIVATE METHODS [ uncommented ] */
	function __fSize(size, dec) {
		if(!dec || dec < 0)
			dec = 2;
		var times = 0;
		var nsize = Number(size);
		var toEval = '';
		var type = Array( 'bytes', 'Kb', 'Mb', 'Gb', 'Tb', 'Zb' );
		while( nsize > 1024 ) {
			nsize = nsize / 1024;
			toEval += ' / 1024';
			times++;
		}
		if( times > 0 )
			eval( 'size = ( size' + toEval + ' );' );
		if(dec > 0) {
			var moltdiv = '(';
			while(dec > 0) {
				moltdiv += '10*';
				dec--;
			}
			moltdiv = moltdiv.substr(0, (moltdiv.length - 1)) + ')';
			eval( 'size = Math.round(size * ' + moltdiv + ') / ' + moltdiv + ';' );
		}
		return size + ' ' + type[times];
	}
	function __readFileSize() {
		__filemonitor.load(phpFile);
	}
	
	/** DECLARATION OF ALL PUBLIC METHODS */
	this.start = start;	// function to start this application
	
	/** PRIVATE VARIABLES */
	var __fileloaderInterval = 0;
	var __maybesomethingwrong = 0;
	var __filemonitor = new LoadVars();
	__filemonitor.onLoad = function(s) {
		var whatsup = '';
		if(s && this.filesize && this.filesize != 'undefined')
			whatsup = '<center><img src="/templates/olate/images/upload_anim.gif" /><font color="#FF8C00">Your file is still uploading. Currently uploaded ' + __fSize(this.filesize) + ' . Please wait.</center><br /><br /> Full res WAVs can take time to upload depending on your connection speed. Do not close the page or quit your browser until you receive confirmation that the file has been uploaded. We suggest that you get on with something else and check back in a while.</font><br /><img src="/templates/olate/images/SmileyFace.gif" />';
		else if(this.filesize && this.filesize == 'undefined') {
			whatsup = '<center>Waiting for succesful connection ...</center>';
			if(__maybesomethingwrong++ > 10) {
				__fileloaderInterval = 0;
				whatsup = 'Temp folder seems to be not valid.';
			}
		}
		else {
			whatsup = 'Unable to find server informations.';
			__fileloaderInterval = 0;
		}
		document.getElementById(this.htmlid).innerHTML = whatsup;
		delete this.filesize;
		if(__fileloaderInterval != 0)
			__fileloaderInterval = setTimeout(__readFileSize, latency);
	}
	if(!latency || latency < 50)
		latency = 1000;
}

