$(document).ready(hideAllOthers);
$(document).ready(setDropDownListeners);
$(document).ready(setCheckOtherListeners);
$(document).ready(setGradeDefaults);
$(document).ready(setGradeListeners);

function hideAllOthers()
{
/*
	$('#durations-other, #sizes-other, #camps-other, #types-other').each(function(i) {
		$(this).hide();
	});
*/
	$('#durations-other span, #sizes-other span, #camps-other span').each(function(i) {
		$(this).html('Please Specify');
		$(this).css('padding-left', '1em');
	});
}

function setDropDownListeners()
{
	$('select').each(function() {
		switch($(this).attr('name'))
		{
			case 'duration':
			case 'group_size':
			case 'camp':
				$(this).change(checkForOtherOption);
				checkForOtherSelected($(this));
		}
	});
}

function checkForOtherSelected(x)
{
	x.children().each(function() {
		c = $(this);
		if (c.attr('selected') == true)
		{
			if (c.html() == 'Other')
			{
				switch(x.attr('name'))
				{
					case 'duration':
						$('#durations-other').show();
						break;
					case 'group_size':
						$('#sizes-other').show();
						break;
					case 'camp':
						$('#camps-other').show();
						break;
				}
			}
			else
			{
				switch(x.attr('name'))
				{
					case 'duration':
						$('#durations-other').hide();
						break;
					case 'group_size':
						$('#sizes-other').hide();
						break;
					case 'camp':
						$('#camps-other').hide();
						break;
				}
			}
		}
	});
}

function setCheckOtherListeners()
{
	$(':checkbox').each(function() {
		switch($(this).attr('name'))
		{
			case 'type[]':
				if ($(this).attr('value') == 'Other')
				{
					if ($(this).attr('checked') == true)
					{
						$('#types-other').show();
					}
					$(this).click(function(e) {
						if ($(e.target).attr('value') == 'Other' && $(e.target).attr('name') == 'type[]')
						{
							if ($(e.target).attr('checked') == true)
							{
								$('#types-other').show();
							}
							else
							{
								$('#types-other').hide();
							}
						}
					});
				}
				break;
		}
	});
}

function checkForOtherOption(e)
{
	checkForOtherSelected($(e.target));
}

function setGradeDefaults()
{
	var start = getGradeStart();
	var end = getGradeEnd();

	$(':checkbox').each(function() {
		var t = $(this);
		if (t.attr('name') == 'grade_option[]' && t.attr('value') == 'range')
		{
			if (t.attr('checked') != true)
			{
				deactivateGradeRange();
			}
		}
		if (t.attr('name') == 'grade_option[]' && t.attr('value') == 'other')
		{
			if (t.attr('checked') != true)
			{
				disableGradeOther();
			}
		}
	});
}

function setGradeListeners()
{
	$(':checkbox').each(function() {
		var t = $(this);
		if (t.attr('name') == 'grade_option[]')
		{
			t.click(function(e) {
				if ($(e.target).attr('value') == 'range')
				{
					if (t.attr('checked') != true)
					{
						deactivateGradeRange();
					}
					else
					{
						activateGradeRange();
					}
				}
				else if ($(e.target).attr('value') == 'other')
				{
					if (t.attr('checked') != true)
					{
						disableGradeOther();
					}
					else
					{
						enableGradeOther();
					}				
				}
			});
		}
	});
}

function getGradeStart()
{
	var start = null;
	$('select').each(function() {
		var t = $(this);
		if (t.attr('name') == 'grade_start')
		{
			start = t;
		}
	});
	return start;
}

function getGradeEnd()
{
	var end = null;
	$('select').each(function() {
		var t = $(this);
		if (t.attr('name') == 'grade_end')
		{
			end = t;
		}
	});
	return end;
}

function activateGradeRange()
{
	start = getGradeStart();
	end = getGradeEnd();
	if (start.attr('disabled') == true)
	{
		start.removeAttr('disabled');
		end.removeAttr('disabled');
	}
}

function deactivateGradeRange()
{
	start = getGradeStart();
	end = getGradeEnd();

	start.attr('disabled', 'disabled');
	end.attr('disabled', 'disabled');
}

function enableGradeOther()
{
	var o = $('#grade_other');
	if (o.attr('disabled') == true)
	{
		o.removeAttr('disabled');
	}
}

function disableGradeOther()
{
	$('#grade_other').attr('disabled', 'disabled');
}
