var FileExplorer = Class.create();
Object.extend(Object.extend(FileExplorer.prototype, Window.prototype), {
	DIRECTORY_ICON_CLASS_NAME : "directoryIcon",
	FILE_ICON_CLASS_NAME : "fileIcon",
	rootFolderName : "PDS/",
	folderName : "",
	leap : 0,
	dirCount : 0,
	fileCount : 0,
	processingCount : 0,
	createDirHandler : function() {
		this.dirs = this.content.getElementsBySelector('div[class="'+
		this.DIRECTORY_ICON_CLASS_NAME +'"]');
		this.dirCount = 0;
		this.setContentHeader(this.folderName);
		this.dirs.each(function(dir) {
			dir.style.cursor = "pointer";
			++this.dirCount;
			Event.observe(dir, "mouseover", function(event) {
				Element.toggleClassName(dir, this.DIRECTORY_ICON_CLASS_NAME);
			});
			Event.observe(dir, "mouseout", function(event) {
				Element.toggleClassName(dir, this.DIRECTORY_ICON_CLASS_NAME);
			});
			var fName = "";
			if(".." == dir.innerHTML) {
				fName = this.folderName.substring(0, this.folderName.lastIndexOf("/"))
			} else {
				fName = this.folderName + "/" + dir.innerHTML;
			}
			Event.observe(dir, "click", function(event) {
				if(0 != this.processingCount) return;
				this.folderName = fName;
				this.request();
				if(this.child) this.child.request();
			}.bindAsEventListener(this));
		}.bind(this));
	},
	createFileHandler : function() {
		this.fileCount = 0;
		this.files = this.content.getElementsBySelector('div[class="' +
		this.FILE_ICON_CLASS_NAME
		 +'"]');
		this.files.each(function(file, index) {
			++this.fileCount;
			file.style.cursor = "pointer";
			var dTarget = $("javawideDownloader" + index);
			if(!dTarget) {
				dTarget = document.createElement("iframe");
				dTarget.style.display = "none";
				dTarget.setAttribute("id", "javawideDownloader" + index);
				document.body.appendChild(dTarget);
			}
			Event.observe(file, "mouseover", function(event) {
				Element.toggleClassName(file, "fileIcon");
			});
			Event.observe(file, "mouseout", function(event) {
				Element.toggleClassName(file, "fileIcon");
			});
			Event.observe(file, "click", function(event) {
				if(0 != this.processingCount) return;
				this.downloadFile(file, index);
			}.bindAsEventListener(this));
		}.bind(this));
	},
	downloadFile : function(file, index) {
		var dTarget = $("javawideDownloader" + index);
		dTarget.setAttribute("src", "content/jsp/file/download.jsp?FOLDERNAME=" +
		encodeURIComponent(this.rootFolderName + this.folderName) + "&FILENAME=" +
		encodeURIComponent(file.innerHTML));
	},
	createAllDownload : function() {
		var btn = document.createElement("input");
		btn.setAttribute("type", "button");
		btn.setAttribute("value", "모두 다운로드 - All Download");
		var index = navigator.appVersion.indexOf("MSIE");
		var version;
		if (-1 != index) {
			version = parseFloat(navigator.appVersion.substring(index+4, index+8));
		}
		Event.observe(btn, "click", function(evt) {
			this.leap = 0;
			var queue = [];
			this.processingCount = 0;
			this.files.each(function(file, index) {
				var timer = new Timer(function() {
					this.downloadFile(file, index);
					--this.processingCount;
				}.bind(this));
				this.leap += Prototype.Browser.IE && version < 7.0 ? 6000 : 2000;
				timer.setTick(this.leap);
				queue.push(timer);
			}.bind(this));
			queue.each(function(item) {
				++this.processingCount;
				item.start();
			}.bind(this));
		}.bindAsEventListener(this));
		return btn;
	},
	createUpload : function() {
		var btnSingle = document.createElement("input");
		btnSingle.setAttribute("type", "button");
		btnSingle.setAttribute("value", "업로드 - Upload");
		var btnMultiple = document.createElement("input");
		btnMultiple.setAttribute("type", "button");
		btnMultiple.setAttribute("value", "다중 업로드 - Multiple Upload");
		Event.observe(btnSingle, "click", function(evt) {
			system.menu.show({id : "52", parentId : this.id});
		}.bindAsEventListener(this));
		Event.observe(btnMultiple, "click", function(evt) {
			system.menu.show({id : "53", parentId : this.id});
		}.bindAsEventListener(this));
		return [btnSingle, btnMultiple];
	},
	createButtons: function() {
		if($(this.id+"explorerButtons")) return;
		var buttons = document.createElement("div");
		buttons.setAttribute("id", this.id+"explorerButtons");
		var uploadButtons = this.createUpload();
		uploadButtons.unshift(this.createAllDownload());
		uploadButtons.each(function(elem) {
			elem.className = "explorerButtons";
			buttons.appendChild(elem);
		});
		this.contentFooter.appendChild(buttons);
	},
	request : function() {
		var param = $H({ contentId : this.link, folderName : this.rootFolderName + this.folderName });
		new Ajax.Request(this.URL, {parameters: param,
			onSuccess: function(response) {
				var upper = document.createElement("div");
				upper.appendChild(document.createTextNode(".."));
				upper.className = this.DIRECTORY_ICON_CLASS_NAME;
				upper = upper.outerHTML||(new XMLSerializer).serializeToString(upper);
				this.setContent(upper + response.responseText);
				this.createDirHandler();
				this.createFileHandler();
				this.createButtons();
				this.setStatusMessage("폴더 갯수 : " + Math.max(0, this.dirCount) + "/파일 갯수 : " + this.fileCount);
			}.bindAsEventListener(this),
			onFailure : function(response) {
				this.setContent("요청한 link가 없거나 서버 오류가 발생했습니다.");
			}.bindAsEventListener(this)}
		);
	}
});