var lasttime = 0;
var count = 0;
setInterval("update_shoutbox()", 15000); // 15 seconds
$("#shoutbox_form").submit(function() {
	var val = $("#shoutbox_text").val();
	if(val == '') {
		alert('Musisz wpisać tekst!');
		return false;
	}
	count++;
	var currenttime = Math.round(new Date().getTime()/1000);
	var d = (30-(currenttime-lasttime));
	if(count > 3) {
		if(d > 0) {
			alert('Nie spamuj! Musisz poczekać jeszcze '+d+'s aby wysłać wiadomość.');
			return false;
		} else {
			count = 0;
		}
	}
	lasttime = Math.round(new Date().getTime()/1000);
	$("#shoutbox_loading").show();
	$("#shoutbox_text").attr('disabled', true);
	$.ajax({
		type : 'POST',
		url : 'shoutbox_dodaj.html',
		data : 'text='+val,
		success : function(data) {
			update_shoutbox();
			$("#shoutbox_text").val('').attr('disabled', false);
		}
	});
	return false;
});
var oldkey = 0;
$("#shoutbox_text").keydown(function(event){
	if(event.keyCode == 13 && oldkey != 16) {
		$("#shoutbox_form").trigger('submit');
	}
	oldkey = event.keyCode;
});

function update_shoutbox() {
	$("#shoutbox_loading").show();
	$.ajax({
		type : 'GET',
		url : 'index.php?p=shoutbox&act=update',
		success : function(data) {
			$("#shoutbox").html(data);
			$("#shoutbox_loading").hide();
		}
	});
}
