var shoutbox_starttimer;
var shoutbox_maxtime=600; //600 secs
var shoutbox_oldtimer;
var shoutbox_oldupdate;
var siteUrl; //externally set

function shoutbox_cancel() {
    shoutbox_starttimer=0;
    if (shoutbox_oldupdate)
        clearTimeout(shoutbox_oldupdate);
    shoutbox_oldupdate=undefined;
    shoutbox_updatetimer(true);
}

function update_shoutbox(manual,msg,archived,onetime) {
  var delay=10; //10 seconds

  if ((!archived && manual) || (!shoutbox_starttimer) ) {
      if (!onetime)
          shoutbox_starttimer=currentTime(); 
  } else {
    if (currentTime()-shoutbox_starttimer>=shoutbox_maxtime*1000) { 
        shoutbox_updatetimer(true);
      return;
    }
  }
  var handler_func=function(data) {
    var element=document.getElementById('shoutbox');
    var inner=document.getElementById('sbInner');
    var obj,textObj;
    var listitem;
    var j;
    var sbShowDate=shoutbox_show_date();

    while (inner.hasChildNodes()) {
      inner.removeChild(inner.firstChild);
    }
    j=0;
    while (j<data.length) {
      var theclass;
      var html;
      if (j%2==0)
        theclass='BG1';
      else theclass='BG2';
      listitem=document.createElement('li');
      listitem.className=theclass;
      // listitem.style.maxWidth='150';
      html='';
      if (data[j].candelete) {
        html+='<span class="body"><a href="'+siteUrl+'/batch.shoutbox.php?action=delete&sid='+data[j].sid+'" title="Delete" style="color: #cc801a;">[x]</a></span>';
      }
      html+='<a href="'+data[j].user_url+'" title="'+data[j].username+'" class="title">'+data[j].username+':</a><span class="body">'+data[j].message+'</span>';
      if (sbShowDate) {
          //html+='<div class="body" style="display: block; font-size: xx-small; ">'+data[j].date+'</div>';
          var d=new Date();
          d.setTime(data[j].dateline*1000);
          var min=d.getMinutes();
          var hour=d.getHours();
          if (hour.toString().length<2)
              hour='0'+hour.toString();
          if (min.toString().length<2)
              min='0'+min.toString();

          html+='<div class="body" style="display: block; font-size: xx-small; ">'+hour+':'+min+'</div>';
      }

      listitem.innerHTML=html;
      inner.appendChild(listitem);
      ++j;
    }
    if (!onetime) {
        if (shoutbox_oldupdate) 
            clearTimeout(shoutbox_oldupdate);
        shoutbox_oldupdate=setTimeout('update_shoutbox()',delay*1000);
    } 

  }
  var param=1;

  if (archived) {
    param='archive';
  }
    var room;

    if (document.cookie.match(/lang=sc/i))
        room=1;
    else room=0;


  var url=siteUrl+'/batch.shoutbox.php?ajax='+param+'&json=1&room='+room;
  if (msg) {
    url+='&msg='+msg;
  }
  var rnd;
  rnd=new Date();
  rnd=rnd.getTime();
  url+='&rnd='+rnd;
    if (window.Json && window.Json.Remote) {
        new Json.Remote(url,{method:"get",onComplete: handler_func}).send();
    } else {
        //mootools 1.2 version
        new Request.JSON({"url":url,
                      "method":"get",
                      "onComplete": handler_func}).send();
    }

    //ajax_json_handler(url,handler_func);
  shoutbox_updatetimer(onetime);
}

function shoutbox_updatetimer(onetime) {
  var element=document.getElementById('shoutbox_secs');
  var diff=currentTime()-shoutbox_starttimer;
  diff=shoutbox_maxtime-parseInt(diff/1000);
  if ((diff<0) || isNaN(diff)) diff=0;
  if (element) {
      element.innerHTML=diff
          if (!onetime) {
              if (shoutbox_oldtimer)
                  clearTimeout(shoutbox_oldtimer);
              shoutbox_oldtimer=setTimeout('shoutbox_updatetimer()',2000);
          }
  }
  element=document.getElementById('shoutbox_auto');
  if (element) {
      if (diff==0)
          element.checked=false;
      else element.checked=true;
  }
}

function shoutbox_toggle_date() {
  if (document.cookie.indexOf('supe_sb_date')==-1)
    document.cookie='supe_sb_date=1;'
    else document.cookie='supe_sb_date=;expires=Fri, 02-Jan-1970 00:00:00 GMT';

  shoutbox_update_datecheck();
  update_shoutbox(true,undefined,false,true);
}

function shoutbox_show_date() {
  return document.cookie.indexOf('supe_sb_date')!=-1;
}

function shoutbox_update_datecheck() {
  var element=document.getElementById('shoutbox_date');
  if (element)
    element.checked=shoutbox_show_date();
}
