/*
 * コメント編集
 *
 * Copyright (c) 2003 DRECOM CO.,LTD. All rights reserved.
 * 
 * info@drecom.co.jp
 * http://www.drecom.co.jp/
 */

var COMMENT_FORM_NAME               = 'WriteCommentForm';
var COMMENT_TEXTAREA_NAME           = 'comment';
var COMMENT_COOKIE_CHECKBOX_NAME    = 'EatCookie';
var COMMENT_PREVIEW_ID              = 'comment_preview';
var COMMENT_TOOLBAR_POSMARK_ID      = 'comment_toolbar_position_marker';

var COMMENT_AUTHOR_NAME             = 'author';
var COMMENT_EMAIL_NAME              = 'email';
var COMMENT_URL_NAME                = 'url';
var COMMENT_MEMBER_ONLY_FLAG_NAME   = 'member_only_flag';

var COMMENT_FORM_MSG				= 'comment_form_msg';
var COMMENT_CONFIRM_MSG				= 'comment_confirm_msg';
var COMMENT_COMMENTED				= 'commented';

var gCommentSaveCookieNames = [COMMENT_AUTHOR_NAME, COMMENT_EMAIL_NAME, COMMENT_URL_NAME];

var gCommentForm               = null;
var gCommentTextArea           = null;
var gCommentSaveCookieCheckbox = null;

// *** Cookie ***//
var COMMENT_COOKIE_NAME         = 'blog.comment.preferences';
var COMMENT_IN_COOKIE_NAME      = 'blog.comment.preferences.in';
var COMMENT_COOKIE_EXPIRE_DAYS  = 30; 

var gCommentCookie = new Cookie(document, COMMENT_COOKIE_NAME, COMMENT_COOKIE_EXPIRE_DAYS * 24, '/');

var MSG_NECESSARYLOGINTOCOMMENT = '<font color="red">*</font>このブログは会員しかコメント出来ません';
var MSG_COMMENTCONFIRM = '<b>*コメントはブログ管理者から承認されるまで非表示となります</b>';
var MSG_SENDACCEPTCOMMENT = 'コメントありがとうございました。<br>コメントはブログ管理者から承認されるまで<br>非表示となります。';

loadCommentCookie.loaded = false;
function loadCommentCookie()
{
	var loaded = false;
	if (null == gCommentForm || null == gCommentSaveCookieCheckbox) {
		return;
	}

    var gCommentInCookie = new Cookie(document, COMMENT_IN_COOKIE_NAME);
	var loginFlag = false;
	loaded = gCommentInCookie.load();
    if (loaded) {
		for (var i = 0; i < gCommentSaveCookieNames.length; i++) {
			var nm = gCommentSaveCookieNames[i];
			var t  = null;
			var v  = null;
		
			if (null == nm) continue;
			t = gCommentForm[nm];
			v = gCommentInCookie[nm];
			if (null == v || null == t) continue;
			// gCommentForm[nm].outerHTML = v + gCommentForm[nm].outerHTML;
			var obj = document.createElement("span");
			obj.appendChild(document.createTextNode(v));
			gCommentForm[nm].parentNode.insertBefore(obj, gCommentForm[nm]);
			gCommentForm[nm].value = v;
			gCommentForm[nm].style.display = "none";
			loginFlag = true;
		}
		if (loginFlag){
			gCommentForm[COMMENT_COOKIE_CHECKBOX_NAME].disabled = true;
		}
    } else {
    	loaded = gCommentCookie.load();
    	
    	// load の結果を反映
    	loadCommentCookie.loaded = loaded;
    	if (gCommentSaveCookieCheckbox != null) {
    		gCommentSaveCookieCheckbox.checked = loaded;
    	} 

		if (gCommentSaveCookieCheckbox == null || true == loadCommentCookie.loaded) {
	    	for (var i = 0; i < gCommentSaveCookieNames.length; i++) {
	    		var nm = gCommentSaveCookieNames[i];
	    		var t  = null;
	    		var v  = null;
	    		
	    		if (null == nm) continue;
	    		t = gCommentForm[nm];
	    		v = gCommentCookie[nm];
	    		if (null == v || null == t) continue;
	    		
	    		t.value = v;
	    	}
		}
    }
    if (! loginFlag) {
		var isMemberOnly = false;
		try {
            // 会員のみコメント可能な場合、メッセージを表示した上でコメントフォームを消します。
        	var lyer = new XBSLayer(COMMENT_FORM_MSG);
    		lyer.setInnerHTML(MSG_NECESSARYLOGINTOCOMMENT);
			isMemberOnly = new Boolean(gCommentForm[COMMENT_MEMBER_ONLY_FLAG_NAME].value);
		} catch(e) {}
		if (isMemberOnly) {
			gCommentForm[COMMENT_COOKIE_CHECKBOX_NAME].disabled = true;
			gCommentForm[COMMENT_TEXTAREA_NAME].disabled = true;
			var es = gCommentForm.elements;
			var es_len = es.length;
			for (var i=0;i<es_len;i++) {
			    if( typeof es[i]  == 'function' ) continue;
				if (es[i].tagName.toLowerCase() == 'input' && es[i].type.toLowerCase() == 'submit') {
					es[i].disabled = true;
					es[i].style.display = "none";
					break;
				}
			}
		}
	}
	try {
		// 承認が必要な場合、メッセージを表示します。
		var lyer = new XBSLayer(COMMENT_CONFIRM_MSG);
		lyer.setInnerHTML(MSG_COMMENTCONFIRM);
	} catch(e) {
	}
	try {
		// 承認が必要なコメントが登録された場合はメッセージを表示します。
		var lyer = new XBSLayer(COMMENT_COMMENTED);
		// 存在チェック
		lyer.setInnerHTML("");
		htmlAlert(MSG_SENDACCEPTCOMMENT);
	} catch(e) {
	}
}
function storeCommentCookie()
{
	for (var i = 0; i < gCommentSaveCookieNames.length; i++) {
		var nm = gCommentSaveCookieNames[i];
		var t  = null;
		
		if (null == nm) continue;
		t = gCommentForm[nm];
		if (null == t || null == t.value) continue;
		
		gCommentCookie[nm] = t.value;
	}
	gCommentCookie.store();
	loadCommentCookie.loaded = true;
}
function removeCommentCookie()
{
	if (loadCommentCookie.loaded) {
		gCommentCookie.remove();
	}
}

