var site_url = "http://www.coffeelab.co.nz/";
function feedback(text, css, id, hide)
{
	if ( ! id)
	{
		id = 'feedback';
	}
	else
	{
		id  = id;
	}
	
	! css ? css = 'information'  : css = css;
	
	if (hide)
	{
		$('#'+id).html("").removeClass().hide();
	}
	else
	{
		$('#'+id).html(text+"<a href='#' class='close' title='Close Notification'>Close</a>").removeClass().addClass("notify "+css).show();
	}
}

$(document).ready(function()
{
	$('a.close').live('click', 
		function(){$(this).parent().html('').removeClass().addClass("hidden");
		return false;
	});
	
	$('.newsletter').click(function(){
		var email    = $('#newsletterInput').val();
		
		if ( ! email)
		{
			feedback('Please enter an email address', 'attention');
		}
		else
		{
			jQuery.ajax({		
				type: 'POST',
				url: site_url+'site/newsletter',
				data: 'email='+email,
				beforeSend: function(){feedback('Please wait - contacting server.')},
				error:function(msg){feedback('Server unreachable.', 'error');},
				success: function(result){
					if (result <= 50)
					{
						feedback('Subscription not added. Please enter your email address', 'attention');
					}
					else if (result == 75)
					{
						feedback('Subscription not added. Invalid email address format.', 'attention');
					}
					else if (result == 100)
					{
						feedback('Subscription not added. Database server error.', 'attention');
					}
					else if (result == 200)
					{
						$('#newsletterInput').val('');
						feedback('Subscription added. ', 'success');
					}
				}
			});
		}
		return false;
	});
	
	$('a.calendar-btn').live('click', function(){
		jQuery.ajax({		
			type: 'POST',
			url: $(this).attr('href'),
			success: function(result){
				$('#blogCalendar').html(result);
			}
		});
		return false;
	});
	
	$('#commentForm').submit(function(){
		var name    = $('#name').val();
		var email   = $('#email').val();
		var comment = $('#comment').val();
		var type    = $('#type').val();

		if ( ! name || ! email || ! comment)
		{
			feedback('Please fill in all required fields', 'attention');
		}
		else
		{
			jQuery.ajax({		
				type: 'POST',
				url: site_url+'site/'+type+'/submit_comment',
				data: $('#sendCommentForm').serialize(),
				beforeSend: function(){feedback('Please wait - contacting server.')},
				error:function(msg){feedback('Server unreachable.', 'error');},
				success: function(result){
					if (result <= 50)
					{
						feedback('Please fill in all required fields', 'attention');
					}
					if (result == 75)
					{
						feedback('Please enter a valid email address. This will never be displayed.', 'attention');
					}
					else if (result != 200)
					{
						feedback('Comment not submitted. Database server error.', 'attention');
					}
					else if (result == 200)
					{
						feedback('Your comment was added. It will display once it has been approved.', 'success');
						$('#commentForm').html('');
					}
				}
			});
		}
		return false;
	});
	
	$('#bioTypeList').change(function(){
		$('#bioFilterForm').submit();
		//window.location = site_url+'educate/bios/'+$(this).val();
	});
});
