window.fbAsyncInit = function() 
{
	FB.init({appId: 'a2748e2ded2a48426a2a82560a5be916', status: true, cookie: true, xfbml: true});
	
	var RMHC = window.RMHC || {};
	
	RMHC.comment = {
		init : function()
		{
			FB.Event.subscribe('auth.sessionChange', function(response) {
				window.location.reload();
			});
			
			$('#fb-logout').click(function(){
				FB.logout();
				return false;
			});
			
			$('#facebook-btn').click(function(){
				FB.login();
				return false;
			});
			
			FB.getLoginStatus(function(response)
			{	
				var form = $('#comment-form form');
				
				//Check if we are on a post story page or comment page
				
				if (form.length > 0) // comment form found
				{
					var rules = { Comment : 'required' };
					
					//User is not logged into Facebook, so they have fillout name and email
					if (!response.session)
					{
						rules.Name = 'required';
						rules.email = 'required';
					}
					
					form.validate({
						submitHandler : RMHC.comment.onCommentFBSubmit,
						rules : rules
					});
				}
				else if ($('#postStory form').length > 0)
				{
					form = $('#postStory form');
					
					var rules = { Title : 'required',
							categoryID : 'required',
							BlogPost : 'required',
							Terms : 'required'};
				
					var messages = { Terms : 'You must agree to the Terms & Conditions for your story to post.' };
					
					if (!response.session)
					{
						rules.Author = 'required';
						rules.email = {required: true, email: true};
						rules.phone = 'required';
					}
					
					form.validate({
						submitHandler : RMHC.comment.onStoryFBSubmit,
						rules : rules,
						messages : messages
					});
				}
			});
			
			$('#hideComments').click(function()
			{
				$('#CommentHolder').toggle();
				$('#comment-form').toggle();
				return false;
			});
		},
		
		onStoryFBSubmit : function(form)
		{
			FB.getLoginStatus(function(response)
			{
				if (response.session)
				{
					var story = $('#BlogPost textarea').get(0).value;
					
					FB.ui({
						method : 'stream.publish',
						message: 'I just posted this Ronald McDonald House Charities story',
						attachment : {
							name: $('#Title input').val(),
							description: story,
							href: window.location.protocol + '//' + window.location.host + '/friends-of-rmhc/your-stories/'
						},
						user_message_prompt : 'Share your comment on Facebook'
					},
					function(response)
					{
						form.submit();
					});
				}
				else
				{
					form.submit();
				}
			});
			
			return false;
		},
		
		onCommentFBSubmit : function(form)
		{
			var publish = $('#Publish input');
			
			if (publish.length <= 0)
			{
				form.submit();
				return;
			}
			
			var checkbox = publish.get(0);

			if (!checkbox.checked)
			{
				form.submit();
				return;
			}
			
			var comment = $('#Comment textarea').get(0).value;
			
			FB.ui({
				method : 'stream.publish',
				message: 'Check out this Ronald McDonald House Charities story',
				attachment : {
					name: document.title,
					description: comment,
					href: window.location.toString()
				},
				user_message_prompt : 'Share your comment on Facebook'
			},
			function(response)
			{
				form.submit();
			});
			
			return false;
		}
	};
	
	RMHC.comment.init();
};