
function check_text() {  

	var last_modified_time = $('#last_modified').html();
	var date = new Date().getTime();
	
	//send ajax request
	$.get("check.php", { last_modified: last_modified_time, hash: date}, function(data){
		if(undefined === data.info) {
			var words = data.words;
			for(var i = 0; i < words.length; i++) {
				if($('#word_'+i).html() != words[i]) {
					$('#word_'+i).addClass('changed');
					
					var current = words[i];
					
					
					$('#history').prepend('<br/>');
					$($('.current_word').get().reverse()).each(function (i) {
						$('#history').prepend($(this).text() + ' ');
					});
					
					$('#revision').html(data.revision);
					
					var last_revision = parseInt(data.revision) - 1;
					$('#history').prepend('#' + last_revision + ': ');
					
					$('#word_'+i).hide("slow", function callback() {
  						$(this).html(current)
					});
					
					
					$('#word_'+i).show("slow", function callback() {
  						$(this).removeClass('changed');
					})
				}
				
			}
			$('#last_modified').html(data.time);
		}
		
		//call function again
		check_text();
	}, "json");
	 
} 

/**
 * document ready function
 */
$(document).ready(function() {

	check_text();
	
	
	$('a.history').click(function() {
		$('#history').toggle();
		return false;
	});
	
	$('.current_word').tooltip({
		track: true,
		delay: 0,
		showURL: false,
		showBody: " - ",
		fade: 250
	});
	
	
});



