$(function() {
	$('#thumbs a').lightBox()
	
	$('#paging a').live('click', function() {
		var anchor   = $(this).attr('href'),
		    $samples = $('#samples'),
		    $loading = $('#loading-samples');
		$loading.show();
		$samples.slideUp('fast', function() {
			$samples.load(anchor + ' #samples', {}, function() {
				$loading.hide();
				$samples.slideDown('fast');
				$('#thumbs a').lightBox();
			});
		});
		return false;
	});
	
	$('#contact-form').submit(function() {
		var $form    = $(this),
		    post     = $form.serialize() + '&ajax=1',
			$loading = $('#loading-contact');
			
		$('#errors, #success').parent().slideUp('fast', function() {
			$(this).remove();
		});
			
		$loading.show();
		$.post('index.php', post, function(data) {
			$loading.hide();
			var data = eval('(' + data + ')'),
			    html = '';
			
			if (data.errors) {
				html += '<div class="hidden"><div id="errors"><p>Oops! The following errors occurred:</p><ul>';
				$.each(data.errors, function(key, message) {
					html += '<li><label for="' + key + '">' + message + '</label></li>';
				});
				html += '</ul></div></div>';
			}
			else {
				html += '<div class="hidden"><div id="success"><p>Thank you for your inquiry.<br />I will get back to you as soon as possible!</p></div></div>';
				$('#name, #email, #message').val('');
			}
			
			$(html).insertAfter($form.find('p:first')).slideDown('fast');
		});
		return false;
	});
});
