
// TODO: only call necessary functions

function scoreboard_general()
{
	// handler for our custom form buttons
	$('form span.button.submit').click(function(){
		$(this).parents('form').submit()
	})

}

function scoreboard_mark()
{
	// handler for table row & correct answer highlights
	$('table#entries tbody tr')
		// table row hover: on
		.mouseover(function(){
			$(this).addClass('over')
		})
		// table row hover: off
		.mouseout(function(){
			$(this).removeClass('over')
		})
		// table row select: highlight and show tick
		.click(function(){
			if( $(this).is('.selected') ) {
				// deselect row
				$(this)
					.removeClass('selected')
					.find('input[name*="[correct]"]').val('N')
			}
			else {
				// select row
				$(this)
					.addClass('selected')
					.find('input[name*="[correct]"]').val('Y')
			}
		})

	// handler for 'select all'
	$('span.button#select-all').click(function(){
		$('table#entries tbody tr')
			.addClass('selected')
			.find('input[name*="[correct]"]').val('Y')
	})

	// handler for 'select none'
	$('span.button#select-none').click(function(){
		$('table#entries tbody tr')
			.removeClass('selected')
			.find('input[name*="[correct]"]').val('N')
	})

	// handler for matching string
	$('form#controls').submit(function(){
		var keyword = $(this).find('input#quickmark-text').val().toLowerCase()
		if ( keyword.length > 0 )
		{
			$('table#entries tbody tr:has(td.answer)').each(function(){
				var td = $(this).find('td.answer').text().toLowerCase()
				if ( td.indexOf(keyword) != -1 )
					$(this)
						.addClass('selected')
						.find('input[name*="[correct]"]').val('Y')
			})
		}
		return false
	})



}

function scoreboard_login()
{
	$('fieldset#login input#password').focus()
}

function scoreboard_setup()
{
	$('form#question-setup fieldset:last input[type="text"]').focus()

	// handler for adding/removing questions
	$('#question-add').click(function(){
		var n = $('form#question-setup fieldset').size()

		// new question fieldset
		$('form#question-setup fieldset:last')
			.after('<fieldset style="display:none;"><legend>Question ' + (n+1) + '</legend>' +
				'<input type="hidden" name="questions[' + n + '][number]" value="' + n + '" />' +
				'<input type="text" name="questions[' + n + '][name]" />' +
				'<input type="hidden" name="questions[' + n + '][running]" value="Y" />' +
				'<div class="question-remove" title="Remove this question"></div>' +
				'</fieldset>')

		// get newly added fieldset
		var form = $('form#question-setup')
		var fieldset = form.find('fieldset:last')

		// animates new fieldset
		fieldset.slideDown(200, function(){
			// focus new question field
			fieldset.find('input[type="text"]').focus()
		})


		// handler for removing this question fieldset
		fieldset.find('div.question-remove').click(function(){
			// hide the fieldset
			$(this).parents('fieldset').slideUp(200, function(){
				// remove the fieldset
				$(this).remove()
				// renumber the remaining fieldsets
				form.find('fieldset').each(function(i){
					$(this).find('legend').text('Question ' + (i+1))
					$(this).find('input[name$="[number]"]')
						.attr('name', 'questions[' + i + '][number]')
						.attr('value', i)
					$(this).find('input[name$="[name]"]')
						.attr('name', 'questions[' + i + '][name]')
					$(this).find('input[name$="[running]"]')
						.attr('name', 'questions[' + i + '][running]')
				})
			})
		})
	})

}

