$(function(){
	// for step 1
	$('#main img').click(function(){
		window.location = 'index?step=2&character='+$(this).attr('character');
	});


	// for step 2
	$('#gender, #play').buttonset();
	$('#finish').button();
	$('#dialog').dialog({
		modal: true,
		autoOpen: false
	});

	// hacks to get things looking correctly
	$('#finish').removeClass('ui-state-default').addClass('ui-state-active');
	$('#finish').hover(function(){$(this).removeClass('ui-state-active');}, function(){ $(this).addClass('ui-state-active');});

	$('#finish').click(function(){
		var years = $('#years').val();
		if (years == '' || isNaN(years)){
			alert('Please enter your age.');
		}
		else if ($('#years').val() > 18){
			alert('You are a bit too old for kids.usablehealth.com. Try our regular site at http://usablehealth.com');
		}
		else if ($(':radio[name=gender]:checked').val() == undefined){
			alert('Please select whether you are a boy or a girl.');
		}
		else if ($(':radio[name=out]:checked').val() == undefined){
			alert('Please select how often you play outside.');
		}
		else {
			// good data, submit to signup ajax
			$.post('ajax_php/signup.php', {
				gender: $(':radio[name=gender]:checked').val(),
				age: $('#years').val(),
				activity: $(':radio[name=out]:checked').val()
			}, function(data){
				if (data != -1){
					window.location = 'location';
				}
				else {
					$('#dialog').dialog('option', 'buttons', {'Close': function(){$(this).dialog('close');}}).text("There was an error.");
					$('#dialog').dialog('open');
				}

			}, 'text');
		}
	});

});

