/*
	A simple class for displaying file information and progress
	Note: This is a demonstration only and not part of SWFUpload.
	Note: Some have had problems adapting this class in IE7. It may not be suitable for your application.
*/

// Constructor
// file is a SWFUpload file object
// targetID is the HTML element id attribute that the FileProgress HTML structure will be added to.
// Instantiating a new FileProgress object with an existing file will reuse/update the existing DOM elements
function FileProgress(file, targetID) {
	this.file = file;
	this.fileProgressID = file.id;

	this.opacity = 100;
	this.height = 0;
		
	this.fileProgressWrapper = document.getElementById(this.fileProgressID);
	if (!this.fileProgressWrapper) {
		this.fileProgressWrapper = document.createElement("div");
		this.fileProgressWrapper.className = "progressWrapper";
		this.fileProgressWrapper.id = this.fileProgressID;

		this.fileProgressElement = document.createElement("div");
		this.fileProgressElement.className = "progressContainer gray";

		// Ãë¼Ò ¹öÆ°
		var progressCancel = document.createElement("a");
		progressCancel.className = "progressCancel";
		progressCancel.style.cursor = "pointer";

		// ÆÄÀÏ ÀÌ¸§
		var progressText = document.createElement("div");
		progressText.className = "progressName";
		progressText.appendChild(document.createTextNode(file.name));

		// ÇÁ·Î±×·¡½º ¹Ù
		var progressBar = document.createElement("div");
		progressBar.className = "progressBarInProgress";
		
		this.fileProgressElement.appendChild(progressCancel);		// [0]
		this.fileProgressElement.appendChild(progressText);			// [1]
		this.fileProgressElement.appendChild(progressBar);			// [2]

		this.fileProgressWrapper.appendChild(this.fileProgressElement);

		document.getElementById(targetID).appendChild(this.fileProgressWrapper);
	} else {
		this.fileProgressElement = this.fileProgressWrapper.firstChild;
		this.reset();
	}

	this.height = this.fileProgressWrapper.offsetHeight;
	this.setTimer(null);
}

FileProgress.prototype.setTimer = function (timer) {
	this.fileProgressElement["FP_TIMER"] = timer;
};
FileProgress.prototype.getTimer = function (timer) {
	return this.fileProgressElement["FP_TIMER"] || null;
};

FileProgress.prototype.reset = function () {
	this.fileProgressElement.className = "progressContainer";

	this.fileProgressElement.childNodes[2].className = "progressBarInProgress";
	this.fileProgressElement.childNodes[2].style.width = "0%";
	
	this.appear();	
};

FileProgress.prototype.setProgress = function (percentage) {
	this.fileProgressElement.className = "progressContainer green";
	this.fileProgressElement.childNodes[2].className = "progressBarInProgress";
	this.fileProgressElement.childNodes[2].style.width = percentage + "%";

	this.appear();	
};
// ¿Ï·á ¼¼ÆÃ
FileProgress.prototype.setComplete = function () {
	this.fileProgressElement.className = "progressContainer blue";
	this.fileProgressElement.childNodes[2].className = "progressBarComplete";
	this.fileProgressElement.childNodes[2].style.width = "";

	this.setEvent();
		
	// ÆÄÀÏ »èÁ¦ ¹öÆ°
	var remove = new Object();
	remove.fileProgress = this;
	remove.r = function() {
		var fileName = this.fileProgress.file.fileName;
		var params = "dfile=" + fileName;
		
		try{
			// callback ¾øÀ½
			sendRequest("/servlets/FileDelete", params, null, "get");
			sel=document.getElementById('select_files');
			var listsize = sel.length;
			if(listsize != 0) {
				for(i=0; i<listsize; i++){
					var s_file = sel.options[i].value;
					
					var array_file = s_file.split("$$"); 					
					var d_file = array_file[0] + "$$" + array_file[1];
					if(d_file == this.fileProgress.file.realName+"$$"+fileName){
						sel[i] = null;
					}
				}
			}
		}catch(ignore) {
			
		}	
		
		this.fileProgress.setCancelled();
	};
	
	this.fileProgressElement.childNodes[0].onclick = function() {
		remove.r();
	};
	
};

FileProgress.prototype.setError = function () {
	this.fileProgressElement.className = "progressContainer red";
	this.fileProgressElement.childNodes[2].className = "progressBarError";
	this.fileProgressElement.childNodes[2].style.width = "";

	var oSelf = this;
	this.setTimer(setTimeout(function () {
		oSelf.disappear();
	}, 5000));
};
FileProgress.prototype.setCancelled = function () {
	// this.fileProgressElement.className = "progressContainer";
	// »¡°£»ö Ã³¸®
	this.setError();
	// ÇÁ·Î±×·¡½º¹Ù ¼û±è
	this.fileProgressElement.childNodes[2].className = "progressBarError";
	this.fileProgressElement.childNodes[2].style.width = "";
	// ÆÄÀÏÀÌ¸§¿¡ ¸µÅ© ¾ø¾Ú
	this.fileProgressElement.childNodes[1].innerHTML = this.file.name;
	this.fileProgressElement.childNodes[1].className = "progressName";
	// Ãë¼Ò ¹öÆ° ¼û±è
	this.fileProgressElement.childNodes[0].style.visibility = "hidden";
	// ¼­¼­È÷ »ç¶óÁü
	var oSelf = this;
	this.setTimer(setTimeout(function () {
		oSelf.disappear();
	}, 2000));
};
// ÆÄÀÏÀÌ¸§ set
FileProgress.prototype.setName = function (name) {
	this.fileProgressElement.childNodes[1].innerHTML = name;
};
// »óÅÂ get
FileProgress.prototype.getName = function () {
	return this.fileProgressElement.childNodes[1].innerHTML;
};

