var last_message;
var blokada=0;
var shoutbox=document.getElementById("shoutbox");
if (shoutbox)
{
var naglowek=document.createElement('div');
naglowek.id="shoutbox_naglowek";
naglowek.innerHTML="<img id=\"shoutbox_ajax\" src=\"/gfx/shoutbox_ajax.gif\" style=\"vertical-align: text-bottom;\">";
shoutbox.appendChild(naglowek);

var content=document.createElement('div');
content.id="shoutbox_content";
shoutbox.appendChild(content);

wyswietl_shoutbox();

var stopka=document.createElement('div');
stopka.id="shoutbox_stopka";
stopka.innerHTML="<div id=\"shoutbox_form_field\"><form id=\"shoutbox_form\" name=\"shoutbox_form\" onsubmit=\"shoutbox_add(); return false;\"><img src=\"/out/token/\"><input type=\"text\" id=\"shoutbox_token\" name=\"shoutbox_token\"><input type=\"text\" id=\"shoutbox_new\" name=\"shoutbox_new\"><input type=\"submit\" value=\"Pisz!\" id=\"shoutbox_submit\"></form></div>";
shoutbox.appendChild(stopka);

var info=document.createElement('div');
info.id='shoutbox_info';
info.innerHTML="<p id=\"komunikat\"></p>";
shoutbox.appendChild(info);
}


function komunikat(tryb,text)
{
if (tryb=='ukryj')
	{
	$("#komunikat").hide("slow");
	}
	else if (tryb=="pokaz")
	{
	$("#komunikat").html(text);
	$("#komunikat").show('slow');
	}
}

function shoutbox_add(){
komunikat("pokaz","<img src=\"/gfx/shoutbox_ajax2.gif\" alt=\"Shoutbox\">");
var shoutbox_new=document.shoutbox_form.shoutbox_new;
var token=document.shoutbox_form.shoutbox_token;
if (shoutbox_new)
	{
	$.post("/js/shoutbox/shoutbox.php",{"content":shoutbox_new.value,"token":token.value},function (data, textStatus){
	
	if (data!="ban" && data!="token" && data!="content" && data!="zaloguj")
	{
	wyswietl_shoutbox();
	shoutbox_new.value='';
	komunikat('ukryj','');
	}
	else 
	{
	if (data=="ban") komunikat("pokaz","Nie pisz tak szybko, flood!:)");
		else if (data=="token") komunikat("pokaz","Źle przepisany tekst z obrazka.");
			else if (data=="content") komunikat("pokaz","Wpisz treść wiadomości.");
				else if (data=="zaloguj") komunikat("pokaz","<form action=\"/out/login_do/\" method=\"post\"><input type=\"text\" id=\"login\" name=\"login\" value=\"Login..\" onfocus=\"input_text(this.id,'Login..')\" onblur=\"input_text(this.id,'Login..')\"> <input type=\"password\" id=\"pass\" name=\"pass\" value=\"Haslo..\" onfocus=\"input_text(this.id,'Haslo..')\" onblur=\"input_text(this.id,'Haslo..')\"> <input type=\"submit\" value=\"zaloguj\"></form>");
	}
	
	});
	
	}
return false;
}


function wyswietl_shoutbox(){
var ranga=readCookie("remiza_login[4]");
var nick=readCookie("remiza_login[1]");
var x;
var shoutbox_content=document.getElementById("shoutbox_content");
$.ajax({type: "POST",url: "/js/shoutbox/shoutbox.xml", dataType: "xml", cache: false, success: function(shoutbox_xml) 
			{shoutbox_content.innerHTML='';
			$(shoutbox_xml).find('shoutbox').each(function()
				{
					var wpis=document.createElement('div');					
					var content=$(this).attr('content');
					var author=$(this).attr('author');
					var date=$(this).attr('date');
					var id=$(this).attr('id');
					var aid=$(this).attr('aid');
					var del;
					if(nick==author || ranga==2) del='<a href="javascript:void(0)" onclick="usun_wpis('+id+')">[x]</a> '; else del='';
					
					wpis.innerHTML=del+'<span class="shoutbox_author"><a href=\"/users/'+aid+'/\">'+author+'</a> </span><span class="shoutbox_date">('+date+')</span>: <span class="shoutbox_content">'+content+'</span>';
					shoutbox_content.appendChild(wpis);
					last_message=$(this).attr('id');
				});
			}, complete: function(){$('#shoutbox_content').scrollTo("div:last",2000);}
		 });
}
		 
setTimeout("odswiez()",10000);
function odswiez(){
$("#shoutbox_ajax").fadeIn();
$.post("/js/shoutbox/last_message.remiza", function(data){
  if (data!=last_message) {wyswietl_shoutbox();}
});
$("#shoutbox_ajax").fadeOut(1000);
setTimeout("odswiez()",6000);
}


function readCookie(name) {
//http://www.quirksmode.org/js/cookies.html
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function usun_wpis(id){
if (blokada!=1)
	{
	blokada=1;
	komunikat("pokaz","<img src=\"/gfx/shoutbox_ajax2.gif\" alt=\"Shoutbox\">");
	$.get("/js/shoutbox/shoutbox_del.php", {"id":id}, function(data){
	  wyswietl_shoutbox();
	});
	komunikat('ukryj','');
	blokada=0;
	}
last_message=0;
}