// 2008-08-04 12:38¿ÀÈÄ
// haroc
// ##############################################################################################
// Ã·ºÎÆÄÀÏÁ¤º¸

var p_Attach = {
	ATTACH_INFO_PARTITION		: "^$$$$$^",
	ATTACH_FILE_PARTITION		: "^#####^",
	_interval			:"",
	_editorName			: "FCKeditor1",
	_contents 			: "virtual_content",
	_selectName			: "BetaBoardFileList",
	_uploadform 		: "frmMainMenu",
	_input_thumbnail 	: "thumbnail",
	_url_folder			: "url_folder",
	_thumbnailPreview	: "thumbnail_preview",
	_image_name			: "/images/common/preview.jpg",
	_thumbnail_filename : "thumbfile",
	_target				: "action_frame",
	_action				: "/common/FileUpload.asp",
	_encoding			: "multipart/form-data",
	_file_span_name		: "NewFile_span",
	_attachTitle		: "attach_title",
	_selectedindex		: -1,
	_optionvalue		: "",
	_uploadfilelist : "upload_file_list", 
	// 2008-08-28 11:37¿ÀÀü
	// haroc
	// ##############################################################################################
	// set UploadForm Name
	setUploadForm : function (frmName) {
		this._uploadform = frmName;
	},
	// 2008-08-05 10:11¿ÀÀü
	// haroc
	// ##############################################################################################
	// FCKEditor¿¡¼­ Select·Î imgÁ¤º¸¸¦ ¿Å°ÜÁØ´Ù.
	fillSelect: function() {
		var html = getHTML(this._editorName);
		$(this._contents).hide();
		$(this._contents).innerHTML = html;

		var vImgList = $(this._contents).getElementsByTagName("img");
		for ( var i = 0 ; i < vImgList.length ; i++ ) {
			var img = vImgList[i];

			//debug(img.getAttribute("src").indexOf(location.host));
			// µ¿ÀÏÇÑ HOSTÀÇ ÀÌ¹ÌÁö¸¸ °¡Á®¿Â´Ù.
			if ( 7 == img.getAttribute("src").indexOf(location.host) ) {
				if ( -1 != img.getAttribute("src").replace(location.host, "").indexOf("/upload/") ) {
					var vFileInfomation 	= img.getAttribute("src").split("/");
					var vFileName 	= vFileInfomation[vFileInfomation.length - 1];
					if ( eval(img.getAttribute("fileSize")) > 0 ) {
						var vFileSize	= Math.round(eval(img.getAttribute("fileSize")) / 1024);
						//debug (fileLabel);
						var FullFileName = p_Attach.getFileNameWithPartition({orgFile:vFileName, realFile:vFileName, fileSize:vFileSize});
		
						var fileLabel = p_Attach.getFileLabel(FullFileName);
		
						if ( !p_Attach.searchFileList (vFileName) ) {
							p_Attach.AddOption(this._selectName, fileLabel, FullFileName);
						}
					}
				}
			}
		}
	},
	// 2008-08-05 10:11¿ÀÀü
	// haroc
	// ##############################################################################################
	// ÀÚµ¿À¸·Î FCKEditor¿¡¼­ Select·Î imgÁ¤º¸¸¦ ¿Å°ÜÁØ´Ù.
	autoFill: function () {
		return false;
		if ( !$(this._editorName) ) {
			return false;
		}
		if ( p_Attach.fillSelect ) {
			this._interval = setInterval ("p_Attach.fillSelect();", 1000);
		}
	},
	// 2008-07-31 11:36¿ÀÀü
	// haroc
	// ##############################################################################################
	// Select¿¡¼­ ÆÄÀÏÀÌ¸§À» Ã£´Â´Ù.
	searchFileList : function(file_name) {
		var vFileList = file_name.split("\\");
		var vSelectList = $(this._selectName);

		//debug ( vSelectList.length );
		for ( var i = 0 ; i < vSelectList.length ; i++ ) {
			var listedFilename = p_Attach.getOriginalFileName(vSelectList[i].value);

			//debug ("debug" + listedFilename);
			//debug ("debug" + vFileList[vFileList.length - 1]);
			// µ¿ÀÏÇÑ ÆÄÀÏ¸íÀÌ ÀÖ´Â °æ¿ì.
			if ( listedFilename == vFileList[vFileList.length - 1] ) {
				return true;
			}
		}
		return false;
	},
	// 2008-07-30 11:11¿ÀÀü
	// haroc
	// ##############################################################################################
	// FileÀÌ¸§À» Select¿¡ Ãß°¡ÇÑ´Ù.
	AddBetaBoardFileList : function (file_name) {
		var fileLabel 	= p_Attach.getFileLabel(file_name);

		p_Attach.AddOption(this._selectName, fileLabel, file_name);

		// frmMainMenu -> form nameÀÌ º¯°æµÇ´Â °æ¿ì ¼öÁ¤ÇØ¾ß ÇÑ´Ù.
		p_Attach.initFileInput(this._file_span_name, this._uploadform);
	},
	// 2008-08-01 11:01¿ÀÀü
	// haroc
	// ##############################################################################################
	// ÆÄÀÏÁ¤º¸¿¡¼­ ¿ø·¡ ÆÄÀÏ¸íÀ» °¡Á®¿Â´Ù.
	getOriginalFileName : function (file_name) {
		var file_info = file_name.split(p_Attach.ATTACH_INFO_PARTITION);
		if ( file_info.length > 0) {
			return file_info[0];
		}
		return '';
	},
	// 2008-08-01 11:04¿ÀÀü
	// haroc
	// ##############################################################################################
	// ÆÄÀÏÁ¤º¸¿¡¼­ ÀúÀåµÈ ÆÄÀÏ¸íÀ» °¡Á®¿Â´Ù.
	getSavedFileName : function (file_name) {
		var file_info = file_name.split(p_Attach.ATTACH_INFO_PARTITION);
		if ( file_info.length > 0) {
			return file_info[1];
		}
		return '';
	},
	// 2008-08-01 11:05¿ÀÀü
	// haroc
	// ##############################################################################################
	// ÆÄÀÏÁ¤º¸¿¡¼­ ÆÄÀÏÅ©±â¸¦ °¡Á®¿Â´Ù.
	getFileSize : function (file_name) {
		var file_info = file_name.split(p_Attach.ATTACH_INFO_PARTITION);
		if ( file_info.length > 0) {
			return file_info[2];
		}
		return '';
	},
	// 2008-08-01 11:05¿ÀÀü
	// haroc
	// ##############################################################################################
	// ÆÄÀÏÁ¤º¸¿¡¼­ ThumbnailÀ¯¹«¸¦ °¡Á®¿Â´Ù.
	getThumbnailYN : function (file_name) {
		var file_info = file_name.split(p_Attach.ATTACH_INFO_PARTITION);
		if ( file_info.length > 0) {
			return file_info[4];
		}
		return '';
	},
	// 2008-08-01 11:07¿ÀÀü
	// haroc
	// ##############################################################################################
	// ÆÄÀÏÁ¤º¸¿¡¼­ Å¸ÀÌÆ²À» °¡Á®¿Â´Ù.
	getTitle : function (file_name) {
		if ( !file_name ) {
			var vList = $(this._selectName);
			if ( -1 == vList.selectedIndex ) {
				return '';
			}
			file_name = vList[vList.selectedIndex].value;
			//debug ( file_name );
		}
		var file_info = file_name.split(p_Attach.ATTACH_INFO_PARTITION);
		for ( var i = 0 ; i < file_info.length ; i++ ) {
			//debug (file_info[i]+i);
		}
		if ( file_info.length > 0) {
			return file_info[5];
		}
		return '';
	},
	// 2008-07-31 1:38¿ÀÈÄ
	// haroc
	// ##############################################################################################
	// ÆÄÀÏÀÔ·Â±â¸¦ ÃÊ±âÈ­ÇÑ´Ù.
	initFileInput : function (span_name, frmName) {
		$(span_name).innerHTML = $(span_name).innerHTML;
		// $(span_name).innerHTML = "<input type=\"file\" id=\"NewFile\" name=\"NewFile\" value=\"ÆÄÀÏ¾÷·Îµå\" onchange=\"p_Attach.FileUpload('"+frmName+"', this); return false;\" />";
	},
	// 2008-07-30 1:02¿ÀÈÄ
	// haroc
	// ##############################################################################################
	// Option À» Ãß°¡
	AddOption : function  (obj_name, fileLabel, fileValue) {
		var fileListObj = $(obj_name);
		var vOption = document.createElement("option");

		vOption.innerHTML= fileLabel;
		vOption.setAttribute("value",fileValue);
		fileListObj.appendChild(vOption);
	},
	// 2008-07-30 1:06¿ÀÈÄ
	// haroc
	// ##############################################################################################
	// Option À» »èÁ¦
	DelOption : function  (obj_name) {
		var fileListObj = $(this._selectName);

		var param = fileListObj[fileListObj.selectedIndex];
		fileListObj.removeChild(param);
	},
	// 2008-07-30 12:38¿ÀÈÄ
	// haroc
	// ##############################################################################################
	// ÆÄÀÏ Á¤º¸¸¦ »èÁ¦ÇÑ´Ù.
	DelBetaBoardFileList : function  (){
		var fileListObj = $(this._selectName);

		if ( fileListObj.selectedIndex == -1 ) {
			alert("¼±ÅÃµÈ ÆÄÀÏÀÌ ¾ø½À´Ï´Ù.");
			return false;
		}
		
		if ( this.checkEditorImage() ) {
			alert("¿¡µðÅÍ¿¡¼­ »ç¿ëÇÏ´Â Ã·ºÎÆÄÀÏÀº »ç¿ëÇÒ¼ö ¾ø½À´Ï´Ù.");
			return false;
		}

		if ( !$(confirm("ÆÄÀÏÀ» »èÁ¦ÇÏ½Ã°Ú½À´Ï±î?")) ) {
			return false;
		}

		$("file_name").value = fileListObj[fileListObj.selectedIndex].value;
		// debug ( fileListObj[fileListObj.selectedIndex].value );

		var ajax_url 	= "/common/deleteFile.asp";
		// var pars 		= Form.serialize($(this._uploadform), 'NS');
		var pars 		= "?file_name="+$("file_name").value;
		pars += "&page_gbn="+$("page_gbn").value;

		if ( $("board_seqnum") ) {
			pars += "&board_seqnum="+$("board_seqnum").value;
		}

		if ( $("seqnum") ) {
			pars += "&seqnum="+$("seqnum").value;
		}
		// debug ( this._uploadform );

		var		myAjax	= new Ajax.Request(ajax_url, {
			method: 		'post',
			parameters:		pars,
			onComplete:		p_Attach.DelBetaBoardFileListComplete
			}
		);
	},
	// 2008-08-28 1:53¿ÀÈÄ
	// haroc
	// ##############################################################################################
	// ¿¡µðÅÍ¿¡ ÀÖ´Â ÀÌ¹ÌÁöÀÎÁö È®ÀÎÇÑ´Ù. 
	checkEditorImage : function () {
		return false;
	}, 
	// 2008-07-30 5:49¿ÀÈÄ
	// haroc
	// ##############################################################################################
	// ÆÄÀÏÀÌ »èÁ¦µÈÈÄ
	DelBetaBoardFileListComplete : function (originalRequest){
		//debug(originalRequest.responseText);
		if ( 'OK' != originalRequest.responseText ) {
			alert("ÆÄÀÏÀÌ Á¤»óÀûÀ¸·Î »èÁ¦µÇÁö ¾Ê¾Ò½À´Ï´Ù.");
			debug (originalRequest.responseText);
			return false;
		}
		p_Attach.DelOption(this._selectName);
		$("thumbnail_preview").src = "/images/common/preview.jpg";
	},
	// 2008-07-31 10:27¿ÀÀü
	// haroc
	// ##############################################################################################
	// Input typeÀÌ fileÀÇ value°ªÀ» ÃÊ±âÈ­ÇÑ´Ù.
	// ¹®Á¦Á¡.. Ã¹¹øÂ° ¿Ã¸° ÆÄÀÏÀº ÃÊ±âÈ­½ÃÄÑÁÖ³ª 2¹øÂ° ÆÄÀÏÀ» ¾÷·ÎµåÇÏ·Á°í ÇÏ¸é ¿¡·¯°¡ ³­´Ù.
	dellFileTypeInputValue : function (){
		var vInputList = document.getElementsByTagName("input");
		for (var i=0; i < vInputList.length ;i++ ) {
			if(vInputList[i].type == "file") {
				vInputList[i].select();
				// ¾Æ·¡ ¹æ¹ý 2ÁßÀÇ ÇÏ³ª¸¦ »ç¿ëÇÏ¸é µÈ´Ù.
				// document.selection.clear();
				document.execCommand('Delete');
			}
		}
	},
	// 2008-08-01 5:19¿ÀÈÄ
	// haroc
	// ##############################################################################################
	// ÆÄÀÏÁ¤º¸¸¦ select¿¡ Ãâ·ÂÇÑ ·¹ÀÌºí ÇüÅÂ·Î ¸¸µé¾î ÁØ´Ù.
	getFileLabel : function (select_value) {
		var fileLabel 	= "["+p_Attach.getTitle(select_value)+"]";
		fileLabel += p_Attach.getOriginalFileName(select_value)+"(";
		fileLabel += p_Attach.getFileSize(select_value)+"KB)";
		return fileLabel;
	},
	// 2008-08-01 5:28¿ÀÈÄ
	// haroc
	// ##############################################################################################
	// ÆÄÀÏÁ¤º¸¸¦ p_Attach.ATTACH_INFO_PARTITIONÀ» ³Ö¾î¼­ ¸¸µé¾î ÁØ´Ù.
	getFileNameWithPartition : function (opt) {
		// ¿ø·¡ ÆÄÀÏ¸í / ÀúÀåµÈ ÆÄÀÏ¸í / ÆÄÀÏ»çÀÌÁî(KB) / ÀÏ·Ã¹øÈ£(¾øÀ¸¸é 0) / THUMBNAIL À¯¹« / Ã·ºÎÆÄÀÏ Å¸ÀÌÆ²
		var file_name = opt.orgFile;
		file_name += p_Attach.ATTACH_INFO_PARTITION + opt.realFile;
		file_name += p_Attach.ATTACH_INFO_PARTITION + opt.fileSize;
		file_name += p_Attach.ATTACH_INFO_PARTITION + '0';
		file_name += p_Attach.ATTACH_INFO_PARTITION + 'EDITOR';
		file_name += p_Attach.ATTACH_INFO_PARTITION + 'Å¸ÀÌÆ²À» ¼³Á¤ÇÏ¼¼¿ä.';


		return file_name;
	},
	// 2008-08-05 10:58¿ÀÀü
	// haroc
	// ##############################################################################################
	// Select Value¸¦ °¡Á®¿Â´Ù.
	getSelectValue : function () {
		if ( $(this._selectName).selectedIndex > -1 ) {
			return $(this._selectName)[$(this._selectName).selectedIndex].value;
		}
		return '';
	},
	// 2008-08-05 11:15¿ÀÀü
	// haroc
	// ##############################################################################################
	// select to thumbnail image
	select2thumbnail : function () {
		var vSelectValue = p_Attach.getSelectValue();
		//debug (vSelectValue);
		if ( vSelectValue ) {
			var vSavedFileName = p_Attach.getSavedFileName(vSelectValue);
			//debug ( vSavedFileName );
			var vFileExt = getFileExtension(vSavedFileName);
			//debug ( getFileExtension(vSavedFileName) );
			var vImageName;
			if ( 'jpg' == vFileExt ) {
				vImageName = $(this._url_folder).value + vSavedFileName;
			}
			else {
				vImageName = this._image_name;
			}
			$(this._thumbnailPreview).src 		= vImageName;
		}
	},
	// 2008-08-05 1:39¿ÀÈÄ
	// haroc
	// ##############################################################################################
	// set Thumbnail Image
	setThumbnailImage : function () {
		var vSelectValue = p_Attach.getSelectValue();
		//debug (vSelectValue);
		if ( vSelectValue ) {
			var vSavedFileName = p_Attach.getSavedFileName(vSelectValue);
			//debug ( vSavedFileName );
			var vFileExt = getFileExtension(vSavedFileName);
			//debug ( getFileExtension(vSavedFileName) );
			if ( 'jpg' == vFileExt ) {
				//debug (vSavedFileName);
				$(this._thumbnail_filename).value = vSavedFileName;
				//debug ( $(this._thumbnail_filename).value );
				if ( $(this._selectName).selectedIndex > -1 ) {
					$(this._selectName)[$(this._selectName).selectedIndex].value = $(this._selectName)[$(this._selectName).selectedIndex].value.replace("UPLOAD", "THUMB");
				}
			}
			else {
				alert("ThumbnailÀ» ÁöÁ¤ÇÒ¼ö ¾ø´Â È®ÀåÀÚ ÀÔ´Ï´Ù.");
			}
		}
	},
	// 2008-08-05 4:10¿ÀÈÄ
	// haroc
	// ##############################################################################################
	// Attach TitleÀ» text·Î º¸³½´Ù.
	attach_title2text : function () {
		p_Attach.select2thumbnail();
		var vTitle = p_Attach.getTitle();
		//debug ( vTitle );
		$(this._attachTitle).value = vTitle;
		$(this._attachTitle).setAttribute("selectedindex", $(this._selectName).selectedIndex);
		//debug ( $(this._attachTitle).getAttribute("selectedindex") ) ;
	},
	// 2008-08-06 2:50¿ÀÈÄ
	// haroc
	// ##############################################################################################
	// changeAttachTitle
	changeAttachTitle : function () {
		var vAttachTitle = $(this._attachTitle).value;
		this._selectedindex = $(this._attachTitle).getAttribute("selectedindex");

		if ( '' == vAttachTitle+"" ) {
			return false;
		}

		if ( null == this._selectedindex ) {
			return false;
		}

		if ( "" == this._selectedindex+"" ) {
			return false;
		}

		if ( vAttachTitle.indexOf(p_Attach.ATTACH_INFO_PARTITION) > -1 || vAttachTitle.indexOf(";") > -1) {
			alert("Å¸ÀÌÆ²¿¡ µé¾î°¥¼ö ¾ø´Â ¹®ÀÚ¿­ÀÌ ÀÖ½À´Ï´Ù. ´Ù½Ã ÁöÁ¤ÇÏ½Ê½Ã¿À.");
			return false;
		}

		this._optionvalue = $(this._selectName)[this._selectedindex].value;
		//debug (this._optionvalue);
		var vList = this._optionvalue.split(p_Attach.ATTACH_INFO_PARTITION);
		vList[5] = vAttachTitle;
		p_Attach.setSelectValue(vList);
	},
	// 2008-08-05 4:29¿ÀÈÄ
	// haroc
	// ##############################################################################################
	// select value settingÇÑ´Ù.
	setSelectValue : function (vList) {
		var vNewValue = '';
		if ( '' == this._selectedindex+"" || null == this._selectedindex || -1 == this._selectedindex) return false;

		for ( var i = 0 ; i < vList.length ; i++ ) {
			if ( 0 == i ) {
				vNewValue = vList[i];
			}
			else {
				vNewValue += p_Attach.ATTACH_INFO_PARTITION + vList[i];
			}
		}
		//debug (this._selectedindex);
		$(this._selectName)[this._selectedindex].value = vNewValue;
		var fileLabel = p_Attach.getFileLabel(vNewValue);
		//debug (fileLabel);
		$(this._selectName)[this._selectedindex].innerHTML = fileLabel;

		//debug ($(this._selectName)[this._selectedindex].value);
	},
	// 2008-08-05 4:55¿ÀÈÄ
	// haroc
	// ##############################################################################################
	// 2008-07-30 10:35¿ÀÀü
	// ÆÄÀÏ ¾÷·Îµå ÀúÀå ÇÔ¼ö
	// ÆäÀÌÁö¸¶´Ù ´Ù¸¦¼ö ÀÖÀ¸¹Ç·Î ÆäÀÌÁöº°·Î ¸¸µå´Â ÇÔ¼ö.
	FileUpload : function (frmName, obj) {
		var vUploadCnt = eval( $("upload_cnt").value ) ;
		//debug (vUploadCnt);
		if ( eval(vUploadCnt) > 0 ) {
			var vLen = eval($(this._selectName).length);
			//debug (vLen);
			if ( eval(vLen) >= eval(vUploadCnt) ) {
				var vMsg = "¾÷·Îµå ÆÄÀÏ¼ö´Â "+vUploadCnt+"°³ ±îÁö ÀÔ´Ï´Ù.\n\n´õÀÌ»ó ÆÄÀÏÀ» Ã·ºÎÇÒ¼ö ¾ø½À´Ï´Ù.";
				p_Attach.initFileInput(this._file_span_name, this._uploadform);
				alert(vMsg);
				return false;
			}
		}

		if ( !confirm("¼±ÅÃÇÑ ÆÄÀÏÀ» ¾÷·ÎµåÇÏ½Ã°Ú½À´Ï±î?") ) {
			return false;
		}

		// ÇöÀç Select¹Ú½º¿¡ µ¿ÀÏÇÑ ÀÌ¸§ÀÌ ÀÖ´ÂÁö Ã£¾Æº»´Ù.
		if ( p_Attach.searchFileList(obj.value) ) {
			alert("ÀÌ¹Ì ¾÷·ÎµåµÈ ÆÄÀÏÀÔ´Ï´Ù.");
			p_Attach.initFileInput(this._file_span_name, this._uploadform);
			return false;
		}

		this.setUploadForm(frmName);

		// debug (frmName);
		document.forms[frmName].target		= this._target;
		document.forms[frmName].action 		= this._action;
		document.forms[frmName].encoding	= this._encoding;
		document.forms[frmName].submit();
	}, 
	// 2008-08-08 11:08¿ÀÀü
	// haroc
	// ##############################################################################################
	// uploadµÉ ÆÄÀÏ ¸®½ºÆ®¸¦ °¡Á®¿Â´Ù.
	addUploadFileList : function () {
		var vFileList = $(this._selectName);
		var vList = '';

		for ( var i = 0 ; i < vFileList.length ; i++ ) {
			if ( 0 == i ) {
				vList = vFileList[i].value;
			}
			else {
				vList += this.ATTACH_FILE_PARTITION + vFileList[i].value;
			}
		}
		
		$(this._uploadfilelist).value = vList;
	}
}