/**
 * ÀÌº¥Æ® set
 * ÆÄÀÏÀÌ ÀÌ¹ÌÁöÀÌ¸é º»¹®¿¡ ¹Ù·Î º¸¿©ÁÖ°í, ±× ÀÌ¿ÜÀÌ¸é ´Ù¿î·Îµå ÇÒ ¼ö ÀÖ°Ô ¸µÅ©¸¦ °Ç´Ù.
 * @param file ¾÷·Îµå ¿Ï·áµÈ ÆÄÀÏÀÇ Á¤º¸
 */
FileProgress.prototype.setEvent = function () {
		
	var html = "";
	var src = "";
	var className = "link";
	
	// isImage(ext) in common.js
	if( isImage(this.file.type) == true ) {
		//src = this.file.context + "/" + this.file.path + "/" + this.file.realName;
		src = "/files/" + this.file.fileName;
		//html += "<span onclick=\"pasteImage('" + src + "', '" + this.file.imgWidth + "', '" + this.file.imgHeight + "')\" class=\"" + className + "\" title=\"º»¹®¿¡ ±×¸²À» µî·ÏÇÕ´Ï´Ù.\">";
		html += "<span onclick=\"pasteImage('" + src + "', '100', '100')\" class=\"" + className + "\" title=\"º»¹®¿¡ ±×¸²À» µî·ÏÇÕ´Ï´Ù.\">";	
	} else { 
		//html += "<span onclick=\"pasteFile('" + src + "', '" + this.file.name + "')\" class=\"" + className + "\" title=\"ÀÌ¹ÌÁö ¹Ì¸®º¸±â.\">";
		html += "<span onclick=\"noImagFile();\"class=\"" + className + "\">";
	}
	
	//alert(document.getElementById("select_files"));
	
	//select box¿¡ Ç×¸ñ Ãß°¡ 
	//var listsize = document.getElementById("select_files").length;
	//document.getElementById("select_files").length.options[listsize] = new Option("\"" + this.file.realName + "\"", "\"" +this.file.realName+"$$"+this.file.fileName + "\"");
	//alert(document.getElementById("select_files").length.options[listsize]);

	//alert(this.file.realName);

	var objSelect = document.getElementById("select_files");
	var listsize =objSelect.options.length;
	
	var optionName = this.file.realName + "$$" + this.file.fileName + "$$" + this.file.imgWidth + "$$" + this.file.imgHeight + "$$" + this.file.size;
	//alert(optionName);
	objSelect.options[listsize]=new Option(this.file.realName,optionName,true,true);	
	
	html += this.file.name;
	html += "[";
	html += this.file.imgWidth;
	html += "X";
	html += this.file.imgHeight;
	html += "&nbsp;/&nbsp;";
	html += calculateFileSize(this.file.size);
	html += "]";
	html += "</span>";
	
	this.setName(html);
};

// ¾÷·Îµå Ãë¼Ò¹öÆ° º¸ÀÌ±â/¼û±â±â
FileProgress.prototype.toggleCancel = function (show, swfUploadInstance) {
	this.fileProgressElement.childNodes[0].style.visibility = show ? "visible" : "hidden";
	if (swfUploadInstance) {
		var fileID = this.fileProgressID;
		this.fileProgressElement.childNodes[0].onclick = function () {
			swfUploadInstance.cancelUpload(fileID);
			return false;
		};
	}
};

FileProgress.prototype.setStatus = function (status) {
	this.fileProgressElement.childNodes[1].innerHTML = status;
};

FileProgress.prototype.appear = function () {
	if (this.getTimer() !== null) {
		clearTimeout(this.getTimer());
		this.setTimer(null);
	}
	
	if (this.fileProgressWrapper.filters) {
		try {
			this.fileProgressWrapper.filters.item("DXImageTransform.Microsoft.Alpha").opacity = 100;
		} catch (e) {
			// If it is not set initially, the browser will throw an error.  This will set it if it is not set yet.
			this.fileProgressWrapper.style.filter = "progid:DXImageTransform.Microsoft.Alpha(opacity=100)";
		}
	} else {
		this.fileProgressWrapper.style.opacity = 1;
	}
		
	this.fileProgressWrapper.style.height = "";
	
	this.height = this.fileProgressWrapper.offsetHeight;
	this.opacity = 100;
	this.fileProgressWrapper.style.display = "";
	
};

// Fades out and clips away the FileProgress box.
FileProgress.prototype.disappear = function () {

	var reduceOpacityBy = 15;
	var reduceHeightBy = 4;
	var rate = 30;	// 15 fps

	if (this.opacity > 0) {
		this.opacity -= reduceOpacityBy;
		if (this.opacity < 0) {
			this.opacity = 0;
		}

		if (this.fileProgressWrapper.filters) {
			try {
				this.fileProgressWrapper.filters.item("DXImageTransform.Microsoft.Alpha").opacity = this.opacity;
			} catch (e) {
				// If it is not set initially, the browser will throw an error.  This will set it if it is not set yet.
				this.fileProgressWrapper.style.filter = "progid:DXImageTransform.Microsoft.Alpha(opacity=" + this.opacity + ")";
			}
		} else {
			this.fileProgressWrapper.style.opacity = this.opacity / 100;
		}
	}

	if (this.height > 0) {
		this.height -= reduceHeightBy;
		if (this.height < 0) {
			this.height = 0;
		}

		this.fileProgressWrapper.style.height = this.height + "px";
	}

	if (this.height > 0 || this.opacity > 0) {
		var oSelf = this;
		this.setTimer(setTimeout(function () {
			oSelf.disappear();
		}, rate));
	} else {
		this.fileProgressWrapper.style.display = "none";
		this.setTimer(null);
	}
};
