$.fn.focusNextInputField = function() {
	return this.each(function() {
		var fields = $(this).parents('form:eq(0),body').find('button,input,textarea,select');
		var index = fields.index( this );
		if ( index > -1 && ( index + 1 ) < fields.length ) {
			fields.eq( index + 1 ).focus();
		}
		return false;
	});
};

$.fn.focusNextRackTile = function() {
	id = parseInt($(this).attr("id").replace("t", ""));
	if (id < 7)
	{
		$("#t"+(id+1)).focus().select();
	}
	return false;
};
$.fn.focusPrevRackTile = function() {
	id = parseInt($(this).attr("id").replace("t", ""));
	if ((id > 1) && (id <= 8))
	{
		$("#t"+(id-1)).focus().select();
	}
	return false;
};

$(document).ready(function() {
	
	$("#t1").focus().select();
	
	$(".t").focus(function() {
		$(this).select();
	});
	
	$(".t").click(function() {
		$(this).select();
	});
	
	$(".t").keydown(function(e) {
		if (e.which == 37)
		{
			$(this).focusPrevRackTile();
			return false;
		}
		else if (e.which == 39)
		{
			$(this).focusNextRackTile();
			return false;
		}
	});
	
	$(".t").keyup(function(e) {
		
		if ((e.which > 64 && e.which < 91) || (e.which > 96 && e.which < 123) || (e.which == 42 || e.which == 95 || e.which == 56 || e.which == 0))
		{
			if ($(this).attr("value").length == 1)
			{
				$(this).focusNextRackTile();
				$(this).attr("value", $(this).attr("value").toUpperCase());
			}
		} else if (e.which == 8)
		{
			$(this).focusPrevRackTile();
		} else if (e.which == 9 || e.which == 16 || e.which == 224 || e.which == 18 || e.which > 36 && e.which < 41)
		{
			// do nothing
		} else
		{
			$(this).attr("value", "");
		}
	});
	
	$("#clearrack").click(function() {
		$(".t").attr("value", "");
		$("#t1").focus().select();
		return false;
	});
	
	$("#wordlist").tablesorter({
		widgets: ['zebra'],
		headers: { 3: { sorter: false} },
		sortList: [[2,1]]
	});
	$("#all_letters").tablesorter({
		widgets: ['zebra']
	});

	var $scrollingDiv = $("#scrollingDiv");
	$(window).scroll(function(){			
		$scrollingDiv
			.stop()
			.animate({"marginTop": ($(window).scrollTop() + 30) + "px"}, "slow" );			
	});

});


















