$(document).ready (function () {
	$('#chat_time_reset').hide ().countdown ({
		until: time_difference, 
		layout: reset_timer_text,
		onTick: function (periods) {
			time_difference -= 1;
			
			if (time_difference < 3600) {
				$(this).fadeIn ('slow');
			}
		},
		onExpiry: function () {
			$(this).hide ();
		}
	});
	
	$('#emoticons img').click (function () {
		var alter = $(this).attr ("title");
		
		input = $('#message');
		input.val ( input.val () + alter );
	});
	
	$("a.external").attr('target','_blank');
	
	$('#comment_form').submit (function () {
		$.ajax({
			type: "POST",
			url: "post.php",
			data: $(this).serialize (),
			success: function (response) {
				if (response == ok_response) {
					$('#response').addClass ('success');
				} else {
					$('#response').removeClass();
				}
				
				$('#response').hide ().html(response).fadeIn(1000);
				
				$('#message').val ('');
				
				if (reverse_order) {
					$('#chat').animate({scrollTop:0}, 'slow');
				}
			}
		});
		
		return false;
	});
});

var $window = $(window).data("isFocused", true).bind("focus blur", function(event) { 
	$window.data("isFocused", (event.type == "focus"));
});

var windowTitle = document.title;
var s = 1;

var oldData = false;
var changeTitle = false;

window.setInterval (function () {
	
	if ($window.data("isFocused")) {
		changeTitle = false;
		document.title = windowTitle;
	}
	
	if (changeTitle) {
		if (s % 2 == 0) {
			document.title = '[ ** ] ' + windowTitle;
		} else {
			document.title = '[ /**/ ] ' + windowTitle;
		}
	}
	
	$.get("get.php", function(data){
		if (!$window.data("isFocused") && data != oldData && oldData != false) {
			changeTitle = true;
		}
		
		oldData = data;
	
		$('#chat').html (data);
	});
	
	$.get("get.php?get_participants", function(data){
		$('.participants ul').html (data);
	});
	
	s++;
}, 1000);

if (!reverse_order) {
	window.setInterval (function () {
		$('#chat').animate({scrollTop: $('#chat')[0].scrollHeight});
	}, 1000);
}
