var iTime = "";
var Timeleft = "";


function roundNumber(num, dec) {
	var result = Math.round(num*Math.pow(10,dec))/Math.pow(10,dec);
	return result;
}

function minsec(time, tempTime) {
var ztime;
	if (time == "m") {
		ztime = Math.floor(tempTime/60);
		if (ztime < 10) {
			ztime = "0" + ztime;	
		}
	} else if (time == "s") {
		ztime = Math.ceil(tempTime % 60);
		if (ztime < 10) {
			ztime = "0" + ztime;	
		}
	} else {
		ztime = "minsec error...";
	}
return ztime;
}

function fileQueueError(fileObj, error_code, message) {
	try {
		var error_name = "";
		switch(error_code) {
			case SWFUpload.ERROR_CODE_QUEUE_LIMIT_EXCEEDED:
				error_name = "You have attempted to queue too many files.";
			break;
		}

		if (error_name !== "") {
			alert(error_name);
			return;
		}

		switch(error_code) {
			case SWFUpload.QUEUE_ERROR.ZERO_BYTE_FILE:
				image_name = "zerobyte.gif";
			break;
			case SWFUpload.QUEUE_ERROR.FILE_EXCEEDS_SIZE_LIMIT:
				image_name = "toobig.gif";
			break;
			case SWFUpload.QUEUE_ERROR.ZERO_BYTE_FILE:
			case SWFUpload.QUEUE_ERROR.INVALID_FILETYPE:
			default:
				alert(message);
				image_name = "error.gif";
			break;
		}

	} catch (ex) { this.debug(ex); }

}

function fileDialogComplete(num_files_queued) {
	try {
		if (num_files_queued > 0) {
			this.startUpload();
			var currentTime = new Date()
			iTime = currentTime;
			Timeleft = "Вычисляю...";
		}
	} catch (ex) {
		this.debug(ex);
	}
}

function fileQueued(fileObj){
	//document.getElementById("btnBrowse").innerHTML = fileObj.name;
}

function uploadProgress(file, bytesLoaded, bytesTotal) {

	try {
		var currentTime = new Date();
		var percent = Math.ceil((bytesLoaded / file.size) * 100);
		var progress = new FileProgress(file,  this.customSettings.upload_target);
		progress.SetProgress(percent);		
		var tempTime = 0;	
		var rndfilesize = roundNumber(((file.size/1024)/1024),1);
		var uploaded = roundNumber(((bytesLoaded/1024)/1024),1);
		var uTime = (Math.ceil(currentTime-iTime)/1000);
		var uSpeed = Math.floor(roundNumber(((bytesLoaded/uTime)/1024),2));
		var tempTime = uTime;
		uTime = minsec("m", tempTime) + ":" + minsec("s", tempTime) + " elapsed";	
		tempTime = roundNumber(((((bytesTotal-bytesLoaded)/uSpeed)/60)/10),2);
		if (tempTime != "Infinity") {
			if (tempTime > 0) {
				Timeleft = minsec("m", tempTime) + ":" + minsec("s", tempTime);
			} else {
				Timeleft = "00:00";
			}
		} else {
			Timeleft = "00:00";
		}
		
		//Variables available
		//uSpeed = the rate of upload (40 kB/s)
		//uploaded = how much of the file has upload in MB
		//rndfilesize = file size in MB
		//uTime = how much time has been spent uploading in min:sec (xx:yy elapsed)
		//Timeleft = how much time is left in min:sec (xx:yy remain)
		progress.SetStatus(' Закачано: ' + uploaded + ' из ' + rndfilesize + ' Мб ' + ' (' + percent + '%) &nbsp;&nbsp;&nbsp; Скорость закачки: ' + uSpeed + ' Кб/сек &nbsp;&nbsp;&nbsp; Осталось: ' + Timeleft);
	} catch (ex) { this.debug(ex); }
}

function uploadSuccess(fileObj, server_data) {
	try {
		var currentTime = new Date()
		document.getElementById('uploaded').style.display = "block"
		document.getElementById('uploaded').innerHTML = document.getElementById('uploaded').innerHTML + 'Ссылка для скачивания "'+fileObj.name+'": <a href="http://yabadaba.ru/files/'+server_data+'">http://yabadaba.ru/files/'+server_data+'</a><br/>'; 
		var progress = new FileProgress(fileObj, this.customSettings.upload_target);
		progress.SetStatus("Файл закачан!");
		progress.setComplete();
		/*var cTime = (Math.ceil(currentTime-iTime)/1000);
		var zmin = 0;
		var zsec = 0;
		zmin = Math.floor(cTime/60);
		if (zmin < 10) {
			zmin = "0" + zmin;	
		}
		zsec = Math.ceil(cTime % 60);
		if (zsec < 10) {
			zsec = "0" + zsec;	
		}*/
		//progress.SetStatus("Время закачки: " + zmin + ":" + zsec);
		progress.toggleCancel(false);
	} catch (ex) { this.debug(ex); }
}

