var gIsAdmin = false;

function showContent(id)
{
	var o = $("#content"+id);
	if(o.html()=='')
	{
		// show
		o.css('display','none').html('<span class="tip"> Loading…</span>').slideDown('normal',function(){
			$.post(ajaxUrl('topic','getContent'),{id:id},function(res){
				o.slideUp('fast',function(){
					o.html(res).slideDown('slow',function(){showComment(id);});
					$('a.pic').fancybox();
					//dp.SyntaxHighlighter.HighlightAll('code'+id);
					//$("img.mcimg").each(function(){alert($(this).attr('src'));});
					//$(function(){$('a[rel*=mcimg]').fancyBox();});
				});
			});
		});
	}else
	{
		// hide
		o.slideUp('fast',function(){o.empty();});
	}
}

function showComment(id)
{
	if(id==0) id=g_ID;
	var o = $("#comment"+id);
	if(!o) return;
	o.html('loading...');
	$.post(ajaxUrl('comment','getTopicComment'),{id:id},function(res){
		var j=getJSON(res);
		var str='';
		var replybutton = '<a class="replay" href="javascript:showReplyBox('+id+',0)">评论几句</a>';
		if(j)
		{
			for(var i in j)
			{
				if(j[i].id==j[i].iReply)
					str+=(str?'</li>':'')+'<li>';
				str+='<div'+(j[i].id==j[i].iReply?'':' class="reply"')+'>'+(j[i].icon?'<img src="'+j[i].icon+'">':'')+'<div class="commentInfo"><strong>'+(j[i].website?'<a href="http://'+j[i].website+'" target="_blank">'+j[i].username+'</a>':j[i].username)+'</strong>'+j[i].timePublish+' | <a href="javascript:showReplyBox('+id+','+j[i].id+',\''+j[i].username+'\');" class="replay">回复</a></div><p>'+j[i].content+'</p></div>';
			}
			str = '<ul class="commentList">'+str+'</li></ul>';
			if(j.length>5) str+=replybutton;
		}else
			str = '<div class="tip">还没有评论</div>';
		o.css('display','none').html(replybutton+str).slideDown('normal');
	});
}

var gReplyid=0;
function showReplyBox(tid, cid, toName)
{
	gReplyid = cid;
	g_ID = tid;
	$.fancybox('modules/comment/replybox.html',{'type':'ajax', 'scrolling':'no', 'onComplete':function(){
		if(toName)
			$('#toName').html('<em>@'+toName+'</em>');
		if(!window.localStorage) return;
		if(localStorage.guestEmail)
		{
			$('#tEmail').val(localStorage.guestEmail);
			getGuestInfo(localStorage.guestEmail);
			$('#tContent').focus();
		}
	}});
}

function signin()
{
	$.fancybox('modules/user/signinbox.html',{'type':'ajax', 'scrolling':'no'});
}

function edit(id)
{
	g_ID = id;
	$.fancybox('modules/topic/topicEditor.html',{'type':'ajax', 'scrolling':'no','transitionIn':'none','hideOnOverlayClick':'false', 'onComplete':function(){
		gEditor.refresh();
	}});
}

function del(id)
{
	if(!deleteAlert()) return;
	$.post(ajaxUrl('topic','onDelete'),{id:id},function(res){
		if(res==OK)
			gotoUrl();
		else
			alert(res);
	});
}
