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();

function swFile(settings)
{
	this.init(settings);
}

swFile.prototype.init=function(settings)
{
	this.settings = settings;
	this.settings["instance"] = swFile.instanceCount++;
	swFile.instances[this.settings["instance"]] = this;

	var name = this.settings["name"];
	var instance = this.settings["instance"];

	window["__swFile_"+instance] = new Object();

	// create SWFObject
	if (this.settings["multiple"] == true)
		this.so = new SWFObject(noCacheIE("/javascripts/swfile/swFile_multiple.swf"), "__swFile_"+instance, "160", "24", "8", "#FFFFFF");
	else
		this.so = new SWFObject(noCacheIE("/javascripts/swfile/swFile_single.swf"), "__swFile_"+instance, "160", "24", "8", "#FFFFFF");
            
            
	this.so.addVariable("uploadScript", this.settings["uploadScript"]);
	this.so.addVariable("uploadProgressCallback", this.settings["uploadProgressCallback"]);
	this.so.addVariable("uploadFinishedCallback", this.settings["uploadFinishedCallback"]);
	this.so.addVariable("fileListingCallback", this.settings["fileListingCallback"]);
	this.so.addVariable("showProgressCallback", this.settings["showProgressCallback"]);
	this.so.addVariable("allowedFileTypes", this.settings["allowedFileTypes"]);
	this.so.addVariable("allowedFileTypesDescription", this.settings["allowedFileTypesDescription"]);
	this.so.addVariable("locale", this.settings["locale"]);
	this.so.addVariable("buttonLabel", LANG_Upload);
	this.so.write(this.settings["target"]);
	
        SWFFormFix("__swFile_"+instance);
	if (flashInstalled == true)
	  document.getElementById("swFileButton_"+name).style.display = "block";

	this.instance = document.getElementById("__swFile_"+instance);
}

swFile.prototype.setSetting=function(key, value) {
        
//        this.instance.addVariable(key, value);
        
}

swFile.prototype.startUpload=function()
{
	this.instance.startUpload();
}

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 = language_keys["NOTICE_UPLOAD_COMPLETE"];
	//document.getElementById("swFileUpload").style.display = "none";
	document.forms.myupload.submit();
	//document.forms[formName].submit();
}

swFile.prototype.displayFiles=function(list)
{
	document.getElementById("fileListing_"+this.settings["name"]).innerHTML = '<br />';
	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 = language_keys["ERROR_MAX_SIZE"] + " " + 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_keys["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_keys["minutes"];
	}
}

function fileMailerSubmit()
{
	if (flashInstalled == true) {
   		new LITBox('<system:serverpath>/loading.php?flash=true',{type:'window',overlay:true,width:260,height:150});
   		swFile.startUpload();
      } else {
  		new LITBox('<system:serverpath>/loading.php?flash=false',{type:'window',overlay:true,width:300,height:330});
  		window.setTimeout("document.stdform.submit();", 1000);
      }
}