// 2008-04-25 5:11¿ÀÈÄ
// haroc
// ##############################################################################################
// Ã·ºÎÆÄÀÏÀÌ ÀÖ´Â °æ¿ì¿¡..
Event.observe(window, 'load', function() {
	// FCKEditor¿¡ ÀÖ´Â ÀÌ¹ÌÁö¸¦ Á¤±âÀûÀ¸·Î select·Î º¹»çÇÑ´Ù.
	// board°Ô½ÃÆÇ¿¡¼­¸¸ »ç¿ë
	p_Attach.autoFill();

	loadBoardConfig();

	if ( $("thumbnail_preview") ) {
		$("thumbnail_preview").onload = function () {
			resizeImg(this, 120, 90);
		}
	}


	// ÀúÀå¹öÆ° Å¬¸¯
	if ( $("BetaBoardFileList") ) {
		Event.observe("BetaBoardFileList", 'click', function() {
			p_Attach.attach_title2text();
		});
	}

	// Thumbnail¼³Á¤ ¹öÆ° Å¬¸¯
	if ( $("btnsetThumbnailImage") ) {
		$("btnsetThumbnailImage").onclick = function () {
			p_Attach.setThumbnailImage();
		}
	}

	// Å¸ÀÌÆ² ¼³Á¤ ÅØ½ºÆ® ÃÊÁ¡À» ÀÒ¾î¹ö·ÈÀ»¶§
	if ( $("attach_title") ) {
		$("attach_title").onblur = function () {
			p_Attach.changeAttachTitle($("attach_title").value);
		}
	}
});