// Uploadprogress
var maxwidth;
var upid;
var bold;
var told;
var refresh = 8;
// Uploadfields
var maxfields = 5;
var fcount;
var uptpl;
var done = false;

$q(document).ready(function(){
	// Uploadfields
	//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
	$q('#uploadmore').click(function(){
		if (fcount < maxfields){
			addfield();
		}
	});
	// Uploadprogress
	//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
	$q('#btn_upload').click(function(sender){
		done = false;
		upid = uniqid();
		rstartprogress();
	});
});

// Uploadfields
//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
function fieldinit(){
	$q('.uploadfieldrow:gt(0)').remove();
	uptpl = $q('.uploadfieldrow:first').clone();
	var dbg = uptpl.find('input')[0];
	dbg.value = "";
	updatefieldcount();
}
function updatefieldcount(){
	fcount = $q('.uploadfieldrow').size();
	if (fcount < maxfields){
		$q('#uploadmore:hidden').show();
	}else{
		$q('#uploadmore:visible').hide();
	}
}
function addfield(){
	uptpl.clone().insertAfter('.uploadfieldrow:last');
	$q('.uploadfieldrow:last').append('<div class="button btn_remove"></div>');
	
	$q('.btn_remove').click(function(){
		$q(this).parent().remove();
		updatefieldcount();
	});
	
	updatefieldcount();
}
// Uploadprogress
//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
function rstartprogress(){
	$q('.uploadcontrolls').hide();
	$q('.uploadprogress').fadeIn("slow");
	maxwidth = $q('.uploadprogress .barframe').css('width');
	maxwidth = maxwidth.replace('px','');
	$q('.uploadprogress .bar').width(0);
	bold = 0;
	//Firefox Bugfix
	$q('.uploadprogress .barframe').css('background-image',$q('.uploadprogress .barframe').css('background-image'));
	$q('.uploadprogress .bar').css('background-image',$q('.uploadprogress .bar').css('background-image'));
	rstartupload();
	setTimeout("rfetchstats()",refresh * 100);
}
function rstartupload(){
	var cb_agb = 'false';
	if ($q('#cb_agb').attr("checked")){ cb_agb = 'true'; }
	var txt_email = $q('#txt_email').val();
	var txt_email_t = $q('#txt_email_t').val();
	var txt_files = new Array();
	$q('.ruploadfield').each(function(){
		txt_files.push($q(this).val());
	});
	$q.post(siteurl + "/doremoteupload.php",
		{ 
			id: upid, 
			rand: Math.random(),
			cb_agb: cb_agb,
			txt_email: txt_email,
			txt_email_t: txt_email_t,
			'txt_files[]' : txt_files
		},
		function(ret){
			if ($q('msg',ret).text() == 'ok'){
				done = true;
				$q('file',ret).each(function(){
					$q('.uploadcomplete .filerow:last').clone().appendTo('.uploadcomplete');
					$q('.uploadcomplete .filerow:last').toggleClass("altcolor");
					$q('.uploadcomplete .filerow:last .up_filename').text(unescape($q(this).text()));
					var alink = siteurl + '/get/' + $q(this).attr("id") + '/' + $q(this).attr("ahash");
					$q('.uploadcomplete .filerow:last .up_alink').text(alink);
					$q('.uploadcomplete .filerow:last .up_alink').attr('href',alink);
					var dellink = siteurl + '/del/' + $q(this).attr("id") + '/' + $q(this).attr("delhash");
					$q('.uploadcomplete .filerow:last .up_dellink').text(dellink);
					$q('.uploadcomplete .filerow:last .up_dellink').attr('href',dellink);
				});
				$q('.uploadcomplete .filerow:first').remove();
				$q('.uploadprogress').hide()
				$q('.uploadcomplete').fadeIn("slow");
			}else{
				$q('.uploadcontrolls').fadeIn("slow");
				$q('.uploadprogress').hide();
				alert(unescape(ret));
			}
		}
	);
}
function rfetchstats(){
	if (done == true){ return; }
	$q.post(siteurl + "/getrprogress.php",
		{ id: upid },
		function(xml){
			var percent = $q('percent',xml).text();
			var totalbyte = $q('totalbyte',xml).text();
			var aktbyte = $q('aktbyte',xml).text();
			var akttime = $q('time',xml).text();
			var aktfile = $q('aktfile',xml).text();
			if (aktfile.length > 57){
				aktfile = aktfile.substr(0,55) + "..";
			}
			var filenum = $q('filenum',xml).text();
			var maxfiles = $q('maxfiles',xml).text();
			var debug = $q('debug',xml).text()
			var neww = Math.round(maxwidth / 100  * percent);
			var dtime = (akttime - told) * 1000;
			var kbs = round((aktbyte - bold) / dtime * 1000 / 1024);
			bold = aktbyte;
			told = akttime;
			$q('.uploadprogress .aktfile').text(unescape(aktfile))
			$q('.uploadprogress .filenum').html(filenum)
			$q('.uploadprogress .maxfiles').html(maxfiles)
			$q('.uploadprogress .percent').html(percent+"%");
			if (kbs > 0){
				$q('.uploadprogress .speed').html(kbs);
			}
			$q('.uploadprogress .transfered').html(round(aktbyte / 1024 / 1024));
			$q('.uploadprogress .size').html(round(totalbyte / 1024 / 1024));
			$q('.uploadprogress .bar').animate({width: neww}, 'slow');
			setTimeout("rfetchstats()",refresh * 100);
		}
	);
}
function round(x) {
	var k = (Math.round(x * 100) / 100).toString();
	k += (k.indexOf('.') == -1)? '.00' : '00';
	return k.substring(0, k.indexOf('.') + 3);
}
function uniqid(){
	var now = new Date();
	return(now.getTime() + Math.floor(Math.random()*100+1));
}
