$(document).ready(function()
{
	inbox.initialise();
});

var inbox =
{
	initialise: function()
	{
		this.set_up_what_does_this_mean();
		this.set_up_delete_button();
	},
	
	set_up_what_does_this_mean: function()
	{
		// Hide the information initially
		$('span.what_does_this_mean span').hide();
		
		// Show the information on click
		$('span.what_does_this_mean a').click(function()
		{
			$(this).next('span').fadeIn('fast');
			return false;
		});
	},
	
	set_up_delete_button: function()
	{
		var self = this;
	
		$('a.delete').click(function()
		{
			var failsafe = $(this).attr('href');
			var message_id = $(this).attr('rel');
			var do_delete = (message_id) ? true : false;
		
			 // Delete if there is a specified message id
			if (!do_delete) return;
		
			if ($(this).hasClass('not_replied'))
			{
				// Prompt the user to respond to the email
				do_delete = confirm('Are you sure you wish to delete? You do not appear to have replied to this message. As a courtesy please consider at least sending our standard auto response. Press cancel to return and respond, or ok to continue with deletion');
			}
			
			if (do_delete)
			{
				var failsafe_timer = setTimeout(function(){document.location.href = failsafe;}, 3000);
				$('body').css('cursor', 'wait');
			
				$.getJSON(self.static_files_server+'/flatshare/ajax.pl', {action:'delete_inbox_message', message_id:message_id}, function(json)
				{
					if (json.response_type == 'success')
					{
						// alert(json.response);
						$('#email_'+message_id).fadeOut();
						clearTimeout(failsafe_timer);
						$('body').css('cursor', 'default');						
					}
					else
					{
						document.location.href = failsafe;
					}
				});	
			}
			
			return false;
		});
	}
};
