(function($){
	$(document).ready(function()
	{
		FB.init("a2748e2ded2a48426a2a82560a5be916", "/xd_receiver.htm", { });
		hope.init();
	});
	
	var hope = {
		storyPrefix: 'Form_postStoryForm',
		commentPrefix: 'HopeStoriesCommentInterface_Form_HopeStoriesCommentsPostCommentForm',
		
		init : function()
		{
			$('a#facebook-btn').click(hope.onFBBtnClick);
			$('a#fb-logout').click(hope.onFBLogout);
			
			$('input#catSelectBtn').hide();
			$('select#category').change(function()
			{
				this.form.submit();
			});
			
			hope.highliteStoryNav();
			
			if(typeof $.fn.validate == 'function' && $('form#'+hope.commentPrefix).length > 0)
			{
				//Disable SilverStripe's prototype validation
				var form = $('form#'+hope.commentPrefix).get(0);
				form.bypassValidation = 'true';
				
				var rules = { Comment : 'required'};
				
				if ($('#'+hope.commentPrefix+'_Name').length > 0)
				{
					rules.Name = 'required';
					rules.email = 'required';
				}
				
				$('form#'+hope.commentPrefix).validate({
					submitHandler : hope.onCommentFBSubmit,
					rules : rules
				});
			}
			
			if(typeof $.fn.validate == 'function' && $('form#'+hope.storyPrefix).length > 0)
			{
				//Disable SilverStripe's prototype validation
				var form = $('form#'+hope.storyPrefix).get(0);
				form.bypassValidation = 'true';
				
				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 ($('#'+hope.storyPrefix+'_Author').length > 0)
				{
					rules.Author = 'required';
					rules.email = {required: true, email: true};
					rules.phone = 'required';
				}
				
				$('form#'+hope.storyPrefix).validate({
					submitHandler : hope.onStoryFBSubmit,
					rules : rules,
					messages : messages
				});
			}
		},
		
		onStoryFBSubmit : function(form)
		{			
			var comment = $('#'+hope.storyPrefix+'_BlogPost').get(0).value;
			var data = {
				'blog' : window.location.protocol + '//' + window.location.host + '/friends-of-rmhc/your-stories/'
			};
			FB.Connect.showFeedDialog(113498313833, 
										data, 
										[], 
										'',
										null, 
										FB.RequireConnect.require, 
										hope.onEndFeedDialog, 
										'You wrote',
										{value: comment});
		},
		
		onCommentFBSubmit : function(form)
		{
			var publish = $('#'+hope.commentPrefix+'_Publish');
			
			if (publish.length <= 0)
			{
				form.submit();
				return;
			}
			
			var checkbox = publish.get(0);

			if (!checkbox.checked)
			{
				form.submit();
				return;
			}
			
			var comment = $('#'+hope.commentPrefix+'_Comment').get(0).value;
			var data = {
				'post' : (window.location.toString().split('#'))[0],
				'blog' : window.location.protocol + '//' + window.location.host + $('#'+hope.commentPrefix+'_Blog').get(0).value,
				'post_name' : document.title
			};
			FB.Connect.showFeedDialog(113498088833, 
										data, 
										[], 
										'',
										null, 
										FB.RequireConnect.require, 
										hope.onEndFeedDialog, 
										'Comment on ' + document.title,
										{value: comment});
		},
		
		onEndFeedDialog : function()
		{
			$('form#'+hope.commentPrefix+',form#'+hope.storyPrefix).get(0).submit();
		},
			
		onFBBtnClick : function(e)
		{
			//thirtyWays.initGuestForm();
			
			//add FB stuff
			FB.Connect.requireSession(hope.onFBUserLogin);
			
			return false;
		},
		
		onFBUserLogin : function()
		{
			window.location.reload();
		},
		
		onFBLogout : function(e)
		{
			FB.Connect.logoutAndRedirect(window.location.toString());
			return false;
		},
		
		highliteStoryNav : function()
		{
			var url = window.location.toString();
			
			if (url.match('your-stories/post'))
				$('a.share-a-story-2').addClass('current');
			
		}
	};
	
})(jQuery);