var randomAnimation;
var acronymLength;

function inputFocus(theInput) {
	if (theInput.value == 'here') {
		theInput.value = '';
		theInput.style.color = 'black';
	}
}

function inputBlur(theInput) {
	if (theInput.value == '') {
		theInput.style.color = '#b7b7b7';
		theInput.value = 'here';
	}
}

function inputUpdate(theInput) {
	var filter = /[^A-Z]/gi;
	if (filter.test(theInput.value)) {
		$('#error').html("Only letters from A to Z, please.").animate({marginBottom: 0, opacity: 1}, 500);
		setTimeout("$('#error').animate({marginBottom: -30, opacity: 0}, 500);",2000);
	}
	var newAcronym = theInput.value.toUpperCase().replace(filter, '');
	theInput.value = newAcronym;
	
	 if (newAcronym != '' && newAcronym.length >= 2) {
		$("#meaning .inner div").animate({opacity: 0}, 200, function() {
			$.get("meaning.php",
				{ acronym: newAcronym },
				function(data) {
					$("#meaning .inner div").html(data).animate({opacity: 1}, 1000);
					//definitionLookup();
				}
			);
		});
	} else if (newAcronym.length >= 1 && newAcronym.length < 2) {
		$('#error').html("At least two characters, please.").animate({marginBottom: 0, opacity: 1}, 500);
		setTimeout("$('#error').animate({marginBottom: -30, opacity: 0}, 500);",2000);
		$("#meaning .inner div").animate({opacity: 0}, 1000);
		setTimeout('$("#meaning .inner div").html("");',1000)
	} else {
		$("#meaning .inner div").animate({opacity: 0}, 1000);
		setTimeout('$("#meaning .inner div").html("");',1000)
	}
}

function refresh() {
	theInput = document.getElementById('acronymInput');
	if (theInput.value != "" && theInput.value != "here") inputUpdate(document.getElementById('acronymInput'));
}


// This is unfinished.
// Also, Google is evil and won't let us use their engine.

/*function definitionLookup() {
	$("#meaning span").each(
		function(i) {	
			var theWord = this;
			$.get("google.php",
				{ term: (theWord.innerHTML) },
				function(data) {
					
					var wordPos = $(theWord).position({ scroll: false });
					wordPos['top'] += 40;
					
					var wordLeft = $(theWord).outerWidth();
					wordLeft = Math.round( wordPos['left'] + (wordLeft/2) - 80 );
					
					$(theWord).after( "<div id='tooltip"+i+"' style='top: "+wordPos['top']+"px; left: "+wordLeft+"px;' class='tooltip'>"+data+"</div>" );
					
					$(theWord).hover(function(){
						$("#tooltip"+i).css({ display:'block' }).animate({ opacity: 1 }, 500);
					},function(){
						$("#tooltip"+i).animate({ opacity: 0 }, 500);
						setTimeout("document.getElementById('#tooltip"+i+").style.display = 'none';",500);
					});
				}
			);
		}
	);
}*/

function randomAcronym() {
	randomAnimation = 0;
	acronymLength = Math.round( 2+2*Math.random() );
	randomizeAcronym(acronymLength);
	document.getElementById('acronymInput').style.color = '#b7b7b7';
}

function randomizeAcronym() {
	var characters = "ABCDEFGHIJKLMNOPQRSTUVWXTZ";
	var acronym = '';
	for (var i=0; i<acronymLength; i++) {
		var rnum = Math.floor(Math.random() * characters.length);
		acronym += characters.substring(rnum,rnum+1);
	}
	document.getElementById('acronymInput').value = acronym;
	
	randomAnimation++;
	if (randomAnimation < 16) {
		setTimeout('randomizeAcronym()', Math.round( Math.pow( randomAnimation,2 )) );
	} else {
		document.getElementById('acronymInput').style.color = 'black';
		inputUpdate(document.getElementById('acronymInput'));
	}
}
