/*
 * コメント編集
 *
 * 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_AREA_ID                 = 'comment_area';
var COMMENT_PREVIEW_ID              = 'comment_preview';
var COMMENT_TOOLBAR_POSMARK_ID      = 'comment_toolbar_position_marker';

var COMMETN_LAYER_CLASS             = 'comment_layer';

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 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 = '<br>このブログへのコメントは会員登録をされた方のみ可能です。<br><br>SPORA新規登録 ・ログインはこちらから！<br><h3 class="login-btn"><a href="/LoginGeneral.blog"><image alt="ログイン" src="/image/login/login-btn.jpg" border="0"/></a></h3>'

var TERMS_OF_SERVICE_TEXT  = '※コメントを行う場合には<a href="http://spora.jp/contents/law">利用規約</a>への同意が必要となります。'; 
var TERMS_OF_SERVICE_LINK  = '<a href="http://spora.jp/contents/law">利用規約</a>';
var TERMS_OF_SERVICE_CHECK_TEXT = '※「同意する」のチェックボックスにチェックが入っていない場合、送信ボタンを押すことができません。';
var TERMS_OF_SERVICE_CHECK_BOX = '<input type="checkbox" id="terms_of_service" name="confirm" onClick="setSubmit(this)"><label for="terms_of_service">同意する</label>';

loadCommentCookie.loaded = false;
function loadCommentCookie()
{
	var loaded = false;
	if (null == gCommentForm || null == gCommentSaveCookieCheckbox) {
		return;
	}

    var gCommentInCookie = new Cookie(document, COMMENT_IN_COOKIE_NAME);
    
    //　コメント投稿にログインが必要な場合はloginFlagをfalseに変更する
	var loginFlag = true;
	
	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.disabled = true;
			gCommentForm.style.display = "none";
		}
	}
}
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);
	}
}

function setSubmit(checkSubmit)
{
	document.WriteCommentForm.post.disabled = !checkSubmit.checked;
}

function comment_show_tosArea()
{
	var isMSIE = /*@cc_on!@*/false; 
		
	var targetNode = document.getElementById(COMMENT_PREVIEW_ID);
		
	var areaNode = document.createElement('div');
	areaNode.id = COMMENT_AREA_ID;
		
	var layerNode = document.createElement('div');
	layerNode.id = COMMENT_PREVIEW_ID;
	if(isMSIE){
		layerNode.setAttribute("className", COMMETN_LAYER_CLASS); // IE
	}else{
		layerNode.setAttribute("class", COMMETN_LAYER_CLASS);     // 他
	}
		
	var tosNode = document.createElement('div');
	tosNode.innerHTML = TERMS_OF_SERVICE_TEXT + '<br>' + 
						//TERMS_OF_SERVICE_LINK + '<br>' +
						TERMS_OF_SERVICE_CHECK_TEXT + '<br>' +
						TERMS_OF_SERVICE_CHECK_BOX;
		
	areaNode.appendChild(layerNode);
	areaNode.appendChild(document.createElement('br'));
	areaNode.appendChild(tosNode);

	targetNode.parentNode.replaceChild(areaNode, targetNode);
	
	document.WriteCommentForm.post.disabled = true;
}

/** 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_show_tosArea();
		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);
