// 文字列 str 中に含まれる全角数字を半角に。
function toHankaku(str) {
  return str.replace(/[０-９]/g, function(str){return String.fromCharCode(str.charCodeAt(0)-65248);});
}


function comment_edit2(comment_length) {
	var comment_length = comment_length;
	var anchor_com_num_array = [];
	var id_obj = {};
	var change_num_color_id = 0;
	var check_comment_id = new RegExp('class="id_(.{9})"');
	var check_anchor_comment = new RegExp('((※|米|\\*)(\\d+))',"g");
	var check_anchor_thread = new RegExp("((&gt;&gt;|＞＞)(\\d+))","g");
	for(var i = 0; i < comment_length; i++){
		var anchor_com_num = 0;
		var add_id_class = 0;
		var comment_id = 'comment_' + (i + 1);
		var comment_cont = document.getElementById(comment_id);
		if(id_temp = check_comment_id.exec(comment_cont.innerHTML) ){
			if(id_obj[id_temp] ==null){
				id_obj[id_temp] = 1;
			}else{
				id_obj[id_temp]++;
			}
			if(id_obj[id_temp] == 2){
				$('.id_'+ id_temp[1]).css({'color':'#800080','font-weight':'bold'});
				$('.id_'+ id_temp[1]).each(function(i){
					jQuery(this).html("<span onmouseover=\"make_id_pop_over('"+id_temp[1]+"',this);\" onmouseout= \"delete_pop_over(this);\">" + jQuery(this).html() + "</span>");
				});
			}else if(id_obj[id_temp] == 5){
				$('.id_'+ id_temp[1]).css('color', '#DD0000');
			}
		}
		comment_cont.innerHTML = toHankaku(comment_cont.innerHTML);
		comment_cont.innerHTML = comment_cont.innerHTML.replace(check_anchor_comment, "<a href='#comment_$3'  class='com_$3' onmouseover=\"make_pop($3,0,this);\" onmouseout=\"delete_pop(this);\">$1</a>");
		comment_cont.innerHTML = comment_cont.innerHTML.replace(check_anchor_thread, "<a href=\"#res_$3\"  class=\"com_$3\" onmouseover=\"make_pop($3,1,this);\" onmouseout=\"delete_pop(this);\">$1</a>");
		while((anchor_com_num = check_anchor_comment.exec(comment_cont.innerHTML)) != null){
			if(anchor_com_num_array[anchor_com_num[3]] == null){
				anchor_com_num_array[anchor_com_num[3]] = 1;
				change_num_color_id = "comment_num_" + anchor_com_num[3];
				add_id_class = "anchor_" + anchor_com_num[3];
				$('#'+ comment_id).addClass(add_id_class);
				document.getElementById(change_num_color_id).innerHTML = document.getElementById(change_num_color_id).innerHTML.replace(/(\d+)/g,'<span id="comment_num_color_$1" onmouseover=\" make_num_pop_over( $1, this);\" onmouseout=\" delete_pop_over(this);\"style=" color: #800080; text-decoration: underline; font-weight: bold;">$1</span>');
			}else{
				anchor_com_num_array[anchor_com_num[3]]++;
				add_id_class = "anchor_" + anchor_com_num[3];
				$('#'+ comment_id).addClass(add_id_class);
				if(anchor_com_num_array[anchor_com_num[3]] == 5){
					change_num_color_id  = "#comment_num_color_" + anchor_com_num[3];
					$(change_num_color_id).css( 'color', '#FF0000');
				}
			}
		}
	}
}


function make_pop(anchor_num,flg,obj){
var anchor_num = anchor_num;
var cls = ".com_" + anchor_num;

var pop_cont = 0;

	if(flg == 0){
		pop_cont = "comment_" + anchor_num;
	}else if(flg == 1){
		pop_cont = "res_" + anchor_num;
	}
	if(document.getElementById(pop_cont) != null){
		$(cls).bt({
			contentSelector: "$('#" + pop_cont + "')",			padding: 10,			width: 700,
			spikeLength: 10,			spikeGirth: 10,			cornerRadius: 15,			fill: 'rgba(255, 255, 255, .85)',
			strokeStyle: '#888888'		});
  		$(obj).btOn();
	}
}

