function gotoThread(thread_id,goto,topicId,o)
{
	$(o).append("<img src='/images/loading.gif'>");
	$.post("/forums?act=goto_thread", { "thread_id":thread_id,"topic_id":topicId, "goto":goto},function(data){
		$(o).children("img").remove();
		if(data)
			location.href=data;
		else if(goto>0)
			location.href="/forums?act=thread&id="+topicId;
		else if(goto<0)
			location.href="/forums?act=thread&id="+topicId;
	});	
}

function OpenPostDialog(topic_id,thread_id)
{
	if(thread_id>0)
		var titleString="Post a reply";
	else
		var titleString="Post a new thread";
	Boxy.load("/forums?act=show_post&topic_id="+topic_id+"&thread_id="+thread_id, 
			  {fixed:false,cache:false,modal: true,borderWidth:0,title:titleString,unloadOnHide:true,
			  	afterShow:	function()
							{
								 var options = {
									url:       		"/forums?act=sent_post", // override for form's 'action' attribute 
									type:      		'post', // 'get' or 'post', override for form's 'method' attribute 
									dataType:  		'json', // 'xml', 'script', or 'json' (expected server response type) 
									timeout:   		30000,
									beforeSubmit:	ValidatePost,
									success:       	AfterPost
								}; 
								$('#formPostForum').submit(function(){
									tinyMCE.triggerSave();
									$(this).ajaxSubmit(options); 
									return false; 
								}); 
								$('#send_to_facebook').click(function(){
									if($(this).attr('checked') && !FB.Connect)
										fb_init();
								});
							}
	});
}
function ValidatePost(formData, jqForm, options)
{
	var form = jqForm[0];
	if(form.email_address)
	{
		if (!form.email_address.value || form.email_address.value==form.email_address.title)
		{
			$('#error_msg').html('Please enter your email address.'); 
			return false; 		
		}
		if (!form.password.value) { 
			$('#error_msg').html('Please enter your password.'); 
			return false; 
		} 
	}
	if (form.title.value && !form.title.value)
	{
        $('#error_msg').html('Please enter your title of thread.'); 
        return false;
	}
	if (!form.contents.value|| form.contents.value==form.contents.title)
	{
        $('#error_msg').html('Please enter your body of '+(form.thread_id.value=='0'?"thread":"reply")+'.'); 
        return false;
	}
	$('#post_new').attr('disabled',true);
	if($('#send_to_facebook').attr('checked') && !FB.Connect)
		fb_init();
    return true; 	
}
var __goto_url__ = "";
var __alert_msg__ = "";
function AfterPost(result)
{
	$('#post_new').attr('disabled',false);
	switch(result.status)
	{
		case -2: //has bad words
			alert(result.msg);
			//$('#error_msg').html(result.msg.replace(/\n/g,'<br />'));
			Boxy.get('.close').hide();
			break;
		case 1: // post successfully
			var url = "/forums?act=post&topic_id="+result.topic_id+"&thread_id="+result.thread_id+"&rand="+Math.random()+(result.page>1?"&page="+result.page:'');
			if($$('send_to_facebook').checked)
			{
				__goto_url__ = url;
				__alert_msg__ = result.msg;
				$('#post_new').attr('disabled',true);
				fb_init(function(){PostToFBWall(result.post_id,url);});
			}
			else
			{
				Boxy.get('.close').hide();
				alert(result.msg);
				location.href=url+"#reply"+result.post_id;
			}
			break;
		default: // error..
			$('#error_msg').html(result.msg.replace(/\n/g,'<br />'));
			break;
	}
	
}

function PostToFBWall(cid,url)
{
	var type =$('#forum_post_type').val();
	var a_url = location.href.replace(/\/forums.*/g,url);
	var c_url = a_url+"#reply"+cid;
	var publish = {
		method: 'stream.publish',
		message: '',
		attachment: {
				"name": (type=='thread')?('New thread: '+$('#post_title').val()):("Replied thread: "+$('#thread_name').html()),
				"href": c_url,
				"description": $($('#post_content').val()).text(),
				"media":[{'type': 'image',"src":"http://www.teenink.com/images/logo_for_fb_large.gif", "href":"http://www.teenink.com"}]
		},
		action_links:[{ "text":"Check out Teenink", "href": "http://www.teenink.com"}],
		user_prompt_message: 'Share your post to your facebook wall.'
	};

	FB.ui(publish,function(r){
		if(r)
		{
			alert(__alert_msg__?__alert_msg__:"Thanks! Your post has been submitted successfully. Please note that some posts are subject to prior approval by our editors, and we thank you for your patience.");
		}
		location.href=c_url;
		Boxy.get('.close').hide();
	});
}
