$(document).ready(function(){
	// clear the timeout
	registerUsernameTimer = 0;
	
	// show hide code for the fieldsets
	$('.jquery-expando').bind('click',function(e) {
		if ($(this).parent().next().css('display') == 'none') {
			$(this).parent().next().show('slow');							// fieldset & explanation text
			$(this).attr('src','/images/btn_previous_small.png');	// change the button
			$(this).parent().parent().removeClass('unactive');				// remove the unactive class
			$(this).parent().parent().addClass('active');					// set the fieldset to active
		} else {
			$(this).parent().next().hide('slow');							// fieldset & explanation text
			$(this).attr('src','/images/btn_next_small.png');		// change the button
			$(this).parent().parent().removeClass('active');					// remove the active class
			$(this).parent().parent().addClass('unactive');					// set the fieldset to unactive
		}
	});
	
	// fake a couple of clicks to close the address and netwok options
	$('#address .jquery-expando').trigger('click');
	$('#network .jquery-expando').trigger('click');
	
	// add in the auto username check
	$('#username').bind('keyup',
		function() {
			if ($('#username').attr('value').length > 3) { // is there enough text to check if the username is valid?
				// change the "bad" image background for the "waiting" one
				flagCompulsoryField('#username','waiting');
				
				clearTimeout(registerUsernameTimer);
				registerUsernameTimer = setTimeout("checkData('#username','un')", 1000);	
			} else {
				// change the "required" image background for the "checking" one
				flagCompulsoryField('#username', 'error');
			}
		}
	);
	
	// add in the auto email check
	$('#email').bind('keyup',
		function() {
			if ($('#email').attr('value').length > 3) { // is there enough text to check if the email is valid?
				// change the "bad" image background for the "waiting" one
				flagCompulsoryField('#email','waiting');
				
				clearTimeout(registerUsernameTimer);
				registerUsernameTimer = setTimeout("checkData('#email','em')", 1000);	
			} else {
				// change the "required" image background for the "checking" one
				flagCompulsoryField('#email', 'error');
			}
		}
	);
	
	// if the user gets binned back to this page, re-run the username checker
	if($('#username').attr('value').length > 3) checkData('#username','un');
	if($('#email').attr('value').length > 3) checkData('#email','em');
});