function delete_pop(obj){
	$(obj).btOff();
}

function make_num_pop(comment_num, obj){
	$(obj).bt(
	'<span onclick="num_in_comment(' + comment_num + ')">これにコメント</span><br><span onclick="num_quotation_in_comment(' + comment_num + ')">これに引用コメント</span>', 
//	'<span onclick="num_in_comment(' + comment_num + ')">これにコメント</span>', 
	{
		//contentSelector: "$('#comment_" + comment_num +"')",
		padding: 10,		width: 150,
		spikeLength: 10,		spikeGirth: 10,		fill: '#FFFFFF',
		trigger: ['click'],
  		strokeStyle: '#888888'	});
	$(obj).btOn();
}

function num_in_comment(comment_num){
	document.getElementById('text').value += "※" + comment_num +"\n";
	location.hash='text'; 
}

function num_quotation_in_comment(comment_num){
	var word =0;
	var comment_num = comment_num;
	var get_content_id = "comment_body_" + comment_num;

	var comment_content = document.getElementById(get_content_id).innerHTML;
	
	comment_content = comment_content.replace(/<\/a>/g,"");
	comment_content = comment_content.replace(/<br>/g,"");
	comment_content = comment_content.replace(/^/g,"＞");
	comment_content = comment_content.replace(/\n/g,"\n＞");
	comment_content = comment_content.replace(/&gt;/g,">");
	comment_content = comment_content.replace(/&lt;/g,"<");
	comment_content = comment_content.replace(/&amp;/g,";");
	comment_content = comment_content.replace(/&gt;/g,"\"");
	comment_content = comment_content.replace(/<a href="#comment_\d+" class="com_\d+" onmouseover="make_pop\(\d+,0,this\);" onmouseout="delete_pop\(this\);">/g,"");
	comment_content = comment_content.replace(/<a href="#res_\d+" class="com_\d+" onmouseover="make_pop\(\d+,1,this\);" onmouseout="delete_pop\(this\);">/g,"");
	comment_content = comment_content.replace(/<a title="" bt-xtitle="" href="#comment_\d+" class="com_\d+" onmouseover="make_pop\(\d+,0,this\);" onmouseout="delete_pop\(this\);">/g,"");


	document.getElementById('text').value += "※" + comment_num + "\n" + comment_content + "\n";
	location.hash='text'; 
}


function make_num_pop_over( anchor_num, obj){
	var anchor_content = "";
	$(".anchor_" + anchor_num).each(function(i){
		anchor_content = anchor_content + jQuery(this).html();
	});
	$("#temp_anchor_content").html(anchor_content);

	$(obj).bt(
	{
		contentSelector: "$('#temp_anchor_content')",
		padding: 10,		width: 700,
		spikeLength: 10,		spikeGirth: 10,		fill: '#FFFFFF',
  		strokeStyle: '#888888'	});
	$(obj).btOn();
}

function delete_pop_over(obj){
	$(obj).btOff();
}

function id_pop(id){
	var id_object = {};

}

function make_id_pop_over( pop_comment_id, obj){
	var anchor_content = pop_comment_id;
	$("." + pop_comment_id).each(function(i){
		anchor_content = anchor_content + jQuery(this).html();
	});
	$("#temp_anchor_content").html(anchor_content);

	$(obj).bt(
	{
		contentSelector: "$('#temp_anchor_content')",
		padding: 10,		width: 700,
		spikeLength: 10,		spikeGirth: 10,		fill: '#FFFFFF',
		positions: 'top',
  		strokeStyle: '#888888'	});
	$(obj).btOn();
}



