var count = 0;
var page = 1;

function sh_delete(id) {
	if (!confirm('Czy na pewno usunąć ?'))
		return false;
	$.post('/shoutbox/usun/' + id);
	refresh(page, false);
	return false;
}
function success(response, status)  { 
	if(status == 'success') {
		$('#shoutbox-response').html('<img src="/img/accept.png" /> Dodano shout');
	//	$('#shoutbox .input input').css('background-color', 'none');
		$('#shoutContent').val('');
		insert(response);
	}
}
/* walidacja wysyłania shouta */
function validate(formData, jqForm, options) {
	for (var i=0; i < formData.length; i++) { 
		if (!formData[i].value) {
			alert('Uzupełnij wszystkie pola'); 
//			$('input[name='+formData[i].name+']').css('background-color', 'red');
			return false; 
		} 
	}
	$('#shoutbox-response').html('<img src="/img/loader.gif" /> Trwa wczytywanie');
	clearTimeout(timeoutID);
	return true;
}
function refresh(iPage, slide) {
	if (iPage == null)
		iPage = page;
	else
		page = iPage;
	$.post('/shoutbox/' + iPage, {}, function(data) {
		insert(data, slide);
	});
	return false;
}
function insert(data, slide) {
	$('#shoutbox-list').html(data);
	timeoutID = setTimeout(function() {refresh(page, false);}, 30000);
}
function sh_page(page) {
	//$('#shoutbox-list').html('<div class="c">Ładowanie danych <br /><img src="/img/loader.gif" alt="" /></div>');
	refresh(page, true);
	return false;
}
$(document).ready(function() { 
	var options = { 
		dataType:       'post',
		beforeSubmit:   validate,
		success:        success
	}; 
	$('#shoutbox-form').ajaxForm(options);
	timeoutID = setTimeout(function() {refresh();}, 30000);
});