function uploadComplete(fileObj) {
	try {
		if (this.getStats().files_queued > 0) {
			this.startUpload();
		} else {
			var progress = new FileProgress(fileObj,  this.customSettings.upload_target);
			progress.SetComplete();
			progress.ToggleCancel(false);
		}
	} catch (ex) { this.debug(ex); }
}

function uploadError(fileObj, error_code, message) {
	var image_name =  "error.gif";
	try {
		switch(error_code) {
			case SWFUpload.UPLOAD_ERROR.UPLOAD_STOPPED:
				try {
					var progress = new FileProgress(fileObj,  this.customSettings.upload_target);
					progress.SetCancelled();
					progress.SetStatus("Stopped");
					progress.ToggleCancel(true, this, fileObj.id);
				}
				catch (ex) { this.debug(ex); }
			case SWFUpload.UPLOAD_ERROR.UPLOAD_LIMIT_EXCEEDED:
				image_name = "uploadlimit.gif";
			break;
			default:
				alert(message);
			break;
		}
	} catch (ex) { this.debug(ex); }

}

function FileProgress(fileObj, target_id) {
	this.file_progress_id = "divFileProgress";

	this.fileProgressWrapper = document.getElementById(this.file_progress_id);
	if (!this.fileProgressWrapper) {
		this.fileProgressWrapper = document.createElement("div");
		this.fileProgressWrapper.className = "progressWrapper";
		this.fileProgressWrapper.id = this.file_progress_id;

		this.fileProgressElement = document.createElement("div");
		this.fileProgressElement.className = "progressContainer";

		var progressSpinner = document.createElement("img");
		progressSpinner.className = "progressSpinner";
		progressSpinner.src = "/images/spinner.gif";
		//progressCancel.style.visibility = "hidden";
		//progressCancel.appendChild(document.createTextNode(" "));

		var progressText = document.createElement("div");
		progressText.className = "progressName";
		progressText.appendChild(document.createTextNode(fileObj.name));

		var progressBar = document.createElement("div");
		progressBar.className = "progressBarInProgress";

		var progressBarContainer = document.createElement("div");
		progressBarContainer.className = "progressBarContainer";
		progressBarContainer.appendChild(progressBar);

		
		var progressStatus = document.createElement("div");
		progressStatus.className = "progressBarStatus";
		progressStatus.innerHTML = "&nbsp;";

		this.fileProgressElement.appendChild(progressSpinner);
		this.fileProgressElement.appendChild(progressText);
		
		this.fileProgressElement.appendChild(progressBarContainer);
		this.fileProgressElement.appendChild(progressStatus);

		this.fileProgressWrapper.appendChild(this.fileProgressElement);

		document.getElementById(target_id).appendChild(this.fileProgressWrapper);

	} else {
		this.fileProgressElement = this.fileProgressWrapper.firstChild;
		this.fileProgressElement.childNodes[1].firstChild.nodeValue = fileObj.name;
	}

	this.height = this.fileProgressWrapper.offsetHeight;

}
FileProgress.prototype.SetProgress = function(percentage) {
	this.fileProgressElement.className = "progressContainer green";
	this.fileProgressElement.childNodes[2].childNodes[0].className = "progressBarInProgress";
	this.fileProgressElement.childNodes[2].childNodes[0].style.width = percentage + "%";
}
FileProgress.prototype.SetComplete = function() {
	this.fileProgressElement.className = "progressContainer blue";
	this.fileProgressElement.childNodes[2].childNodes[0].className = "progressBarComplete";
	this.fileProgressElement.childNodes[2].childNodes[0].style.width = "";

}
FileProgress.prototype.SetError = function() {
	this.fileProgressElement.className = "progressContainer red";
	this.fileProgressElement.childNodes[2].childNodes[0].className = "progressBarError";
	this.fileProgressElement.childNodes[2].childNodes[0].style.width = "";

}
FileProgress.prototype.SetCancelled = function() {
	this.fileProgressElement.className = "progressContainer";
	this.fileProgressElement.childNodes[2].childNodes[0].className = "progressBarError";
	this.fileProgressElement.childNodes[2].childNodes[0].style.width = "";

}
FileProgress.prototype.SetStatus = function(status) {
	this.fileProgressElement.childNodes[3].innerHTML = status;
}

FileProgress.prototype.ToggleCancel = function(show, upload_obj, file_id) {
	this.fileProgressElement.childNodes[0].style.display = show ? "block" : "none";
//	if (upload_obj) {
//		this.fileProgressElement.childNodes[0].onclick = function() { upload_obj.cancelUpload(); return false; };
//	}
}

