// How quickly to get the next message, in millis
var pollSpeed = 5000;

var maxItems     = 1;
var total_height = 0;
var max_height   = 300;

//Specify speed of scroll. Larger=faster (ie: 5)
var scrollSpeed = 5, marginTop = 5;

var seen_ids = new Array();

// Getters/setters
function setY(node, val) { node.style.top = val + "px"; }
function getY(node) { return parseInt(node.style.top); }

//
// Polling for new messages - num is the number of messages you want back
//
function doPoll(num) {

        setTimeout("doPoll(1)", pollSpeed);

        var filter = '';

        if($('has_filter') && $('has_filter').value == 1) {
            filter = encodeURIComponent( $('filter').value );
        }

        ATTAP.ajax({url:'/ajax/discuss.cgi',section:'get_questions',skip_auth:1,extra_params:'filter='+filter+'&numq='+num,handler:handleResponse});
}


//
// Called when a new XML document arrives from the server
//
function handleResponse(rs) {

         if(rs.response && rs.response == 700){
                var questions = rs.data.new_questions;
                for(var i=0; i<questions.length; i++){
                    if(questions[i] == null)
                      continue;

                    if(!seen_ids['id'+questions[i].topic_id] ){
                      addItem(questions[i]);
                      seen_ids['id'+questions[i].topic_id] = 1;
                    }
                }
                 _initDrag([_gel('scroll'),_gel('save')]);
         } else {
            ATTAP.default_handler(rs);
         }
}

//
// Creates a new item in the dom
//
function createItem(item_info)
{
        if(seen_ids['id'+item_info.topic_id])
        return;

        var item = document.createElement("div");
        var html;

        html    = "<div class='rbox item discuss'>";
        html   += "<div class='bt'><div></div></div><div class='i1'><div class='i2'><div class='i3'>";
        html   += "<h3>";

        switch(item_info.type){
            case 'ITEM':
            html += "<a class='itemlink' href='/chat/"+item_info.assoc_id+"/item/'>Item:</a>"; break;

            case 'WIKI':
            html += "<a class='itemlink' href='/item/"+item_info.assoc_id+"/history/'>Wiki:</a>"; break

            case 'REC':
            html += "<a class='itemlink' href='/user/"+item_info.username+"/'><img width='20px' src='/images/user/"+item_info.user_id+"' alt='"+item_info.username+"'></a>"; break;

	    case 'MSG':
            html += "<a class='itemlink' href='/user/"+item_info.username+"/'><img width='20px' src='/images/user/"+item_info.user_id+"' alt='"+item_info.username+"'></a>"; break;

            default:
            break;
        };

        html += "<a href='#' onclick='return false;' id='"+item_info.topic_id+"_h' class='topiclink' title='drag this box to your topics to chat on this item'>"+(item_info.type == "RES" ? "RE: " : "")+item_info.name+"<b></b></a>";
        html += "<span id='"+item_info.topic_id+"_r' class='controls'><a href='#' onclick=\"collapse_box('"+item_info.topic_id+"'); return false;\" title='minimize this box'><img src='/images/min.gif' alt='min' width='15' height='15' /></a></span>";
        html += "</h3>";

        html += "<div id='"+item_info.topic_id+"_c' class='inner'></div>";

        html += "</div></div></div><div class='bb'><div></div></div>";


        item.id = item_info.topic_id;
        item.innerHTML = html;


        seen_ids['id'+item_info.topic_id] = 1;

        return item;
}

//
// Adds an item to the list
//
function addItem(item_info) {

        var item = createItem(item_info)

        if(scrollDiv.hasChildNodes() )
           scrollDiv.insertBefore(item,scrollDiv.firstChild);
        else
           scrollDiv.appendChild(item);


        item.style.position = "relative";

        total_height += item.offsetHeight;

        new Effect.Appear(item.id);

        if(total_height > max_height )
                doScroll();
}

//
// Animates the scrolling
//
function doScroll() {
        var topItem =  scrollDiv.lastChild;
        height = topItem.offsetHeight - marginTop;
        if (height == 0) {
                setTimeout("doScroll()",10);
        } else {
                setY(scrollDiv, getY(scrollDiv) - scrollSpeed);
                if (getY(scrollDiv) < -height) {
                        removeTopItem();
                        return;
                }
                setTimeout("doScroll()",40)
        }
}

function removeTopItem() {
        var topItem = scrollDiv.lastChild;

        var height = topItem.offsetHeight;
        total_height -= height;
        scrollDiv.removeChild(topItem);
        setY(scrollDiv, getY(scrollDiv) + height + marginTop);

}


//
// initialize
//
function initializeScroller() {
        scrollDiv = $('scroll');
        setY(scrollDiv, marginTop);
        // Jump back in the stream a bit

        doPoll(maxItems);
}

if (window.addEventListener)
        window.addEventListener("load", initializeScroller, false)
else if (window.attachEvent)
        window.attachEvent("onload", initializeScroller)
else
        window.onload=initializeScroller