/**
 * checkbox - onclick
 */
function handleEatCookieCheckBoxOnClick(aCheckBox)
{
	var checked = (true == aCheckBox.checked);
	if (checked) {
		storeCommentCookie();
	} else {
		removeCommentCookie();
	}	
}
/** form.onsubmit */
function comment_form_onsubmit()
{
	var lyer = XBSLayer.makeLayer(gColorPaletteID);
	
	// 2004-05-19  Takanori Ishikawa 
	// -----------------------------------------------------------
	// カラーパレットの　RGB 手入力の完了
	// HTML の構成上、Form にすることができなかったので
	// 苦肉の策
	if (lyer != null && lyer.isVisible()) {
		colorPaletteFieldValueDidAction();
		return false;
	}
	if (comment_form_onsubmit.tooLate) {
		return false;
	}
	comment_form_onsubmit.tooLate = true;
	
    if (gCommentForm[COMMENT_COOKIE_CHECKBOX_NAME].disabled) {
	} else if (gCommentSaveCookieCheckbox != null && true == gCommentSaveCookieCheckbox.checked) {
		storeCommentCookie();
	} else {
		removeCommentCookie();
	}
	return true;
}
function comment_setUp_textArea(textArea)
{
	// 2004-05-19  Takanori Ishikawa 
	// -----------------------------------------------------------
	// テンプレート編集でユーザに JavaScript を見せたくないので、ここで設定
	
	textArea.onfocus     = function(e){ };
	textArea.onmouseup   = function(e){ initializeSelectedText(this); cutOutTagFragmentOnSelectionArea(); renderPreview(this, COMMENT_PREVIEW_ID);}; 
	textArea.onchange    = function(e){ renderPreview(this, COMMENT_PREVIEW_ID); };
	textArea.onkeydown   = function(e){ 
		if(e == null) e = window.event;
		initializeSelectedText(this);
		controlKeystroke(e);
	}
	textArea.onkeyup     = function(e){
		if(e == null) e = window.event;
		initializeSelectedText(this);
		finalizeSelectedText();
		renderPreviewOnKeyEvent(e, this, COMMENT_PREVIEW_ID);
	}
}

/** body.onload */
function comment_body_onload()
{
	/*
	 * HTML を有効にする
	 * @see render.js
	 */
	Renderer.enableHTML = false;
	
	Renderer.isComment = true;
	
	gCommentForm = document[COMMENT_FORM_NAME];
	if (gCommentForm != null) {
		gCommentTextArea = gCommentForm[COMMENT_TEXTAREA_NAME];
	
		// null の可能性もあり
		gCommentSaveCookieCheckbox = gCommentForm[COMMENT_COOKIE_CHECKBOX_NAME];

		comment_setUp_textArea(gCommentTextArea);
		renderPreviewAll(gCommentForm);
	
		// UtilKit.addhock だと false を返してキャンセルできない
		gCommentForm.onsubmit = comment_form_onsubmit;
		loadCommentCookie();
	}
}

/* shortcut for render.js*/
function comment_render()
{
	if (gCommentTextArea != null) {
		renderPreview(gCommentTextArea, COMMENT_PREVIEW_ID);
	}
}


/* onmousedownにすると、onClickが無効になるので、up */
UtilKit.addhook(document, 'onmouseup', hideAllPalette);
UtilKit.addhook(window, 'onload', comment_body_onload);
