
function refreshchat(url){
    var commentsdiv = document.getElementById('chatcomments');
    for(var i=0; i < chatarray.length; i++){
        var timeago = chatarray[i][4];
        var posttext = ' second';
        if(chatarray[i][4] < 3600){
            timeago = Math.round(chatarray[i][4]/60);
            posttext = ' minute';
        }else if(chatarray[i][4] < 86400){
            timeago = Math.round(chatarray[i][4]/3600);
            posttext = ' hour';
        }else if(chatarray[i][4] < 604800){
            timeago = Math.round(chatarray[i][4]/86400);
            posttext = ' day';
        }else{
            timeago = Math.round(chatarray[i][4]/604800);
            posttext = ' week';
        }
        if(timeago==1)timeago += posttext+' ago';
        else timeago += posttext+'s ago';

        var $comment = jQuery('#comment'+chatarray[i][0]);
        if ($comment.length){
            if(timeago != $comment.find('i').html()){
                $comment.find('i').html(timeago).css({
                    opacity: 0
                }).animate({
                    opacity: ''
                }, 300);
            }
            if(chatarray[i][2]>0)rating = '+'+chatarray[i][2];
            else rating = chatarray[i][2];
            if(rating != $comment.find('span.tally').html()){
                $comment.find('span.tally').html(rating).css({
                    opacity: 0
                }).animate({
                    opacity: ''
                }, 300);
            }
        
            
            $comment.find('span.up').html('<a title="Yay" href="'+url+chatarray[i][0]+'/1/'+chatarray[i][2]+'" id="yaylink'+chatarray[i][0]+'" onclick=" event.returnValue = false; return false;">Yay</a><script>//<![CDATA[\n\
jQuery(\'#yaylink'+chatarray[i][0]+'\').click( function() {jQuery.ajax({beforeSend:function(request) {request.setRequestHeader(\'X-Update\', \'tally'+chatarray[i][0]+'\');}, success:function(data, textStatus) {jQuery(\'#tally'+chatarray[i][0]+'\').html(data);}, async:true, type:\'post\', url:\''+url+chatarray[i][0]+'/1/'+chatarray[i][2]+'\'}); return false;});\n\
//]]></script>');
            $comment.find('span.down').html('<a title="Nay" href="'+url+chatarray[i][0]+'/-1/'+chatarray[i][2]+'" id="naylink'+chatarray[i][0]+'" onclick=" event.returnValue = false; return false;">Nay</a><script>//<![CDATA[\n\
jQuery(\'#naylink'+chatarray[i][0]+'\').click( function() {jQuery.ajax({beforeSend:function(request) {request.setRequestHeader(\'X-Update\', \'tally'+chatarray[i][0]+'\');}, success:function(data, textStatus) {jQuery(\'#tally'+chatarray[i][0]+'\').html(data);}, async:true, type:\'post\', url:\''+url+chatarray[i][0]+'/-1/'+chatarray[i][2]+'\'}); return false;});\n\
//]]></script>');

        }else{
            var blurbdl = document.createElement('dl');
            blurbdl.setAttribute('class', 'g5 nml blurb');
            var namedt = document.createElement('dt');
            var namebold = document.createElement('b');
            var nametext = document.createTextNode(chatarray[i][3]+' ');
            var timeitalic = document.createElement('i');
            var timetext = document.createTextNode(timeago);
            var bodydd = document.createElement('dd');
            var bodytext = document.createTextNode(chatarray[i][1]);
            bodydd.appendChild(bodytext);
            timeitalic.appendChild(timetext);
            namebold.appendChild(nametext);
            namebold.appendChild(timeitalic);
            namedt.appendChild(namebold);
            blurbdl.appendChild(namedt);
            blurbdl.appendChild(bodydd);
            var votediv = document.createElement('div');
            votediv.setAttribute('class', 'g1 nmr vote');
            votediv.setAttribute('id', 'tally'+chatarray[i][0]);
            var tallyspan = document.createElement('span');
            tallyspan.setAttribute('class', 'tally');
            tallyspan.setAttribute('id', 'tallyrating'+chatarray[i][0]);
            if(chatarray[i][2]>0)rating='+'+chatarray[i][2];
            else rating=chatarray[i][2];
            var ratingtext = document.createTextNode(rating);
            var upspan = document.createElement('span');
            upspan.setAttribute('class', 'up');
            var yaylink = document.createElement('a');
            yaylink.setAttribute('title', 'Yay');
            yaylink.setAttribute('href', url+chatarray[i][0]+'/1/'+chatarray[i][2]);
            yaylink.setAttribute('id', 'yaylink'+chatarray[i][0]);
            yaylink.setAttribute('onClick', ' event.returnValue = false; return false;');
            var yaytext = document.createTextNode('Yay');
            var downspan = document.createElement('span');
            downspan.setAttribute('class', 'down');
            var naylink = document.createElement('a');
            naylink.setAttribute('title', 'Nay');
            naylink.setAttribute('href', url+chatarray[i][0]+'/-1/'+chatarray[i][2]);
            naylink.setAttribute('id', 'naylink'+chatarray[i][0]);
            naylink.setAttribute('onClick', ' event.returnValue = false; return false;');
            var naytext = document.createTextNode('Nay');
            var spacetext = document.createTextNode(' ');
            yaylink.appendChild(yaytext);
            upspan.appendChild(yaylink);
            naylink.appendChild(naytext);
            downspan.appendChild(naylink);
            tallyspan.appendChild(ratingtext);
            votediv.appendChild(tallyspan);
            votediv.appendChild(spacetext);
            votediv.appendChild(upspan);
            votediv.appendChild(spacetext);
            votediv.appendChild(downspan);
            var maindiv = document.createElement('div');
            maindiv.setAttribute('class', 'comment hidden');
            maindiv.setAttribute('id', 'comment'+chatarray[i][0]);
            maindiv.appendChild(blurbdl);
            maindiv.appendChild(votediv);
            //document.write('<script type="text/javascript">//<![CDATA[Event.observe("link'+chatarray[i][0]+'", "click", function(event) { new Ajax.Updater("tally'+chatarray[i][0]+'","/lawinfo/chats/rate/'+chatarray[i][0]+'/1/'+chatarray[i][2]+'", {asynchronous:true, evalScripts:true, requestHeaders:["X-Update", "tally'+chatarray[i][0]+'"]}) }, false);Event.observe("link'+chatarray[i][0]+'", "click", function(event) { new Ajax.Updater("tally'+chatarray[i][0]+'","/lawinfo/chats/rate/'+chatarray[i][0]+'/-1/'+chatarray[i][2]+'", {asynchronous:true, evalScripts:true, requestHeaders:["X-Update", "tally'+chatarray[i][0]+'"]}) }, false);//]]></script>');
            commentsdiv.insertBefore(maindiv, commentsdiv.firstChild);

            var upscript = document.createElement('script');
            var upscr = '//<![CDATA[\n\
jQuery(\'#yaylink'+chatarray[i][0]+'\').click( function() {jQuery.ajax({beforeSend:function(request) {request.setRequestHeader(\'X-Update\', \'tally'+chatarray[i][0]+'\');}, success:function(data, textStatus) {jQuery(\'#tally'+chatarray[i][0]+'\').html(data);}, async:true, type:\'post\', url:\''+url+chatarray[i][0]+'/1/'+chatarray[i][2]+'\'}); return false;});\n\
//]]>';
            upscript.text = upscr;
            upspan.insertBefore(upscript, upspan.firstChild);

            var downscript = document.createElement('script');
            var downscr = '//<![CDATA[\n\
jQuery(\'#naylink'+chatarray[i][0]+'\').click( function() {jQuery.ajax({beforeSend:function(request) {request.setRequestHeader(\'X-Update\', \'tally'+chatarray[i][0]+'\');}, success:function(data, textStatus) {jQuery(\'#tally'+chatarray[i][0]+'\').html(data);}, async:true, type:\'post\', url:\''+url+chatarray[i][0]+'/-1/'+chatarray[i][2]+'\'}); return false;});\n\
//]]>';
            downscript.text = downscr;
            downspan.insertBefore(downscript, downspan.firstChild);


            if(jQuery('#chatcomments div.comment').size()>100)jQuery('div.comment:last').unbind().slideUp('normal', function(){
                jQuery('div.comment:last').remove();
            });
            jQuery('#comment'+chatarray[i][0]).slideDown();
        }
    }
}

