var TIME_BUFFER = 1 * 24 * 60 * 60 * 1000;
var coachList = new Array();
var months = new Array();
months["Jan"] = 0;
months["Feb"] = 1;
months["Mar"] = 2;
months["Apr"] = 3;
months["May"] = 4;
months["Jun"] = 5;
months["Jul"] = 6;
months["Aug"] = 7;
months["Sep"] = 8;
months["Oct"] = 9;
months["Nov"] = 10;
months["Dec"] = 11;

$(function(){
	coachList['initiation'] = '[{"n":"Matthew Hage","t":"NE121"},{"n":"Jeff Gagne","t":"NE122"},{"n":"Dean Starchuk","t":"NE123"}]';
	coachList['initiation2'] = '[{"n":"Dany Cassivi","t":"NE124"},{"n":"Wilsons","t":"NE125"},{"n":"Nevin Filipchuk","t":"NE134"}]';
	coachList['novice'] = '[{"n":"Chris Stolte","t":"NE126"},{"n":"James Dyment","t":"NE127"},{"n":"Glenn Sommerville","t":"NE133"}]';
	coachList['atom'] = '[{"n":"Jason Jenkinson","t":"NE128"},{"n":"Art Truscott","t":"NE129"}]';
	coachList['peewee'] = '[{"n":"Jason Hall","t":"NE130"},{"n":"Rod Slobinyk","t":"NE131"}]';
	coachList['bantam'] = '[{"n":"Rob Ballantyne","t":"NE132"}]';
	
	renderTeamSelecter();
	
	$('.dateToggle').click(function() {
		showHidePastDates(this, this.id.substring(6));
	});
	return true;
});

$('#agegroup').live("change", function(){
	var age=$('#agegroup option:selected').val();

	$('#coach')
		.find('option')
		.remove()
		.end()
		.append('<option value="">-- Choose --</option>');

	if (age == '') {
		$('#coachDiv').hide();
	} else {
		//populate coach select list options based on age group selected
		var opts = eval(coachList[age]);

		$.each(opts, function(index, value) {
			$('#coach').append(
				$('<option></option>').val(this.t).html(this.n + ' (' + this.t + ')')
			);
		});

		$('#coachDiv').show();
	}
});
$('#coach').live("change", function(){
	var age = $('#agegroup option:selected').val() + '.html#';
	if(window.location.href.indexOf('age-groups/') == -1) {
		age = 'age-groups/' + age;
	}
	var team=$('#coach option:selected').val();
	window.location.href = age + team;
});

/* Assumes a div exists with id="teamSelecter" */
function renderTeamSelecter() {
	var newHtml = '<span class="subhead">Team Schedules - select your team</span><br/>';
	newHtml += 'Age Group: <span id="agechoice">';
	newHtml += '<select name="agegroup" id="agegroup">';
	newHtml += '<option value="">-- Choose --</option>';
	newHtml += '<option value="initiation">Initiation I (6 yrs &amp; under)</option>';
	newHtml += '<option value="initiation2">Initiation II (6 yrs &amp; under)</option>';
	newHtml += '<option value="novice">Novice (7 &amp; 8 yrs)</option>';
	newHtml += '<option value="atom">Atom (9 &amp; 10 yrs)</option>';
	newHtml += '<option value="peewee">Peewee (11 &amp; 12 yrs)</option>';
	newHtml += '<option value="bantam">Bantam (13 &amp; 14 yrs)</option>';
	newHtml += '</select>';
	newHtml += '</span>';

	newHtml += '<div style="display:none" id="coachDiv">';
	newHtml += 'Coach:';
	newHtml += '<select name="coach" id="coach">';
	newHtml += '</select>';
	newHtml += '</div>';
	
	$('#teamSelecter').html(newHtml);
	return true;
}

function showHidePastDates(toggle, schedule) {
	var now = new Date();
	$('#' + schedule + ' tr').each(function(index) {
		if (index > 0) {
			var dateStr = $(this).find(':eq(1)').html();
			var bits = dateStr.split('-');
			var mo = months[bits[1]];
			var iceTime = new Date('20' + bits[2], months[bits[1]], bits[0]);
			if (now.getTime() > (iceTime.getTime() + TIME_BUFFER)) {
				$(this).toggle()
			}
		}
	});
	if (toggle.innerHTML == 'Show Full Schedule') {
		toggle.innerHTML = 'Hide Past Dates';
	} else {
		toggle.innerHTML = 'Show Full Schedule';
	}
}

