$(function(){
	$('#comment_form').submit(function(){
		var log = $('#log_res');
		var page =  $('#page').attr('value'); //current page url
		var fb_uid = $('#fb_uid').attr('value'); //facebook user id value
		//var action = $('#newRET').attr("value");
		var comment = $('#comment');
		
		//FB Publish Content
		var title = $('#fb_title').text();
		var summary = $('#fb_summary').text();
		var image = $('#fb_image_file').attr("src");
		var caption = ' commented on the blog post <a href=\"'+page+'\" title=\"'+title+'\">'+title+'</a> on <a href=\"http://www.ccm.org.uk\" title="Christ Church Manchester Homepage">Christ Church Manchester</a>';
		
		$('#comment_form').ajaxSubmit({
			success: function(data){
				if(fb_uid != ''){
					var attachment = {
						'name':	title, 
						'href':	page, 
						'caption': '{*actor*}', 
						'description': summary, 
						'media':[{
							'type': 'image', 
							'src': 'http://www.ccm.org.uk'+image, 
							'href': page
						}]
					};
					var action_links = [{
						'text'	: 'Read post',
						'href'	: page
					}]
					
					FB.Connect.streamPublish(comment.attr("value"), attachment, action_links, null, "Your comment");
				}
				FB.XFBML.Host.parseDomTree();
				comment.attr("value", "");
				log.html("<p><b>Your comment has gone for moderation.<br />If it passes it will appear here soon.</b></p>");
			}
		});	
		return false;
	});	
});

//method to update comment form
function update_usercoment_box() {
	var comment_user_box = $("#comment_user");
	var user_pref_chk_box =  $("#user_pref_chk");
	var fbc_login_area =   $("#fbc_login_area");
 	var page =  $('#page').attr('value');
 	
	fbc_login_area.html("<fb:profile-pic uid='loggedinuser' facebook-logo='true' linked='true' size='square' /><p>Welcome <fb:name uid='loggedinuser' useyou='false'></fb:name>, You are signed in with your Facebook account.</p><p><a href='#' onclick='FB.Connect.logoutAndRedirect(\""+page+"\")' class='underline'>Click here to sign out.</a></p>");
	 
	comment_user_box.html("<input type=\"hidden\" name=\"name\" id=\"name\" value=\"Facebook user\" /><input type=\"hidden\" name=\"email\" id=\"email\" value=\"anonymous@facebook.com\" />");
	
	$("#fb_uid").attr("value", FB.Connect.get_loggedInUser());
	user_pref_chk_box.hide();
	FB.XFBML.Host.parseDomTree();
}