var flash = new SWFObject("", "", "0", "0", "12");
if (flash.installedVer.major > 0 && flash.installedVer.major >= 8)
    var flashInstalled = true;
else
    var flashInstalled = false;

swFile.instanceCount = 0;
swFile.instances = new Array();

var language = new Array();
language["de"] = new Array();
language["de"]["seconds"] = "Sekunden";
language["de"]["minutes"] = "Minuten";
language["de"]["hours"] = "Stunden";

language["en"] = new Array();
language["en"]["seconds"] = "seconds";
language["en"]["minutes"] = "minutes";
language["en"]["hours"] = "hours";

function swFile(settings)
{
	this.init(settings);
}

swFile.prototype.init=function(settings)
{
	window["myFlash"] = new Object();
	this.settings = settings;
	this.settings["instance"] = swFile.instanceCount++;
	this.settings["files"] = 0;
	swFile.instances[this.settings["instance"]] = this;
	

	var name = this.settings["name"];
	var instance = this.settings["instance"];
	
	// create SWFObject
	if (this.settings["multiple"] == true)
		var so = new SWFObject(noCacheIE("/_system/form/js/swfile/swFile_multiple.swf"), "myflash", "0", "0", "8", "#FFFFFF");
	else
		var so = new SWFObject(noCacheIE("/_system/form/js/swfile/swFile_single.swf"), "myFlash", "0", "0", "8", "#FFFFFF");
	so.addVariable("uploadScript", this.settings["uploadScript"]);
	so.addVariable("uploadProgressCallback", this.settings["uploadProgressCallback"]);
	so.addVariable("uploadFinishedCallback", this.settings["uploadFinishedCallback"]);
	so.addVariable("fileListingCallback", this.settings["fileListingCallback"]);
	so.addVariable("showProgressCallback", this.settings["showProgressCallback"]);
	so.addVariable("allowedFileTypes", this.settings["allowedFileTypes"]);
	so.addVariable("allowedFileTypesDescription", this.settings["allowedFileTypesDescription"]);
	so.addVariable("locale", this.settings["locale"]);
	so.write(this.settings["target"]);
	SWFFormFix("myFlash");
	if (flashInstalled == true)
	  document.getElementById("swFileButton_"+name).style.display = "block";

	this.instance = document.getElementById("myFlash");
}

swFile.prototype.startUpload=function()
{
	if (checkProduct() == true) {
		if (this.settings["files"] > 0) 
			this.instance.startUpload();
		else 
			document.getElementById("noFile").style.display = "block";
	}
}

swFile.prototype.showProgress=function()
{
	this.instance.showProgress();
}

swFile.prototype.uploadProgress=function(uploadProgress)
{
	document.getElementById("uploadProgress").style.display = "block";

	name = uploadProgress.name;

	if (name.length > 16) {
		name = name.substr(0, 16) + "...";
	}

	document.getElementById("fileName").innerHTML = name;
	document.getElementById("uploadedBytes").innerHTML = formatFileSize(uploadProgress.uploadedBytes);
	document.getElementById("totalBytes").innerHTML = formatFileSize(uploadProgress.totalBytes);
	document.getElementById("currentSpeed").innerHTML = formatFileSize(uploadProgress.currentSpeed);
	document.getElementById("timeRemaining").innerHTML = formatDateType(uploadProgress.timeRemaining);
	document.getElementById("percentCompleted").style.width = uploadProgress.percent+"%";
}

swFile.prototype.uploadFinished=function()
{
	this.uploadFinished = true;
	document.getElementById("uploadProgress").innerHTML = "Dateiupload abgeschlossen!";
	document.getElementById("calculatePreview").style.display = "block";
	document.getElementById("swFileUpload").style.display = "none";
	var formName = this.settings["form"];
	document.forms[formName].submit();
}

swFile.prototype.displayFiles=function(list)
{
	this.settings["files"] = list.length
	document.getElementById("fileListing_"+this.settings["name"]).innerHTML = '<br />';
	document.getElementById("noFile").style.display = "none";
	var totalBytes = 0;
	for (i=0; i<list.length; i++) {

		var name = list[i].name;
		if (name.length > 16)
			name = name.substr(0, 16) + "...";

		var text = "";
		text += '<div class="fileListLeft">';
		text += name;
		text += '</div>';
		text += '<div class="fileListRight">';
		text += formatFileSize(list[i].size);
		text += '</div><br style="clear: both;" />';
		totalBytes += parseInt(list[i].size);
		//document.getElementById("s_filecount").value++;
		document.getElementById("fileListing_"+this.settings["name"]).innerHTML += text;
	}
	// uploads too big
	totalBytes = parseInt(totalBytes);
	maxFileSize = parseInt(this.settings["maxFileSize"]*1024*1024);
	if (totalBytes > maxFileSize) {
		document.getElementById("swFileError").innerHTML = "Die gewünschte Datei übersteigt die maximal erlaubte Größe von " + this.settings["maxFileSize"] + "MB";
		document.getElementById("upload").disabled = "disabled";
	} else
		document.getElementById("upload").disabled = "";
}

swFile.prototype.openFileBrowser=function()
{
	this.instance.openFileBrowser();
}

function formatFileSize(size)
{
	if(size < 1024)
		return parseInt(size*100)/100 + " bytes";
	if(size < 1048576)
		return parseInt((size / 1024)*100)/100 + "KB";
	if(size < 1073741824)
	   return parseInt((size / 1048576)*100)/100 + "MB";
	return parseInt((size / 1073741824)*100)/100 + "GB";
}

function formatDateType(seconds)
{
	if (seconds < 60)
		return seconds + " " + language["de"]["seconds"];
	else {
		var minutes = Math.floor(seconds / 60)
		seconds = seconds - (minutes * 60);
		if (seconds < 10)
			var newseconds = "0" + seconds;
		else
			var newseconds = seconds;
		return minutes + ":" + newseconds + " " + language["de"]["minutes"];
	}
}

function fileMailerSubmit()
{
	if (flashInstalled == true) {
   		new LITBox('/loading.php?flash=true',{type:'window',overlay:true,width:260,height:150});
   		swFile.startUpload();
      } else {
  		new LITBox('/loading.php?flash=false',{type:'window',overlay:true,width:300,height:330});
  		window.setTimeout("document.stdform.submit();", 1000);
      }
}
	