

function reporttopic(id, member_id, reason) {
	$.post('/ajax/topicengine.php', { op:'report', id:id, member_id:member_id, reason:reason },
		function(data) {
			if (data.status == 'ok') {
				$('#topic-rating').find('.reportlink').empty().after('แจ้งลบแล้ว');
			} else {
				alert(data.msg);
			}
		}, "json"
	);
}

function votetopic(id) {
	$.post('/ajax/topicengine.php', { op:'vote', id:id },
		function(data) {
			if (data.status == 'ok') {
				$('#topic-rating').find('.votelink').empty().after('โหวตให้แล้ว');
				num_voted = parseInt($('#voted').text());
				$('#voted').text(String(num_voted + 1));
			} else {
				alert(data.msg);
			}
		}, "json"
	);
}

function votereply(id) {
	$.post('/ajax/replyengine.php', { op:'vote', id:id },
		function(data) {
			if (data.status == 'ok') {
				$('#reply-'+id).find('.votelink').empty().after('เยี่ยม');
				$('#reply-'+id).find('.reportlink').empty().after('ห่วย');
			} else {
				alert(data.msg);
			}
		}, "json"
	);
}

function reportreply(id) {
	$.post('/ajax/replyengine.php', { op:'del', id:id },
		function(data) {
			if (data.status == 'ok') {
				$('#reply-'+id).find('.reportlink').empty().after('ห่วย');
				$('#reply-'+id).find('.votelink').empty().after('เยี่ยม');
			} else {
				alert(data.msg);
			}
		}, "json"
	);
}
function reportreply2(id){
	$.post('/ajax/replyengine.php', {op:'report_del', id:id}, 
		function(data){
			if(data.status == 'ok'){
				$('#reply-'+id).find('.reportlink').empty().after('แจ้งลบแล้ว');
			} else {
				alert(data.msg);
			}
		}, "json"
	
	);
}
function showreply(id) {
	$.post('/ajax/replyengine.php', { op:'show', id:id },
		function(data) {
			if (data.status == 'ok') {
				reply_parent = $('#reply-'+id).parent();
				reply_parent.find('.blocked-info').hide();
				reply_parent.find('.reply').fadeIn('normal');
				reply_parent.find('.reply-detail').prepend(data.reply.detail);
			} else if (data.status == 'error') {
				alert(data.msg);
			}
		}, "json"
	);
}
function addtag(id, tags) {
	$('#tags')[0].focus();
	// validation
	tag_list = tags.split(/\s*,\s*/);
	tag_array = [];
	is_error = false;
	for (i = 0; i < tag_list.length; i++) {
		trimmed = $.trim(tag_list[i]);
		if ((trimmed.length < 2 || trimmed.length > 32) && (trimmed != '')) {
			is_error = true;
		}
	} 
	if (is_error) {
		alert('แท็กแต่ละอันมีความยาว 2-32 ตัวอักษร ใช้เฉพาะตัวภาษาไทย, ภาษาอังกฤษและตัวเลขนะ');
		$('#tags')[0].focus();
		return false;
	}
	// disable form
	$('#addtag-form').find('br').before('<img class="inline-icon" src="/static/images/indicator.gif" />');
	$('#tags').attr('disabled', 'true');
	$('#addtag-bttn').attr('disabled', 'true');
	// ajax request
	// we need to re-enable form inside function because the function is called after XHR finish
	$.post('/ajax/topicengine.php', { op:'addtag', id:id, tags:tags },
		function(data) {
			if (data.status == 'ok') {
				// re-enable form
				$('#addtag-form').find('img').remove();
				$('#tags').attr('value', '');
				$('#tags').removeAttr('disabled');
				$('#addtag-bttn').removeAttr('disabled');
				displaytag();
				$('#tags')[0].focus();
			} else {
				// re-enable form
				$('#addtag-form').find('img').remove();
				$('#tags').removeAttr('disabled');
				$('#addtag-bttn').removeAttr('disabled');
				alert(data.msg);
				$('#tags')[0].focus();
			}
		}, "json"
	);
	return false;
}
function showquotedtext(e) {
	$(e).find('.inner-quoted').css('display', 'block');
}
function sendtofriend(id) {
	window.open('/sendtofriend.php?id='+id, 'newwindow','height=600,width=400,top=100,left=100');
}
function addfavorite(tid) {
	$.post('/ajax/topicengine.php', { op:'addfav', id:tid}, function(data){
		if (data.status == 'ok') {
			$('#topic-rating').find('.favorite').empty().after('<a href="javascript:delfav('+tid+')">เอาออกจาก favorited</a>');
		} else {
			alert(data.msg);
		}
	}, "json");
}
function delfav(tid) {
	$.post('/ajax/topicengine.php', { op:'delfav', topic_id:tid}, function(data) {
		if (data.status == 'ok') {
			$('#topic-rating').find('.favorite').empty().after('<a href="javascript:addfavorite('+tid+')">เก็บเป็น favorite</a>');
		} else {
			alert(data.msg);
		}
	}, "json");
}
function show_reportpanel() {
	$('#topic-report-panel').fadeIn('fast');
}
function hide_reportpanel() {
	$('#topic-report-panel').fadeOut('fast');
}
function recommendtopic(id){
	$.post('/ajax/topicengine.php', { op:'recommend', id:id },
		function(data){
			if (data.status == 'ok') {
				$('#topic-rating').find('.recommendlink').empty().after('แนะนำแล้ว');
			}
			else {
				alert(data.msg);
			}
		},"json"
	);
}
function toggle_content_size() {
	content_control = $('#content-control');
	content_container = $('#content-container');
	if (content_container.css('overflow') == 'auto') {
		content_container.css('height', '');
		content_container.css('overflow', 'visible');
		content_control.find('a').empty().append('ดูย่อๆ')
	} else {
		content_container.css('height', '250px');
		content_container.css('overflow', 'auto');
		content_control.find('a').empty().append('ดูเต็มๆ')
	}
}
function reply_handler(e) {
	frm = $('#replyform')[0];
	$('#replyform').find('#detail').attr('disabled', 'disabled');
	$('#replybttn').attr('disabled', 'disabled');
	$('#replybttn').after('<img class="inline-icon" src="/static/images/indicator.gif" />');
	$.post('/ajax/replyengine.php', 
		{ op:'add', topic_id:frm.topic_id.value, sender:frm.sender.value, detail:frm.detail.value },
		function(data){
			
			if (data.status != 'ok') {
				$('#reply-notify').addClass('msgbox-error');
				$('#reply-notify').empty().prepend('<ul></ul>');
				ul = $('#reply-notify').find('ul');				
									
				for (i = 0; i < data.error.length; i++) {
					ul.append('<li>'+data.error[i]+'</li>');
				}
			} else {
				$('#reply-notify').removeClass('msgbox-error');
				$('#reply-notify').addClass('msgbox-ok');
				$('#reply-notify').empty().prepend('ตอบกระทู้แล้ว');
				$('#replyform').find('#detail').attr('value', '');
				$.post('/ajax/replyengine.php', {op:'getreply', id:data.reply_id}, 
					function(d){
						if (d.status == 'ok') {
							$('#replyform').before(d.html);
							$('#reply-'+data.reply_id).fadeIn('normal');
							if (data.login_alert == true) {
								$('#alert-comment').addClass('msgbox-comment');
								$('#alert-comment').append('<img style="float:left" src="/static/images/ques_m.gif" />นี่ใครอะ? คราวหน้าล็อกอินดีกว่าม่ะ <br />จะได้มีไอคอนสวยๆ แทนรูปหัวกลมๆ ตัวดำๆ');
							} 
						}
					}, 
					"json");
			}
			if(data.status == 'spam'){
				$('#replybttn').attr('disabled', 'disabled');
				setTimeout("$('#replybttn').removeAttr('disabled')", 60000);
				$('#replyform').find('#detail').removeAttr('disabled');			
				$('#replyform').find('img.inline-icon').remove();
			} else {
				$('#replyform').find('#detail').removeAttr('disabled');
				$('#replybttn').removeAttr('disabled');
				$('#replyform').find('img.inline-icon').remove();
			}
		},"json"
	);			
}
function addemoticon(emo) {
	var txt_comment = $('#detail').val();
	if (txt_comment) {
		$('#detail').val(txt_comment+emo);
	} else {
		$('#detail').val(emo);
	}
}

	
	function change_emo(member_id,viewuser_url) {
		var pic = document.getElementById('emo-name').value;
		$.post("/ajax/replyengine.php",{ op:'change_emo' , pic:pic , member_id:member_id },
			function(data) {
				if (data.status == 'ok') {
					alert(data.msg);	
					//alert(viewuser_url);
					location.href="";
				} else {
					alert(data.msg);
				}
			}, "json"
		);
		
	}
