/*
 * \u30b3\u30e1\u30f3\u30c8\u7de8\u96c6
 *
 * 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>\u3053\u306e\u30d6\u30ed\u30b0\u306f\u4f1a\u54e1\u3057\u304b\u30b3\u30e1\u30f3\u30c8\u51fa\u6765\u307e\u305b\u3093';
var MSG_COMMENTCONFIRM = '<div class="comment_approval_alert">\u203b\u30b3\u30e1\u30f3\u30c8\u306f\u30d6\u30ed\u30b0\u7ba1\u7406\u8005\u304b\u3089\u627f\u8a8d\u3055\u308c\u308b\u307e\u3067\u975e\u8868\u793a\u3068\u306a\u308a\u307e\u3059</div>';
var MSG_SENDACCEPTCOMMENT = '\u30b3\u30e1\u30f3\u30c8\u3042\u308a\u304c\u3068\u3046\u3054\u3056\u3044\u307e\u3057\u305f\u3002<br>\u30b3\u30e1\u30f3\u30c8\u306f\u30d6\u30ed\u30b0\u7ba1\u7406\u8005\u304b\u3089\u627f\u8a8d\u3055\u308c\u308b\u307e\u3067<br>\u975e\u8868\u793a\u3068\u306a\u308a\u307e\u3059\u3002';

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 \u306e\u7d50\u679c\u3092\u53cd\u6620
    	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 {
            // \u4f1a\u54e1\u306e\u307f\u30b3\u30e1\u30f3\u30c8\u53ef\u80fd\u306a\u5834\u5408\u3001\u30e1\u30c3\u30bb\u30fc\u30b8\u3092\u8868\u793a\u3057\u305f\u4e0a\u3067\u30b3\u30e1\u30f3\u30c8\u30d5\u30a9\u30fc\u30e0\u3092\u6d88\u3057\u307e\u3059\u3002
        	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 {
		// \u627f\u8a8d\u304c\u5fc5\u8981\u306a\u5834\u5408\u3001\u30e1\u30c3\u30bb\u30fc\u30b8\u3092\u8868\u793a\u3057\u307e\u3059\u3002
		var lyer = new XBSLayer(COMMENT_CONFIRM_MSG);
		lyer.setInnerHTML(MSG_COMMENTCONFIRM);
	} catch(e) {
	}
	try {
		// \u627f\u8a8d\u304c\u5fc5\u8981\u306a\u30b3\u30e1\u30f3\u30c8\u304c\u767b\u9332\u3055\u308c\u305f\u5834\u5408\u306f\u30e1\u30c3\u30bb\u30fc\u30b8\u3092\u8868\u793a\u3057\u307e\u3059\u3002
		var lyer = new XBSLayer(COMMENT_COMMENTED);
		// \u5b58\u5728\u30c1\u30a7\u30c3\u30af
		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 
	// -----------------------------------------------------------
	// \u30ab\u30e9\u30fc\u30d1\u30ec\u30c3\u30c8\u306e\u3000RGB \u624b\u5165\u529b\u306e\u5b8c\u4e86
	// HTML \u306e\u69cb\u6210\u4e0a\u3001Form \u306b\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u306a\u304b\u3063\u305f\u306e\u3067
	// \u82e6\u8089\u306e\u7b56
	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 
	// -----------------------------------------------------------
	// \u30c6\u30f3\u30d7\u30ec\u30fc\u30c8\u7de8\u96c6\u3067\u30e6\u30fc\u30b6\u306b JavaScript \u3092\u898b\u305b\u305f\u304f\u306a\u3044\u306e\u3067\u3001\u3053\u3053\u3067\u8a2d\u5b9a
	
	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 \u3092\u6709\u52b9\u306b\u3059\u308b
	 * @see render.js
	 */
	Renderer.enableHTML = false;
	
	Renderer.isComment = true;
	
	gCommentForm = document[COMMENT_FORM_NAME];
	if (gCommentForm != null) {
		gCommentTextArea = gCommentForm[COMMENT_TEXTAREA_NAME];
	
		// null \u306e\u53ef\u80fd\u6027\u3082\u3042\u308a
		gCommentSaveCookieCheckbox = gCommentForm[COMMENT_COOKIE_CHECKBOX_NAME];

		comment_setUp_textArea(gCommentTextArea);
		renderPreviewAll(gCommentForm);
	
		// UtilKit.addhock \u3060\u3068 false \u3092\u8fd4\u3057\u3066\u30ad\u30e3\u30f3\u30bb\u30eb\u3067\u304d\u306a\u3044
		gCommentForm.onsubmit = comment_form_onsubmit;
		loadCommentCookie();
	}
}

/* shortcut for render.js*/
function comment_render()
{
	if (gCommentTextArea != null) {
		renderPreview(gCommentTextArea, COMMENT_PREVIEW_ID);
	}
}


/* onmousedown\u306b\u3059\u308b\u3068\u3001onClick\u304c\u7121\u52b9\u306b\u306a\u308b\u306e\u3067\u3001up */
UtilKit.addhook(document, 'onmouseup', hideAllPalette);
UtilKit.addhook(window, 'onload', comment_body_onload);

