// At what age are we letting people through?
var minimumAge = 14;

// Destination variables
var links = new Array();
var currentIdx = -1;

// Debug?
var debug = false;

function checkBday() {
	// Get user-entered date
	var month = $('#coppaBdayMonth').val();
	var day = $('#coppaBdayDate').val();
	var year = $('#coppaBdayYear').val();

	// User's birthdate
	var bd = new Date();
	bd.setTime(Date.parse(month+'/'+day+'/'+year));
	
	// minimumAge as of today
	var td = new Date();
	td.setFullYear(td.getFullYear()-minimumAge);

	// Is the user minimumAge?
	if (bd <= td) {
		// Yes: let them go on
		var expDate = new Date();
		$.cookie('oldEnough', 'true', {expires:1000});
		document.location = links[currentIdx];
	}
	else { 
		// Nope. Oops!
		$('#ageForm').addClass("hidden");
		$('#tooYoung').removeClass("hidden");
	}
}

function addThickbox() {
	// If we're not debugging and the cookie is set, return
	if (!debug && ($.cookie('oldEnough')=='true')) return;
	
	// If user hasn't already said he's old enough, add thickbox foo
	$(".checkAge").each(function (i) {
		idx = links.push(this.href);
		$(this).click(function() {
			setIdx(idx-1);
		});
		$(this).attr('href','#TB_inline?KeepThis=true&inlineId=tboxAge&width=300&height=155');
		$(this).attr('title','Please confirm your age');
		$(this).addClass('thickbox');
	});
	setUpDatePicker();
}

function setIdx(idx) {
	currentIdx = idx;
}

function setUpDatePicker() {
	var today = new Date();
	var year = today.getFullYear();
	
	for (i=1;i<=31;i++) 
		$("#coppaBdayDate").append('<option value="'+i+'">'+i+'</option>');
	for (i=year;i>=(year-100);i--)
		$("#coppaBdayYear").append('<option value="'+i+'">'+i+'</option>');

	($('#coppaBdayDate')[0]).selectedIndex = today.getDate() - 1;
	($('#coppaBdayMonth')[0]).selectedIndex = today.getMonth();
	($('#coppaBdayYear')[0]).selectedIndex = today.getFullYear() - year;
}
