var editorMode;

function initCommentEditor(popupURL, mode) {
	if (!tinyMCE.isSafari) {
		//		tinyMCE.init({mode: 'textarea'});
		tinyMCE.init({
                theme: "advanced", 
                mode: "exact",
                language: "ja_UTF-8",
                elements: ('r' == mode ? "game_review_comment" : ""),
                theme_advanced_toolbar_location: "top",
                theme_advanced_buttons1: "formatselect,bold,italic,underline,strikethrough,forecolor,separator,bullist,numlist,snsimage,link,unlink,outdent,indent,code",
                theme_advanced_buttons2: "",
                theme_advanced_buttons3: "",
                plugins: "snsimage",
                sns_image_popup_url: popupURL,
				content_css: "/stylesheets/entry_editor.css",
				relative_urls: false
					});
	}
	editorMode = mode;
}

function insertImageFromPopup(url, align, alt) {
	//	tinyMCEPopup.restoreSelection();
	tinyMCE.themes['advanced']._insertImage(url, alt, 0, null, null, null, null, align);
	tinyMCEPopup.close();
}

function toggleImageType() {
	var type = $F('type_file') || $F('type_url');
	var anotherType = type == 'file' ? 'url' : 'file';
	Element.hide($('table_for_' + anotherType));
	Element.show($('table_for_' + type));
}

function getEditorContent() {
	if (tinyMCE.getInstanceById('game_review_comment') != null) {
		return tinyMCE.getContent('game_review_comment');
	} else {
		return $F('game_review_comment');
	}
}

function setEditorContent() {
	if (tinyMCE.getInstanceById('game_review_comment') != null) {
		return tinyMCE.getContent('game_review_comment');
	} else {
		return $F('game_review_comment');
	}
}

// 指定されたモードが HTML かプレーンテキストかを返す
function modeIsHtml(mode) {
	return mode == 'h' || mode == 't';
}

function toggleEditor(mode) {
	var shown = tinyMCE.getInstanceById('game_review_comment') != null;
	var clear = false;
	if (modeIsHtml(editorMode) != modeIsHtml(mode)) {
		if (getEditorContent()) {
			if (confirm('切り替えを行うと編集中の内容は破棄されます。切り替えてもよろしいですか？')) {
				if (!shown) {
					$('game_review_comment').value = '';
				}
				clear = true;
			} else {
				$('game_review_editor_' + mode).checked = false;
			    $('game_review_editor_' + editorMode).checked = true;
				return;
			}
		}
	}
	if ('r' == mode) {
		if (!shown) tinyMCE.addMCEControl($('game_review_comment'), 'game_review_comment');
		document.getElementById('add_photo').style.display = 'none';
	} else {
		if (shown) {
			tinyMCE.removeMCEControl('game_review_comment');
			if (clear) {
				$('game_review_comment').value = '';
			}
		}
		document.getElementById('add_photo').style.display = '';
	}
	editorMode = mode;
}
