/*
 * コメント編集
 *
 * Copyright (c) 2003 DRECOM CO.,LTD. All rights reserved.
 *
 * info@drecom.co.jp
 * http://www.drecom.co.jp/
 */
//*** ログイン情報 ***//
 var loginAuthor = null;
 var loginUrl    = null;

 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_URL_NAME                = 'url';
 var gCommentSaveCookieNames = [COMMENT_AUTHOR_NAME, COMMENT_URL_NAME];

var gCommentForm               = null;
var gCommentTextArea           = null;
var gCommentSaveCookieCheckbox = null;

// *** Cookie ***//
var COMMENT_COOKIE_NAME         = 'blog.comment.preferences';
var COMMENT_COOKIE_EXPIRE_DAYS  = 30;

var gCommentCookie = new Cookie(document, COMMENT_COOKIE_NAME, COMMENT_COOKIE_EXPIRE_DAYS * 24);

loadCommentCookie.loaded = false;
function loadCommentCookie()
{
	var loaded = false;

	if (null == gCommentForm || null == gCommentSaveCookieCheckbox) {
		return;
	}

	loaded = gCommentCookie.load();
	loadCommentCookie.loaded = loaded;
	if (false == loadCommentCookie.loaded) {
		if( loginAuthor!=null && loginUrl!=null ){
			gCommentForm[COMMENT_AUTHOR_NAME].value = loginAuthor;
			gCommentForm[COMMENT_URL_NAME].value    = loginUrl;;
		}
		return;
	}

	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;
		}else{
			t.value = v;
		}
	}
}
function storeCommentCookie()
{
	if (gCommentForm == null) {
		gCommentForm = document[COMMENT_FORM_NAME];
	}
	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) {
		removeCommentCookie();
		storeCommentCookie();
	} else {
		removeCommentCookie();
	}
}
/** form.onsubmit */
function comment_form_onsubmit()
{
	var lyer = XBSLayer.makeLayer(gColorPaletteID);

	// -----------------------------------------------------------
	// カラーパレットの　RGB 手入力の完了
	// HTML の構成上、Form にすることができなかったので
	// 苦肉の策
	if (lyer != null && lyer.isVisible()) {
		colorPaletteFieldValueDidAction();
		return false;
	}
	if (comment_form_onsubmit.tooLate) {
		return false;
	}
	comment_form_onsubmit.tooLate = true;

	var checked;
	checked = (gCommentSaveCookieCheckbox != null && true == gCommentSaveCookieCheckbox.checked);
	if (checked) {
		removeCommentCookie();
		storeCommentCookie();
	} else {
		removeCommentCookie();
	}
	return true;
}
function comment_setUp_textArea(textArea)
{
	// -----------------------------------------------------------
	// テンプレート編集でユーザに 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){ var event = window.event || e; initializeSelectedText(this); controlKeystroke(event); };
	textArea.onkeyup     = function(e){ var event = window.event || e; initializeSelectedText(this); finalizeSelectedText(); renderPreviewOnKeyEvent(event, this, COMMENT_PREVIEW_ID); };
}

function setCommentFormData()
{
	try{
		LatestSearchWord.makeVisitorComment(callbackDWR);
	}catch(e){}
}
// dwr callback
function callbackDWR(comment)
{
	if( comment!=null ){
		loginAuthor = comment.author;
		loginUrl    = comment.url;
	}
}

/** body.onload */
function comment_body_onload()
{
	setCommentFormData();

	/*
	 * 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);

		setTimeout(loadCommentCookie,400);
	}

	if (true == gCommentCookie.load()) {
		document.getElementById("EatCookie").checked = true;
	}
}

/* shortcut for render.js*/
function comment_render()
{
	if (gCommentTextArea != null) {
		renderPreview(gCommentTextArea, COMMENT_PREVIEW_ID);
	}
}

/* onmousedownにすると、onClickが無効になるので、up */
UtilKit.addhook(window, 'onload', comment_body_onload);

/* コメント投稿部分のjQueryフローティングウィンドウ用2010_10_04出井追加 */
if(j$ == null){
	jQuery.noConflict();
	var j$ = jQuery;
}
j$(function(){
	j$("a.yapFWindowOpen").click(function(){
		j$("#yapFloatWindow").fadeIn("fast");
		return false;
	})

	j$("#yapFloatWindow a.yapFWindowClose").click(function(){
		j$("#yapFloatWindow").fadeOut("fast");
		return false;
	})


	j$("#yapFloatWindow dl dt").mousedown(function(e){

		j$("body").bind('selectstart', function(){
			return false;
		});

		j$("#yapFloatWindow")
			.data("clickPointX" , e.pageX - j$("#yapFloatWindow").offset().left)
			.data("clickPointY" , e.pageY - j$("#yapFloatWindow").offset().top);

		j$(document).mousemove(function(e){
			j$("#yapFloatWindow").css({
				top:e.pageY  - j$("#yapFloatWindow").data("clickPointY")+"px",
				left:e.pageX - j$("#yapFloatWindow").data("clickPointX")+"px"
			})
		})

	}).mouseup(function(){
		j$("body").unbind('selectstart');
		j$(document).unbind("mousemove")

	})
})
