var chatscroll = new Object();
var t;

chatscroll.Pane = 
    function(scrollContainerId)
    {
        this.bottomThreshold = 25;
        this.scrollContainerId = scrollContainerId;
    }

chatscroll.Pane.prototype.activeScroll = 
    function()
    {
        var scrollDiv = document.getElementById(this.scrollContainerId);
        var currentHeight = 0;
        
        if (scrollDiv.scrollHeight > 0)
            currentHeight = scrollDiv.scrollHeight;
        else 
            if (objDiv.offsetHeight > 0)
                currentHeight = scrollDiv.offsetHeight;

        if (currentHeight - scrollDiv.scrollTop - ((scrollDiv.style.pixelHeight) ? scrollDiv.style.pixelHeight : scrollDiv.offsetHeight) < this.bottomThreshold)
            scrollDiv.scrollTop = currentHeight;

        scrollDiv = null;
    }
    
function URLEncode (clearString) {
  var output = '';
  var x = 0;
  clearString = clearString.toString();
  var regex = /(^[a-zA-Z0-9_.]*)/;
  while (x < clearString.length) {
    var match = regex.exec(clearString.substr(x));
    if (match != null && match.length > 1 && match[1] != '') {
    	output += match[1];
      x += match[1].length;
    } else {
      if (clearString[x] == ' ')
        output += '+';
      else {
        var charCode = clearString.charCodeAt(x);
        var hexVal = charCode.toString(16);
        output += '%' + ( hexVal.length < 2 ? '0' : '' ) + hexVal.toUpperCase();
      }
      x++;
    }
  }
  return output;
}

    
    function timesince()
    {
    var curdate = new Date()
	curdate.setSeconds(curdate.getSeconds()-300)
	curdate.setHours(curdate.getHours())
    return curdate.toGMTString();
    }	
    
function update(since)
{
	   var url = "http://www.kinderchat.nl/nl/twitterchat/index.htm?actie=get_messages";
       if(since==1)
       {
        	  url += "&since=1";
       }	
       if(since==2)
       {
       		 url += "&last=1";
       }	

   

	   var newmessages= "";

       $.getJSON(url, 
        function(data)
        {
          rExp = /((http|https|ftp):\/\/(([A-Z0-9][A-Z0-9_-]*)(\.[A-Z0-9][A-Z0-9_-]*)+)(\/*)(:(\d+))?([A-Z0-9_\/.?~-]*))/gi
           if(since==2) 
           {
           	if (data[0].user.screen_name != "kinderchat")
          	{
           	text = new String(data[0].text);
          	text = text.replace(rExp,'<a href="$1">$1</a>');
           	newmessages = "<strong>" + data[0].user.screen_name + "</strong>: " +text + "<br />";
          	}
           }	
          
          else
          $.each(data, function(i, item)  
          {
    		text = new String(item.text);
          	text = text.replace(rExp,'<a href="$1">$1</a>');
    		
          	if (item.user.screen_name != "kinderchat")
          	{
          	newmessages = "<strong>" + item.user.screen_name + "</strong>: " +text + "<br />" + newmessages;
          
          	}
	        
          }
          );
          $("#chatbox").append(newmessages);
          divScroll.activeScroll();
        });
	
        t=setTimeout("update(1)",300000);
}	
function formupdate()
{
	update(1);
	//$.timer.reset(300000);
	clearTimeout(t);
	t=setTimeout("update(1)",300000);
	$('#twittertext').show();
	$('#throbber').hide();
}	
function hideform()
{
	$('#twittertext').hide();
	$('#throbber').show();
}	
var divScroll = new chatscroll.Pane('chatbox');
$(document).ready(function()
{
update();	
//$.timer(300000, function () {update(1);});

 var options = { 
        beforeSubmit:  hideform,
        success:       formupdate,  // post-submit callback 
        clearForm: true
    }; 
 $('#twittertext').ajaxForm(options);    
 });