function comment(type) {
	if(type) {
		$('#sign-type').val(type);
	}else {
		$('#sign-type').val('');
	}
	$('#comment-box').slideToggle('slow');
}
	
function show_comment(type) {
	if(type) {
		show_direct(type);		
	}else {
		$('#sign-type').val('');
		$('#sign-normal').attr("onclick","hide_comment()");
	}
	$('#comment-box').slideDown('slow');
	
}
function hide_comment(type) {
	if(type) {
		hide_direct(type);
	}else {
		$('#sign-normal').attr("onclick","show_comment()");
	}
	
	$('#comment-box').slideUp('slow');	
}
function show_direct(type) {
	$('#sign-type').val(type);
	$('#sign-direct').attr("onclick","hide_comment("+type+")");
}	
function hide_direct(type) {
	$('#sign-direct').attr("onclick","show_comment("+type+")");
}
function numtohex(input){
	var t="0123456789ABCDEF"
	return (t.charAt(input>>4)+t.charAt(input%16))
 }
function convert_color_code(color_code) {
	var new_color_code = '';
	if(/^#/.test(color_code)){
		new_color_code = color_code.replace("#","0x");
	}else if (/^rgb/.test(color_code)) {
		color_code = color_code.replace(/^rgb\(/,"");
		color_code = color_code.replace(/\)$/,"");
		var ar = color_code.split(',');
		new_color_code = '0x'+numtohex(ar[0])+numtohex(ar[1])+numtohex(ar[2]);
	}else { 
		var color_map = { black: '0x000000', red: '0xFF0000', green: '0x00FF00', blue: '0x0000FF', yellow: '0xFFFF00', magenta: '0xFF00FF', cyan: '0x00FFFF', white: '0xFFFFFF', gray: '0x808080', maroon: '0x800000', purple: '0x800080', 'darker green': '0x008000', olive: '0x808000', navy: '0x000080', teal: '0x008080', silver: '0xC0C0C0' };
		if(color_map[color_code.toLowerCase()] != null)
			new_color_code = color_map[color_code.toLowerCase()];
	}
	
	return new_color_code;
}	
function detail_userclass() {
	window.open('/detail.php', 'newwindow','scrollbars=yes,height=400,width=400,top=500,left=500');
}
