var i_shout=0,lines_shout=0,iTop=10;

function load_shoutbox() {
 if(document.getElementById) {
  check_text();
 } else {
  if(document.layers) {
   check_text();
  } else {
   check_text();
  }
 }
}

function shoutscroll() {
 shoutsize = $("shoutbox").offsetHeight;
 iTop=iTop-10;
 if(iTop < -shoutsize) {
  iTop = shoutsize;
  if(i_shout == len_shout) { i_shout = 1; }
  else { i_shout++; }
  $("shoutbox").innerHTML = comments[i_shout-1];
 }
 if(iTop < 10 && iTop >= 0) {
  timeou = 4000;
  $("shoutbox").style.top=0+"px";
 } else {
  timeou = 50;
  $("shoutbox").style.top=iTop+"px";
 }
 initi_shout = setTimeout("shoutscroll();", timeou);
}

function check_text() {
 var myAjax = new Ajax.Request(
  "check_text.php",
  {method: 'get', onComplete: text_analyse}
 );
}

function text_analyse(originalRequest) {
 if(parseFloat(originalRequest.responseText) != lines_shout) {
  lines_shout = parseFloat(originalRequest.responseText);
  get_text();
 }
}

function get_text() {
 var myAjax = new Ajax.Request(
  "get_text.php",
  {method: 'get', onComplete: set_text}
 );
}

function set_text(originalRequest) {
 eval(originalRequest.responseText);
 if(typeof initi_shout != "undefined") {
  clearTimeout(initi_shout);
  iTop=10;
 }
 len_shout = comments.length;
 i_shout = 1;
 $('shoutbox').innerHTML = comments[0];
 shoutscroll();
}

function send_msg() {

 if(document.shoutit.shoutname.value == "") {
  alert("Du musst schon einen Namen eingeben!");
  document.shoutit.shoutname.focus();
 } else {
  if(document.shoutit.shoutcontent.value == ""  || document.shoutit.shoutcontent.value == "Hier deine Nachricht") {
   alert("Du musst schon einen Text eingeben!");
   document.shoutit.shoutcontent.focus();
  } else {
   var myAjax = new Ajax.Request(
    "send.php",
    {method: 'post', parameters: {name: document.shoutit.shoutname.value, msg: document.shoutit.shoutcontent.value}}
   );

   $("shoutbox").style.top=0+"px";
   $('shoutbox').innerHTML = "<img src=\"loading.gif\" width=\"16\" height=\"16\" alt=\"\" />";

   document.shoutit.shoutname.value = "";
   document.shoutit.shoutcontent.value = "";

   check_text();
  }
 }
 return false;
}

checkall = window.setInterval('check_text()', 10000);