/*	Lista pojmovi za animaciju teksta u search field-u
*/
var animationTerms = new Array (
"searching",
"exploring",
"finding",
"playing",
"words",
"work"
);

/*	DOM spreman
*/
$(function () {
	/*	Micanje onsubmit eventa
	*/
	$('#searchContainerTop form, #searchContainerCenter form').submit (function () { return false; });
	
	var textField = $('#searchField');
	if (textField.val() === "") { textField.val(""); }
	//var currentTerm = (Math.random() * animationTerms.length) | 0; // cast to int
	var currentTerm = 0;
	var currentChar = 0;
	var animationDelay = 200;
	var termDelay = 1000;
	var randomEnabled = true;
	
	/*	Funkcija animacije automatskog upisa teksta u search field
	*/
	var animateText = function () {		
		var term = new String (animationTerms[currentTerm]);
		if (randomEnabled) { animationDelay = Math.random() * 300 + 100; }
		
		if (currentChar >= term.length) { 
			currentChar = 0;
		}
		
		if ($(this).val() != term) {
			$(this).val ($(this).val() + term.charAt (currentChar));
			currentChar++;
			$(this).animate (
				{marginLeft: "0"},
				animationDelay,
				animateText
			);
		}
		else {
			if (currentTerm + 1 == animationTerms.length) { currentTerm = 0; }
			else { currentTerm++; }		
			$(this).animate (
				{marginLeft: "0"},
				termDelay,
				function () {
					$(this).val ("");
					$(this).animate (
					{marginLeft: "0"},
					animationDelay,
					animateText
					);
				}
			);
		}
	}
	
	/*	Započni animaciju automatskog teksta
	*/
	if (textField.val() == "") {
		 textField.animate ({marginLeft: "0"}, 500, animateText);
	}
	
	$("#searchContainerTop, #searchContainerCenter").click (function () {
		textField.focus();
	});
	
	/*	Onfocus event prekida animaciju
	*/
	textField.focus (function () { 
		$(this).stop();
		$(this).val("");
	})
});