$(function() {
	var originalvalue = $('#codebox').val();
	$(function(){$("form input").keypress(function(e){if((e.which&&e.which==13)||(e.keyCode&&e.keyCode==13)){$('#submitbutton').click();return false;}else{return true;}});});
	$('#submitbutton').click(function() {
		var raw = $('#codebox').val().replace(/[^a-zA-Z0-9]+/g,'');
		if (raw != originalvalue.replace(/[^a-zA-Z0-9]+/g,''))
		{
			$.post("code", {code : raw}, function(data) {
				$("#coderesult").slideDown('slow')
				$("#coderesult").html("<img src='/images/animations/loading2_fff.gif' alt='loading' />")
				setTimeout(function() { $("#coderesult").html(data); },3000);
				$("#codebox").val('insert code here');
			});
			$('#codebox').blur();
		}else{
			$('#codebox').blur();
			return false;
		}
	});
	
	$('#codebox').focus(function(){if($.trim($(this).val())==originalvalue){$(this).val('');}});
	$('#codebox').blur(function(){if($.trim($(this).val())==''){$(this).val(originalvalue);}});

	$('#codebox').bind("change keyup", function() {
		var raw = $(this).val().replace(/[^a-zA-Z0-9]+/g,'');
		var length = raw.length;
		var refined = "";

		if (length > 25) length = 25;

		for(var i=0;i<length;i++)
		{
			if (raw.length)
			{
				if (i%5 == 0 && i>0)
				{
					refined = refined+"-";
				}
				refined = refined+raw.charAt(i);
			}
		}
		if (length > 0)
		{
			if (length%5==0) $(this).css("color","#000");
			else $(this).css("color","#999");
			$(".codeheight").css('height','auto');
			$('#submitbutton').slideDown('slow');
		}else{
			$('#submitbutton').slideUp('slow');
			$(".codeheight").css('height','10px');
		}
		if (refined != 'undefined')
		$(this).val(refined);
	});	
});

