var updated=false;
var commentData="";

function firstUpdateShoutbox()
{
   var messageList = $(".content > ul");
   var loading = $("#shoutboxLoading");
   //just for the fade effect
   messageList.hide();
   loading.fadeIn();
   //send the post to shoutbox.php
   $.ajax(
   {
      type: "POST", url: "ajax/shoutbox.php", data: "action=update",
      complete: function(data)
      {
	 loading.fadeOut();
	 messageList.html(data.responseText);
	 messageList.fadeIn(1500);
	 clearTimeout(tt);
	 var tt=setTimeout('updateShoutbox()', 20000);
      }
   });
}

function checkDeleteAnswer(commentId, userId, type, targetId, refresh)
{
   if (!isNaN(commentId) && !isNaN(userId))
   {
      if (confirm("Czy usunąć komentarz nr "+commentId+" ?"))
      {
	 deleteUserComment(commentId, userId, type, targetId, refresh);
      }
   }
}

function deleteUserComment(commentId, userId, type, targetId, refresh)
{
   if (!isNaN(commentId) && !isNaN(userId))
   {
      $.ajax(
      {
	 type: "POST", url: "ajax/comments.php", data: "commentId="+commentId+"&userId="+userId+"&type="+type+"&targetId="+targetId,
	 complete: function(data)
	 {
	    var respond=trim(data.responseText);
	    
	    if (respond=="1")
	    {
	       alert("Komentarz został usunięty");
	    }
	    else
	    {
	       alert("Z niewiadomych przyczyn nie można usunąć komentarza");
	    }
	    if (refresh=="1") $("#send").attr({ disabled:false, value:"Wyślij" });
	 }
      });
      if (refresh=="1") showUserComments(true);
   }
   else alert("Podano nieprawidłowe dane, operacja przerwana");
}

var tt2;

function updateShoutbox()
{
   var messageList = $(".content > ul");
   var loading = $("#shoutboxLoading");
   //just for the fade effect
   messageList.hide();
   loading.fadeIn();
   //send the post to shoutbox.php
   $.ajax(
   {
      type: "POST", url: "ajax/shoutbox.php", data: "action=update",
      complete: function(data)
      {
	 loading.fadeOut();
	 messageList.html(data.responseText);
	 messageList.fadeIn(1500);
	 tt2=setTimeout('updateShoutbox()', 20000);
      }
   });
}

   function deleteShoutMsg(msgId)
   {
      if(!isNaN(msgId))
      {
	 	 clearTimeout(tt2);

	    var messageList = $(".content > ul");
	 //we deactivate submit button while sending
	 $("#send").attr({ disabled:true, value:"Kasowanie..." });
	 $("#send").blur();
	 //send the post to shoutbox.php
	 $.ajax(
	 {
	    type: "POST", url: "ajax/shoutbox.php", data: "action=delete&msgId=" + msgId,
	    complete: function(data)
	    {
	       messageList.html(data.responseText);
	       alert("Wiadomość usunięta");
	       updateShoutbox();
	       //reactivate the send button
	       $("#send").attr({ disabled:false, value:"Wyślij" });
	    }
	 });
      }
      else alert("Podane błędne dane!");
      //we prevent the refresh of the page after submitting the form
      return false;
   }


   function showUserComments(forceUpdate)
   {
      var userId = $("#userIdComments").attr("value");
      var userLevel = $("#userCommentLevel").attr("value");
      var userLogin = $("#userLoginComments").attr("value");
      var token = $("#userTokenComments").attr("value");
      var loadingComments=$("#loadingComments");
      var commentList = $("#userComments");
      var commentLink=$("#showUserCommentsLink");

      loadingComments.fadeIn();


      if (updated && commentData.length>0 && forceUpdate==false)
      {//if not first time use cache data
	 loadingComments.fadeOut();
	 commentList.html(commentData);
	 commentLink.html("Ukryj komentarze");
	 commentList.fadeIn(1500);
      }
      else
      {//first time get data
	 if (forceUpdate)    commentData="";
	 $.ajax(
	 {
	    type: "POST",
	    url: "ajax/comments.php",
	    data: "userId="+userId+"&userLogin="+userLogin+"&token="+token+"&level="+userLevel,
	    complete: function(data)
	    {
	       if (forceUpdate) commentList.fadeOut();
	       loadingComments.fadeOut();
	       commentList.html(data.responseText);
	       updated=true;
	       commentData=data.responseText;
	       commentLink.html("Ukryj komentarze");
	       commentList.fadeIn(1500);

	    }
	 });
      }
      return false;
   }

$(document).ready(function()
{
   //global vars shoutbox
   var inputUser = $("#nick");
   var inputMessage = $("#message");
   var loading = $("#shoutboxLoading");
   var messageList = $(".content > ul");

   //global vars show user comments
   var commentForm=$("#showUserComments");
   var commentLink=$("#showUserCommentsLink");
   var loadingComments=$("#loadingComments");
   var commentList = $("#userComments");




   commentLink.click(function(event)
   {
      commentForm.submit();
      event.preventDefault();
   });

   //functions
   //check if all fields are filled
   function checkForm()
   {
      if( inputMessage.attr("value")) return true;
      else return false;
   }



   //Load for the first time the shoutbox data
   firstUpdateShoutbox();
   //on submit event
   $("#form").submit(function()
   {
      if(checkForm())
      {
	 clearTimeout(tt2);
	 var nick = inputUser.attr("value");
	 var message = inputMessage.attr("value");
	 //we deactivate submit button while sending
	 $("#send").attr({ disabled:true, value:"Wysyłanie..." });
	 $("#send").blur();
	 //send the post to shoutbox.php
	 $.ajax(
	 {
	    type: "POST", url: "ajax/shoutbox.php", data: "action=insert&nick=" + nick + "&message=" + message,
	    complete: function(data)
	    {
	       messageList.html(data.responseText);
	       updateShoutbox();
	       //reactivate the send button
	       $("#send").attr({ disabled:false, value:"Wyślij" });
	       tt2=setTimeout('updateShoutbox()', 20000);
	       $("#message").attr('value', '');
	    }
	 });
      }
      else alert("Proszę uzupełnić wszystkie pola!");
      //we prevent the refresh of the page after submitting the form
      return false;
   });

   commentForm.submit(function(event)
   {
      if (commentList.is(':hidden')) showUserComments(false);
      else hideUserComments();
      event.preventDefault();
   });

   function hideUserComments()
   {
      commentList.fadeOut();
      commentLink.html("Pokaż komentarze użytkownika");
   }


});
