// Uploadprogress
var maxwidth;
var date = new Date();
var upid;
var bold;
var refresh = 1;

$(document).ready(function(){
	// Uploadprogress
	//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
	$('#uploadform').submit(function(sender){
		upid = $('#uploadform input[@name=APC_UPLOAD_PROGRESS]').val();
		if (upid){
			if($('#txt_movieurl').val()){
				rstartprogress();
				sender.preventDefault();
			}else{
				startprogress();
			}
		}
		//sender.preventDefault();
	});
});


// Uploadprogress
//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
function startprogress(){
	$('.uploadcontrolls').hide();
	$('#uploadprogress').fadeIn("slow");
	maxwidth = $('#uploadprogress .barframe').css('width');
	maxwidth = maxwidth.replace('px','');
	$('#uploadprogress .bar').width(0);
	bold = 0;
	//Firefox Bugfix
	$('#uploadprogress .bar').css('background-image',$('#uploadprogress .bar').css('background-image'));
	setInterval("fetchstats()",refresh * 1000);
}
function fetchstats(){
	$.post(siteurl + "/getprogress.php?remote=0",
		{ id: upid, remote: '0', rand: Math.random() },
		function(xml){
			var percent = $('percent',xml).text();
			var totalbyte = $('totalbyte',xml).text();
			var aktbyte = $('aktbyte',xml).text();
			var debug = $('debug',xml).text()
			var neww = Math.round(maxwidth / 100  * percent);
			var kbs = round((aktbyte - bold) * (1 / refresh) / 1024);
			bold = aktbyte;
			$('#uploadprogress .speed').html(kbs);
			$('#uploadprogress .transfered').html(round(aktbyte / 1024 / 1024));
			$('#uploadprogress .size').html(round(totalbyte / 1024 / 1024));
			$('#uploadprogress .bar').animate({width: neww}, 'slow');
		}
	);
}
// Remoteuploadprogress
//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
function rstartprogress(){
	$('.uploadcontrolls').hide();
	$('#ruploadprogress').fadeIn("slow");
	maxwidth = $('#ruploadprogress .barframe').css('width');
	maxwidth = maxwidth.replace('px','');
	$('#ruploadprogress .bar').width(0);
	bold = 0;
	//Firefox Bugfix
	$('#ruploadprogress .bar').css('background-image',$('#ruploadprogress .bar').css('background-image'));
	rstartupload();
	setInterval("rfetchstats()",refresh * 1000);
}
function rstartupload(){
	var txt_title = $('#txt_title').val();
	var txt_description = $('#txt_description').val();
	var txt_name = $('#txt_name').val();
	var txt_siteurl = $('#txt_url').val();
	var txt_movieurl = $('#txt_movieurl').val();
	var cb_tomorrow = $('#cb_tomorrow').attr('checked') ? "yes" : "no";
	var cb_gay = $('#cb_gay').attr('checked') ? "yes" : "no";
	$.post(siteurl + "/remoteupload.php",
		{ 
			id: upid, 
			rand: Math.random(),
			txt_title: txt_title,
			txt_description: txt_description,
			txt_name: txt_name,
			txt_siteurl: txt_siteurl,
			txt_movieurl: txt_movieurl,
			cb_tomorrow: cb_tomorrow,
			cb_gay: cb_gay
		},
		function(ret){
			if ($('msg',ret).text() == 'ok'){
				var filehash = $('msg',ret).attr('hash');
				var fileid = $('msg',ret).attr('id');
				$('#tracelink').attr('href',mainsiteurl + '/status/' + fileid + '/' + filehash)
				$('#ruploadprogress').hide()
				$('#ruploaddone').fadeIn("slow");
			}else{
				$('.uploadcontrolls').fadeIn("slow");
				$('#ruploadprogress').hide();
				alert('The following errors occurred:\n' + ret);
			}
		}
	);
}
function rfetchstats(){
	$.post(siteurl + "/getprogress.php",
		{ id: upid, remote: '1', rand: Math.random() },
		function(xml){
			var percent = $('percent',xml).text();
			var totalbyte = $('totalbyte',xml).text();
			var aktbyte = $('aktbyte',xml).text();
			var debug = $('debug',xml).text()
			var neww = Math.round(maxwidth / 100  * percent);
			var kbs = round((aktbyte - bold) * (1 / refresh) / 1024);
			bold = aktbyte;
			$('#ruploadprogress .speed').html(kbs);
			$('#ruploadprogress .transfered').html(round(aktbyte / 1024 / 1024));
			$('#ruploadprogress .size').html(round(totalbyte / 1024 / 1024));
			$('#ruploadprogress .bar').animate({width: neww}, 'slow');
		}
	);
}
function round(x) {
	var k = (Math.round(x * 100) / 100).toString();
	k += (k.indexOf('.') == -1)? '.00' : '00';
	return k.substring(0, k.indexOf('.') + 3);
}



