Element.extend({
	/** 글쓰기 매크로용 내용치환
	 * @class Element 
	 * @param
			$head: 머리말
			$foot: 꼬리말
	 */
	replaceSel: function (head, foot) {
		var rng;
		var v;
		if (!$chk(head)) head = '';
		if (!$chk(foot)) foot = '';

		if (window.ie) {
			rng = document.selection.createRange();

			if (rng.text) {
				rng.text = head + rng.text + foot;
				rng.select();
			}
			else if (!$chk(rng.text) && $chk(this.caret)) {
				this.caret.text = head + foot;
				this.caret.select();
			}				
			else {
				this.value += head + foot;
			}
		}

		else {
			//+ 아르고에서 매크로가 제일 처음에 삽입됨.
			v = this.value;
			var start = this.selectionStart;
			var end = this.selectionEnd;

			this.value = v.substring(0, this.selectionStart) + head + v.substring(this.selectionStart, this.selectionEnd) + foot + v.substring(this.selectionEnd, v.length);
			
//			if (!head || !foot || start == end) {
//				start = start + head.toString().length + foot.toString().length;
//				this.setSelectionRange(start, start);
//			}
//			else {
				end = end + head.toString().length + foot.toString().length;
				this.setSelectionRange(end, end);
//			}			
		}
	}
});

//// range 저장, 복구
var saveRange = new Class({
	start: 0,
	end: 0,
	rng: {},

	initialize: function (obj) {
		this.obj = obj;

		if (window.ie) {
			this.rng = document.selection.createRange();
		}

		else {
			this.start = this.obj.selectionStart;
			this.end = this.obj.selectionEnd;
		}
	},

	restore: function () {
		var size = window.getSize();
		this.obj.focus();
		
		if (window.ie) {
			this.rng.select();
		}

		else {
			this.obj.setSelectionRange(this.start, this.end);
		}

		window.scrollTo(size.scroll.x, size.scroll.y);
	}
});

