$(document).ready(function() {
	var root = $('#header h1 a').attr('href');

	/* Some nice form things */
	$('form').find('input:first').focus();
	
	$('form').SymphonyInlineValidation({
		canofspam: true,
		url: root + "special/validation"
	});

	if($('#membership-form').length == 1) {
		var member	= $('#membership-form');

		member.find('select:first').focus();		
		
		$('div.field-checklist input').click(function() {
			var self = $(this);
			if(self.attr("checked") == false) {
				self.parent().removeClass("checked");
			} else {
				self.parent().addClass("checked");
			}
		})
		
	}

	if($('#contact-form').length == 1) {
		var contact = $('#contact-form');
		var	resume	= $('div.field-file', contact),
			options = $('div.field-select', contact);
			
		// 	Hide Resume by default
		resume.hide();

		//	Bind to the change event of the Enquiry Type field
		options.change(function() {
			//	Careers? Hell yes, do the drop
			if($('option:selected',$(this)).val() == "Careers Enquiry") {
				resume.slideDown("normal");
			} else {
			//	Not careers, pull back up in the shell
				resume.slideUp("fast");
			}
		});
	}


	// Sign up form
	$('a.sign-up, ul.public li.public.shot').click(function() {
		var box = null;
		var attach_form = function() {
			$('.close, .boxy-modal-blackout').click(function() {
				box.hideAndUnload(); return false;
			});

			$('#social-member').SymphonyInlineValidation({
				canofspam: true,
				url: root + "special/validation"
			});

			$('#social-member').ajaxForm({
				target:		'#signup-container',
				url:		root + 'special/create/',
				success:	attach_form
			});
		}

		// Load form:
		$.get(root + 'special/create/', null, function(data) {
			// Load box:
			box = new Boxy(
				'<div id="signup-container" class="formatted">' + data + '</div>',
				{
					title:		"Sign up",
					modal:		true,
					show:		true,
					afterShow:	attach_form
				}
			);

		});

		return false;
	});

	// Verify form
	$('a.login').click(function() {
		var box = null;
		var attach_form = function() {
			$('.close, .boxy-modal-blackout').click(function() {
				box.hideAndUnload(); return false;
			});

			$('#login-form').SymphonyInlineValidation({
				canofspam: true,
				url: root + "special/validation"
			});

			$('#login-form').ajaxForm({
				target:		'#signup-container',
				url:		root + 'special/verify/',
				success:	attach_form
			});
		}

		// Load form:
		$.get(root + 'special/verify/', null, function(data) {
			// Load box:
			box = new Boxy(
				'<div id="signup-container">' + data + '</div>',
				{
					title:		"Login",
					modal:		true,
					show:		true,
					afterShow:	attach_form
				}
			);
		});

		return false;
	});

});