// GameBank v2 ShoutBox
$(document).ready(function(){
  $("#shoutbox_msg").click(function(){
	if(this.value == "Viesti") this.value = "";
  });
  $("#shoutbox_msg").blur(function(){
	if(this.value == "") this.value = "Viesti";
  });
  $("#shoutboxform").submit(function(){
    var message = $("#shoutbox_msg").val();
    $.ajax({
      type: "POST", url: "/shoutbox.php", data: "message="+message, 
      complete: function(d){
        var text = d.responseText;
        if(text == 'ERROR_LOGIN'){
          alert('Et ole kirjautunut sisään.');
        } else if(text == 'ERROR_TIME'){
          alert('Et voi kirjoittaa uutta viestiä heti perään.');
        } else {
          $("#shoutbox_content").hide();
          $.get("/shoutbox.php", function(d){
            var html = "";
            var shoutbox_class = "box-area-right";
            $(d).find('message').each(function(){
              var $message = $(this);
              var id = $message.find('id').text();
              var user_id = $message.find('user_id').text();
              var username = $message.find('username').text();
              var date = $message.find('date').text();
              var msg = $message.find('msg').text();
              html += '<div class="'+shoutbox_class+'"><a href="/user/'+user_id+'">'+username+'</a> '+date+'<br />'+msg+'</div>';
              if(shoutbox_class == "box-area-right"){ shoutbox_class = "box-area-right-grey"; } else { shoutbox_class = "box-area-right"; }
            });
            if(html == "") html = '<div class="box-area-right-grey">Ei huutoja</div>';
            $("#shoutbox_content").html(html);
            $("#shoutbox_content").fadeIn(1500);
            $("#shoutbox_msg").val("Viesti");
          });
        }
      }
    });
    return false;
  });
});