var iiEditorIndex = 0;
var iiEditorText = new Class({
	/** 초기화
	 * @class iiEditorText
	 * @param
			-obj: 대상 DOM object
			-position: 툴바 위치 [!top|bottom]
			-skin: 스킨 이름 [!default]
			-toolbar: 툴바 배치. $기능명 형식으로 배치하면 됩니다
	 */
	initialize: function (setting) {
		// 설정 초기화
		if (!$chk(setting)) setting = {};
		if (!$chk(setting['obj'])) { error('iiEditor 초기화 실패: 대상이 없습니다'); return false; }
		if (!$chk(setting['position'])) setting['position'] = 'top';
		if (!$chk(setting['skin'])) setting['skin'] = 'default';
		if (!$chk(setting['height'])) setting['height'] = '';
		if (!$chk(setting['width'])) setting['width'] = '';
		if (!$chk(setting['toolbar'])) setting['toolbar'] = '$font$style$bold$italic$underline$strike$upper$lower $highlight$box$code$fold$page $link$image$slide$movie$music$preview';
		this.sdir = miniDir + 'addon/iiEditor/skin/default/';
		this.setting = setting;
		this.index = iiEditorIndex;
		iiEditorIndex++;

		// 툴바 삽입
		this.obj = setting['obj'];
		this.form = this.obj.form;
		this.toolbar = new Element('div', {
			'styles': {
			}
		});
		
		if (!setting['toolbar'].toString().match(/\$([a-z0-9]+)/i)) { error('iiEditor 초기화 실패: 툴바 설정이 올바르지 않습니다'); return false; }
		setting['toolbar'] = setting['toolbar'].toString().replace(/\$([a-z0-9]+)/ig, "<img src='" + this.sdir + "b_$1.gif' border='0' alt='$1' style='vertical-align:middle; cursor:pointer' class='iiEditor_button' />");

		this.toolbar.innerHTML = setting['toolbar'];

		switch (setting['position']) {
			case 'top':
				this.toolbar.injectBefore(this.obj);
				break;
			case 'bottom':
				this.toolbar.injectAfter(this.obj);
				break;
		}

		this.toolbar.getChildren().each(function (item) {
			if (item.nodeName.toString().match(/img/i) && item.hasClass('iiEditor_button')) {
				var s = item.src.toString().match(/_([a-z0-9]+)\.gif$/i)[1];
				item.addEvent('click', (function (e) {
					this.action(s, new Event(e));
				}).bind(this));
			}
		}, this);

		// range 문제 해결
		this.caret = null;
		
		this.obj.addEvents({
			'keyup': this.saveCaret,
			'select': this.saveCaret,
			'click': this.saveCaret
		});

		// 단축키 지정
		//+ opera 에서 다른 동작이 되는 버그가 있음
		//+ w,o,
		this.obj.addEvent('keydown', (function (e) {
			var event = setEvent(e);
			if ($chk(event.control) && !$chk(event.alt) && !$chk(event.shift) && $chk(event.code)) {
				if (event.key.toString().match(/[^a-z0-9]/i)) event.key = event.code;
				switch (event.key) {
					case 190:
						new Event(e).stop();
						this.action('indent', event);
						break;
					case 'b':
						new Event(e).stop();
						this.action('bold', event);
						break;
					case 'i':
						new Event(e).stop();
						this.action('italic', event);
						break;
					case 'u':
						new Event(e).stop();
						this.action('underline', event);
						break;
					case 192:
						new Event(e).stop();
						this.action('preview', event);
						break;
					case 'h':
						new Event(e).stop();
						this.action('highlight', event);
						break;
					case 191:
						new Event(e).stop();
						this.action('font', event);
						break;
					case 222:
						new Event(e).stop();
						this.action('box', event);
						break;
					case 219:
						new Event(e).stop();
						this.action('code', event);
						break;
					case 221:
						new Event(e).stop();
						this.action('fold', event);
						break;
					case 220:
						new Event(e).stop();
						this.action('page', event);
						break;
					case 186:
						new Event(e).stop();
						this.action('link', event);
						break;
					case '0':
						new Event(e).stop();
						this.action('style', event);
						break;
					case '1':
						new Event(e).stop();
						this.obj.replaceSel("[h1]", "[/h1]");
						break;
					case '2':
						new Event(e).stop();
						this.obj.replaceSel("[h2]", "[/h2]");
						break;
					case '3':
						new Event(e).stop();
						this.obj.replaceSel("[h3]", "[/h3]");
						break;
					case '4':
						new Event(e).stop();
						this.obj.replaceSel("[h4]", "[/h4]");
						break;
					case '5':
						new Event(e).stop();
						this.obj.replaceSel("[h5]", "[/h5]");
						break;
					case '6':
						new Event(e).stop();
						this.obj.replaceSel("[h6]", "[/h6]");
						break;
//					default: error(event.key);
				}
			}
		}).bind(this));
	},

	/** 메뉴 레이어 제거
	 * @class iiEditorText
	 */
	close: function () {
		if ($chk(this.div)) {
			this.div.remove();
			this.div = null;
		}
	},


	/** 메뉴 레이어 생성
	 * @class iiEditorText
	 * @param
			$mode: mode
			$e: 넘겨받은 event
			$ment: 들어갈 HTML
	 */
	createDialog: function (mode, e, ment) {
		var pos = $chk(e.target) ? e.target.getCoordinates() : this.obj.getCoordinates();
		var pos2 = this.obj.getCoordinates();
		
		var div = new Element('div', {
			styles: {
				position: 'absolute',
				left: (pos.left + 200 >= pos2.right ? pos2.right - 200: pos.left),
				top: ($chk(e.target) && !e.target.isTag('textarea') ? pos.bottom : pos.top),
				width: '200px',
				backgroundColor: '#fff',
				border: '10px solid #545454',
				padding: '7px',
				zIndex: '100'
			}
		});

		this.div = div;

		var content = new Element('div', {
			styles: {
				clear: 'both',
				paddingBottom: '7px',
				marginBottom: '7px',
				font: '11px dotum',
				lineHeight: '1.5'
			}
		}).inject(div);
		
		content.innerHTML = ment;

		var close = new Element('div', {
			styles: {
				'float': 'left',
				font: '11px dotum',
				cursor: 'pointer'
			},
			events: {
				click: this.close.bind(this)
			}
		}).setHTML('닫기').inject(div);

		var ok = new Element('div', {
			styles: {
				'float': 'right',
				font: '11px dotum',
				cursor: 'pointer'
			},
			events: {
				click: (function () {
					this.rng.restore();
					var v1 = '';
					var v2 = '';

					switch (mode) {
						case 'code':
							v1 = $('iiEditor' + this.index + '_tmp_code_mode').getValue();
							v2 = $('iiEditor' + this.index + '_tmp_code_startLine').getValue();

							this.obj.replaceSel("[code:" + v1 + (v2 ? ", startLine:" + v2 : "") + "]", "[/code]");
							break;

						case 'font':
							v1 = $('iiEditor' + this.index + '_tmp_font').getValue();
							v2 = $('iiEditor' + this.index + '_tmp_font_size').getValue();

							this.obj.replaceSel("[font:" + v1 + (v2 ? ", size:" + v2 : "") + "]", "[/font]");
							break;

						case 'style':
							if ($('iiEditor' + this.index + '_tmp_style1').checked == true) v1 = 'h1'; 
							if ($('iiEditor' + this.index + '_tmp_style2').checked == true) v1 = 'h2';
							if ($('iiEditor' + this.index + '_tmp_style3').checked == true) v1 = 'h3';
							if ($('iiEditor' + this.index + '_tmp_style4').checked == true) v1 = 'h4';
							if ($('iiEditor' + this.index + '_tmp_style5').checked == true) v1 = 'h5';
							if ($('iiEditor' + this.index + '_tmp_style6').checked == true) v1 = 'h6';

							this.obj.replaceSel("[" + v1 + "]", "[/" + v1 + "]");
							break;

						case 'box':
							if ($('iiEditor' + this.index + '_tmp_box1').checked == true) v1 = '1'; 
							if ($('iiEditor' + this.index + '_tmp_box2').checked == true) v1 = '2';
							if ($('iiEditor' + this.index + '_tmp_box3').checked == true) v1 = '3';
							if ($('iiEditor' + this.index + '_tmp_box4').checked == true) v1 = '4';
							if ($('iiEditor' + this.index + '_tmp_box5').checked == true) v1 = '5';

							this.obj.replaceSel("[box:" + v1 + "]", "[/box]");
							break;

						case 'fold':
							v1 = $('iiEditor' + this.index + '_tmp_fold').getValue();

							this.obj.replaceSel("[fold:" + v1 + "]", "[/fold]");
							break;

						case 'page':
							v1 = $('iiEditor' + this.index + '_tmp_page').getValue();
							v2 = $('iiEditor' + this.index + '_tmp_page_list').getValue();

							if (!v2) 
								this.obj.replaceSel("[list:" + v1 + "]", "[/list]");
							else
								this.obj.replaceSel("", "[printList]");
							break;

						case 'link':
							v1 = $('iiEditor' + this.index + '_tmp_link').getValue();
							if (!v1.toString().match(/^[a-z0-9]+\:\/\//i)) v1 = "http://" + v1;

							this.obj.replaceSel("[a:" + v1 + "]", "[/a]");
							break;

						case 'image':
							v1 = $('iiEditor' + this.index + '_tmp_image').getValue();

							this.obj.replaceSel("", "[img:" + v1 + "]");
							break;

						case 'slide':
							v1 = $('iiEditor' + this.index + '_tmp_slide').getValue();
							v1 = v1.toString().replace(/[\r\n]+/g, '|');

							this.obj.replaceSel("", "[slide:" + v1 + "]");
							break;

						case 'movie':
							v1 = $('iiEditor' + this.index + '_tmp_movie').getValue();
							v1 = v1.toString().replace(/[\r\n]+/g, '|');

							this.obj.replaceSel("", "[flv:" + v1 + "]");
							break;

						case 'music':
							v1 = $('iiEditor' + this.index + '_tmp_music').getValue();
							v1 = v1.toString().replace(/[\r\n]+/g, '|');

							this.obj.replaceSel("", "[music:" + v1 + "]");
							break;
					}

					this.close();
				}).bind(this)
			}
		}).setHTML('확인').inject(div);
		div.inject(document.body);

		// 포커스
		var focusEvent = (function (e) {
			var event = setEvent(e);

			if ($chk(event.key) && event.key == 'enter') {
				new Event(e).stop();
				ok.fireEvent('click');
			}
			if ($chk(event.key) && event.key == 'esc') {
				new Event(e).stop();
				close.fireEvent('click');
				this.obj.focus();
			}
		}).bind(this);

		// 포커스 이벤트 적용
		$$('input[id^=iiEditor' + this.index + '_tmp_]').each(function (item) {
			item.addEvent('keydown', focusEvent);
		});
		$$('select[id^=iiEditor' + this.index + '_tmp_]').each(function (item) {
			item.addEvent('keydown', focusEvent);
		});

		// 포커스 적용
		switch (mode) {
			case 'box':
				$('iiEditor' + this.index + '_tmp_box1').focus();
				break;

			case 'style':
				$('iiEditor' + this.index + '_tmp_style1').focus();
				break;

			case 'code':
				$('iiEditor' + this.index + '_tmp_code_mode').focus();
				break;

			case 'font':
			case 'page':
			case 'fold':
			case 'image':
			case 'slide':
			case 'movie':
			case 'music':
			case 'link':
				//+ IE 오류 떄문에 focus 2번 잡아줌
				$('iiEditor' + this.index + '_tmp_' + mode).focus();
				$('iiEditor' + this.index + '_tmp_' + mode).focus();
				break;
		}
	},


	/** 행동
	 * @class iiEditorText
	 * @param
			$mode: mode
			$e: event
	 */
	action: function (mode, e) {
		var ment = '';
		var line = "<tr><td height='1' colspan='2' bgcolor='#e1e1e1'></td></tr>";
		this.rng = new saveRange(this.obj);
		if (!$chk(this.rng.text)) this.saveCaret();
		this.close();

		switch (mode) {
			case 'indent':				
				this.obj.replaceSel("	", "");
				break;

			case 'bold':
				this.obj.replaceSel("[b]", "[/b]");
				break;

			case 'italic':
				this.obj.replaceSel("[i]", "[/i]");
				break;

			case 'underline':
				this.obj.replaceSel("[u]", "[/u]");
				break;

			case 'strike':
				this.obj.replaceSel("[strike]", "[/strike]");
				break;

			case 'upper':
				this.obj.replaceSel("[sup]", "[/sup]");
				break;

			case 'lower':
				this.obj.replaceSel("[sub]", "[/sub]");
				break;

			case 'highlight':
				this.obj.replaceSel("[h]", "[/h]");
				break;

			case 'font':
				ment += "<table border='0' cellpadding='0' cellspacing='0' width='100%'>"
					+ "<tr><td colspan='2' style='padding:5px 0; font-weight:bold;'>글꼴선택</td></tr>"
					+ "<tr><td colspan='2' style='padding:5px 0; font:11px dotum; color:#545454; line-height:1.5;'>글꼴의 종류와 크기를 선택합니다. 사용자의 컴퓨터에 설치되어있지 않은 폰트는 굴림이나 돋움으로 보일 수 있습니다.</td></tr>"
					+ line
					+ "<tr>"
						+ "<td style='padding:3px 0; font:11px dotum;' nowrap='nowrap'>종류</td>"
						+ "<td style='padding:3px 0; font:11px dotum;'><select id='iiEditor" + this.index + "_tmp_font' class='formSelect'>"
							+ "<option value='gulim' style='font-family:gulim;'>굴림</option>"
							+ "<option value='dotum' style='font-family:dotum;'>돋움</option>"
							+ "<option value='gungsuh' style='font-family:gungsuh;'>궁서</option>"
							+ "<option value='malgun gothic' style='font-family:\"malgun gothic\";'>맑은고딕</option>	"
							+ "<option value='Arial' style='font-family:Arial;'>Arial</option>"
							+ "<option value='Calibri' style='font-family:Calibri;'>Calibri</option>"
						+ "</select></td>"
					+ "</tr>"
					+ line
					+ "<tr>"
						+ "<td style='padding:3px 0; font:11px dotum;' nowrap='nowrap'>크기</td>"
						+ "<td style='padding:3px 0; font:11px dotum;'><select id='iiEditor" + this.index + "_tmp_font_size' class='formSelect'>"
							+ "<option value='10px'>10px</option>"
							+ "<option value='11px'>11px</option>"
							+ "<option value='12px' selected='selected'>12px</option>"
							+ "<option value='14px'>14px</option>"
							+ "<option value='18px'>18px</option>"
							+ "<option value='20px'>20px</option>"
							+ "<option value='25px'>25px</option>"
							+ "<option value='30px'>30px</option>"
							+ "<option value='50px'>50px</option>"
						+ "</select></td>"
					+ "</tr>"
					+ line
					+ "</table>";
				break;

			case 'style':
				ment += "<table border='0' cellpadding='0' cellspacing='0' width='100%'>"
					+ "<tr><td colspan='2' style='padding:5px 0; font-weight:bold;'>문단 스타일선택</td></tr>"
					+ line
					+ "<tr>"
						+ "<td style='padding:3px 0; cursor:pointer' nowrap='nowrap' colspan='2' onclick='$(\"iiEditor" + this.index + "_tmp_style1\").checked = true;'><h1 style='margin:3px;'><input type='radio' name='tmp_style' value='h1' id='iiEditor" + this.index + "_tmp_style1' class='middle' checked='checked' /><label for='iiEditor" + this.index + "_tmp_style1'>1. 첫번째 Abc</label></h1></td>"
					+ "</tr>"
					+ line
					+ "<tr>"
						+ "<td style='padding:3px 0; cursor:pointer' nowrap='nowrap' colspan='2' onclick='$(\"iiEditor" + this.index + "_tmp_style2\").checked = true;'><h2 style='margin:3px;'><input type='radio' name='tmp_style' value='h2' id='iiEditor" + this.index + "_tmp_style2' class='middle' /><label for='iiEditor" + this.index + "_tmp_style2'>2. 두번째 Abc</label></h2></td>"
					+ "</tr>"
					+ line
					+ "<tr>"
						+ "<td style='padding:3px 0; cursor:pointer' nowrap='nowrap' colspan='2' onclick='$(\"iiEditor" + this.index + "_tmp_style3\").checked = true;'><h3 style='margin:3px;'><input type='radio' name='tmp_style' value='h3' id='iiEditor" + this.index + "_tmp_style3' class='middle' /><label for='iiEditor" + this.index + "_tmp_style3'>3. 세번째 Abc</label></h3></td>"
					+ "</tr>"
					+ line
					+ "<tr>"
						+ "<td style='padding:3px 0; cursor:pointer' nowrap='nowrap' colspan='2' onclick='$(\"iiEditor" + this.index + "_tmp_style4\").checked = true;'><h4 style='margin:3px;'><input type='radio' name='tmp_style' value='h4' id='iiEditor" + this.index + "_tmp_style4' class='middle' /><label for='iiEditor" + this.index + "_tmp_style4'>4. 네번째 Abc</label></h4></td>"
					+ "</tr>"
					+ line
					+ "<tr>"
						+ "<td style='padding:3px 0; cursor:pointer' nowrap='nowrap' colspan='2' onclick='$(\"iiEditor" + this.index + "_tmp_style5\").checked = true;'><h5 style='margin:3px;'><input type='radio' name='tmp_style' value='h5' id='iiEditor" + this.index + "_tmp_style5' class='middle' /><label for='iiEditor" + this.index + "_tmp_style5'>5. 다섯번째 Abc</label></h5></td>"
					+ "</tr>"
					+ line
					+ "<tr>"
						+ "<td style='padding:3px 0; cursor:pointer' nowrap='nowrap' colspan='2' onclick='$(\"iiEditor" + this.index + "_tmp_style6\").checked = true;'><h6 style='margin:3px;'><input type='radio' name='tmp_style' value='h6' id='iiEditor" + this.index + "_tmp_style6' class='middle' /><label for='iiEditor" + this.index + "_tmp_style6'>6. 여섯번째 Abc</label></h6></td>"
					+ "</tr>"
					+ line
					+ "</table>";
				break;

			case 'box':
				ment += "<table border='0' cellpadding='0' cellspacing='0' width='100%'>"
					+ "<tr><td colspan='2' style='padding:5px 0; font-weight:bold;'>글 상자 넣기</td></tr>"
					+ line
					+ "<tr>"
						+ "<td style='padding:3px 0; cursor:pointer' nowrap='nowrap' colspan='2'>"
							+ "<div class='iiBox_1' style='margin-bottom:5px;' onclick='$(\"iiEditor" + this.index + "_tmp_box1\").checked = true;'><input type='radio' name='tmp_box' value='1' id='iiEditor" + this.index + "_tmp_box1' class='middle' checked='checked' />1. 글상자 Box</div>"
							+ "<div class='iiBox_2' style='margin-bottom:5px;' onclick='$(\"iiEditor" + this.index + "_tmp_box2\").checked = true;'><input type='radio' name='tmp_box' value='2' id='iiEditor" + this.index + "_tmp_box2' class='middle' />2. 글상자 Box</div>"
							+ "<div class='iiBox_3' style='margin-bottom:5px;' onclick='$(\"iiEditor" + this.index + "_tmp_box3\").checked = true;'><input type='radio' name='tmp_box' value='3' id='iiEditor" + this.index + "_tmp_box3' class='middle' />3. 글상자 Box</div>"
							+ "<div class='iiBox_4' style='margin-bottom:5px;' onclick='$(\"iiEditor" + this.index + "_tmp_box4\").checked = true;'><input type='radio' name='tmp_box' value='4' id='iiEditor" + this.index + "_tmp_box4' class='middle' />4. 글상자 Box</div>"
							+ "<div class='iiBox_5' style='margin-bottom:5px;' onclick='$(\"iiEditor" + this.index + "_tmp_box5\").checked = true;'><input type='radio' name='tmp_box' value='5' id='iiEditor" + this.index + "_tmp_box5' class='middle' />5. 글상자 Box</div>"
						+ "</td>"
					+ "</tr>"
					+ line
					+ "</table>";
				break;

			case 'code':
				ment += "<table border='0' cellpadding='0' cellspacing='0' width='100%'>"
					+ "<tr><td colspan='2' style='padding:5px 0; font-weight:bold;'>코드 넣기</td></tr>"
					+ "<tr><td colspan='2' style='padding:5px 0; font:11px dotum; color:#545454; line-height:1.5;'>프로그램 소스를 삽입할 때 사용합니다. 소스를 구문별로 색깔을 사용하여 강조합니다. 코드안에 매크로롤 넣을 수 없습니다.</td></tr>"
					+ line
					+ "<tr>"
						+ "<td style='padding:3px 0; font:11px dotum;' nowrap='nowrap'>종류</td>"
						+ "<td style='padding:3px 0; font:11px dotum;'><select id='iiEditor" + this.index + "_tmp_code_mode' class='formSelect'>"
							+ ($chk(editorSyntax_rule) && editorSyntax_rule.toString().match(/cpp/i) ? "<option value='cpp'>C++, C</option>" : "")
							+ ($chk(editorSyntax_rule) && editorSyntax_rule.toString().match(/csharp/i) ? "<option value='csharp'>C#</option>" : "")
							+ ($chk(editorSyntax_rule) && editorSyntax_rule.toString().match(/css/i) ? "<option value='css'>Css</option>" : "")
							+ ($chk(editorSyntax_rule) && editorSyntax_rule.toString().match(/delphi/i) ? "<option value='delphi'>Delphi</option>" : "")
							+ ($chk(editorSyntax_rule) && editorSyntax_rule.toString().match(/java/i) ? "<option value='java'>Java</option>" : "")
							+ ($chk(editorSyntax_rule) && editorSyntax_rule.toString().match(/jscript/i) ? "<option value='javascript'>Javascript</option>" : "")
							+ ($chk(editorSyntax_rule) && editorSyntax_rule.toString().match(/php/i) ? "<option value='php'>PHP</option>" : "")
							+ ($chk(editorSyntax_rule) && editorSyntax_rule.toString().match(/python/i) ? "<option value='python'>Python</option>" : "")
							+ ($chk(editorSyntax_rule) && editorSyntax_rule.toString().match(/ruby/i) ? "<option value='ruby'>Ruby</option>" : "")
							+ ($chk(editorSyntax_rule) && editorSyntax_rule.toString().match(/sql/i) ? "<option value='sql'>Sql</option>" : "")
							+ ($chk(editorSyntax_rule) && editorSyntax_rule.toString().match(/vb/i) ? "<option value='vb'>VB</option>" : "")
						+ "</select></td>"
					+ "</tr>"
					+ line
					+ "<tr>"
						+ "<td style='padding:3px 0; font:11px dotum;' nowrap='nowrap'>시작줄번호</td>"
						+ "<td style='padding:3px 0; font:11px dotum;'><input type='text' id='iiEditor" + this.index + "_tmp_code_startLine' class='formText' size='5' /></td>"
					+ "</tr>"
					+ line
					+ "</table>";
				break;

			case 'fold':
				ment += "<table border='0' cellpadding='0' cellspacing='0' width='100%'>"
					+ "<tr><td colspan='2' style='padding:5px 0; font-weight:bold;'>펼침상자 넣기</td></tr>"
					+ "<tr><td colspan='2' style='padding:5px 0; font:11px dotum; color:#545454; line-height:1.5;'>펼침상자 제목을 클릭하면 내용이 열리는 펼침상자(폴딩)을 넣습니다.</td></tr>"
					+ line
					+ "<tr>"
						+ "<td style='padding:3px 0; font:11px dotum;' nowrap='nowrap'>제목</td>"
						+ "<td style='padding:3px 0; font:11px dotum;'><input type='text' id='iiEditor" + this.index + "_tmp_fold' class='formText' style='width:100px;' /></td>"
					+ "</tr>"
					+ line
					+ "</table>";
				break;

			case 'page':
				ment += "<table border='0' cellpadding='0' cellspacing='0' width='100%'>"
					+ "<tr><td colspan='2' style='padding:5px 0; font-weight:bold;'>페이지 나누기</td></tr>"
					+ "<tr><td colspan='2' style='padding:5px 0; font:11px dotum; color:#545454; line-height:1.5;'>여러 주제가 함께 있는 글일 경우(리뷰 등) 게시물 내에서 페이지를 나눠 볼 수 있게 설정합니다. 페이지목록 넣기를 선택하면 페이지목목을 표시하는 상자를 삽입합니다.</td></tr>"
					+ line
					+ "<tr>"
						+ "<td style='padding:3px 0; font:11px dotum;' nowrap='nowrap'>제목</td>"
						+ "<td style='padding:3px 0; font:11px dotum;'><input type='text' id='iiEditor" + this.index + "_tmp_page' name='iiEditor" + this.index + "_tmp_page' class='formText' style='width:100px;' /></td>"
					+ "</tr>"
					+ line
					+ "<tr>"
						+ "<td style='padding:3px 0; font:11px dotum;' nowrap='nowrap'>페이지목록</td>"
						+ "<td style='padding:3px 0; font:11px dotum;'><input type='checkbox' value='1' id='iiEditor" + this.index + "_tmp_page_list' class='middle' /><label for='iiEditor" + this.index + "_tmp_page_list'>목록만 넣기</label></td>"
					+ "</tr>"
					+ line
					+ "</table>";
				break;

			case 'link':
				ment += "<table border='0' cellpadding='0' cellspacing='0' width='100%'>"
					+ "<tr><td colspan='2' style='padding:5px 0; font-weight:bold;'>링크 넣기</td></tr>"
					+ "<tr><td colspan='2' style='padding:5px 0; font:11px dotum; color:#545454; line-height:1.5;'>페이지를 이동하는 하이퍼링크를 넣습니다.</td></tr>"
					+ line
					+ "<tr>"
						+ "<td style='padding:3px 0; font:11px dotum;' nowrap='nowrap'>URL</td>"
						+ "<td style='padding:3px 0; font:11px dotum;'><input type='text' id='iiEditor" + this.index + "_tmp_link' name='iiEditor" + this.index + "_tmp_link' class='formText' style='width:100px;' /></td>"
					+ "</tr>"
					+ line
					+ "</table>";
				break;

			case 'image':
			case 'slide':
			case 'movie':
			case 'music':
				var mode_name = '';
				switch (mode) {
					case 'image': mode_name = '이미지'; break;
					case 'slide': mode_name = '슬라이드'; break;
					case 'movie': mode_name = '동영상'; break;
					case 'music': mode_name = '음악'; break;
				}

				ment += "<table border='0' cellpadding='0' cellspacing='0' width='100%'>"
					+ "<tr><td colspan='2' style='padding:5px 0; font-weight:bold;'>" + mode_name + " 넣기</td></tr>"
					+ "<tr><td colspan='2' style='padding:5px 0; font:11px dotum; color:#545454; line-height:1.5;'>" + mode_name + (mode_name == '이미지' || mode_name == '슬라이드' ? '를' : '을') + " 넣습니다. 첨부파일을 넣을 경우 파일번호만 입력하거나 파일첨부에서 추가해주세요. " + (mode != 'image' ? "여러개를 입력할 경우 엔터(줄바꿈)를 사용하여 구분해 주세요." : "") + "</td></tr>"
					+ line;

				if (mode == 'image')
				ment += 
					"<tr>"
						+ "<td style='padding:3px 0; font:11px dotum;' nowrap='nowrap'>URL</td>"
						+ "<td style='padding:3px 0; font:11px dotum;'><input type='text' id='iiEditor" + this.index + "_tmp_" + mode + "' name='iiEditor" + this.index + "_tmp_" + mode + "' class='formText' style='width:100px;' /></td>"
					+ "</tr>"
					+ line
					+ "</table>";

				else
				ment += 
					"<tr>"
						+ "<td style='padding:3px 0; font:11px dotum;' nowrap='nowrap'>URL </td>"
						+ "<td style='padding:3px 0; font:11px dotum;'><textarea id='iiEditor" + this.index + "_tmp_" + mode + "' class='formTextarea' style='width:150px;' cols='15' rows='5' /></textarea></td>"
					+ "</tr>"
					+ line
					+ "</table>";
				break;

			case 'preview':
				if (!this.obj.value) {
					error('내용을 입력해주세요');
					return false;
				}

				if (iiPopup.isOpen()) {
					iiPopup.close();
					return false;
				}

				// 폼 생성
				var form = new Element('form', {
					action: miniDir + 'preview.php?id=' + id + '&autobr=' + ($chk(this.form.elements['autobr']) ? $(this.form.elements['autobr']).getValue() : '0'),
					method: 'post',
					target: 'iiPopupIframeName'
				});

				var form_ment = new Element('textarea', {
					name: 'ment',
					value: this.obj.value
				}).inject(form);
				
				form.inject(document.body);

				// 레이어 띄우기
				var w = window.opera ? window.innerWidth : window.getSize().size.x;
				w = (parseInt(w) > 800) ? 800 : w-80;

				iiPopup.init({
					url: "",
					width: w,
					height: 600
				});

				form.submit();
				form.remove();
				break;

		}

		if (ment) this.createDialog(mode, e, ment);
	},

	saveCaret: function (obj) {
		if (window.ie) {
			this.caret = document.selection.createRange();
		}
	}
});

window.addEvent('load', function () {
	$$('.editable').each(function (item) {
		if (item.nodeName.toString().match(/textarea/i)) {
			new iiEditorText({
				obj: item
			});
		}
	});

	$$('.editable_cmt').each(function (item) {
		if (item.nodeName.toString().match(/textarea/i)) {
			new iiEditorText({
				obj: item,
				toolbar: '$font$style$bold$italic$underline$strike$upper$lower $highlight$box$code$fold $link$image$slide$movie$music$preview'
			});
		}
	});
});