/* licensed under gpl v2 - you can use this code if you'd like */

var Digg = 
{
    apikey : 'http://sandbox.sourcelabs.com/'
};

Digg.request = function (path, count, cbkName, offset, minTimestamp)
{
    var url = 'http://services.digg.com/' + path;

    if (!offset) offset = 0; 

    var params = $H(
    {
        'count'    :  count, 
        'type'     : 'javascript', 
        'appkey'   :  Digg.apikey, 
        'callback' :  cbkName,
        'offset'   :  offset
    });

    if (minTimestamp) params['min_date'] = minTimestamp;

    url += '?' + params.toQueryString();

    var head = document.getElementsByTagName('head')[0];
    var request = document.createElement('script');
        request.type = 'text/javascript';
        request.src = url;

    head.appendChild(request);
}
 

function processCommentsResponse(response) 
{
    CommentSpy.showComments(response.comments, response.timestamp);
}

function processStoriesResponse(response)
{
    CommentSpy.showTitlesFromStories(response.stories);
}

CommentSpy = 
{
    'maxComments':20
};

CommentSpy.showTitlesFromStories = function(stories)
{
    if (!stories) return;

    var storyData = {};

    for (var i = 0, story; story = stories[i]; i++)
    {
        storyData[story.id] = story;
    }

    var comments = document.getElementsByClassName('comment');

    for (var i = 0, comment; comment = comments[i]; i++)
    {
        if (storyData[comment.id.substring(2)])
        {
            var label = comment.getElementsByTagName('label')[0];
            var story = storyData[comment.id.substring(2)];

            if (Element.hasClassName(comment, 'context'))
            {
                label.innerHTML = '<a target="_blank" href="' + story.href + '">' + story.title + '</a> ' +
                                  '<span class="' + story.status + ' diggs">(' + story.diggs + ')</span>';
            }            
            else
            {
                var anchor = label.getElementsByTagName('a')[0];    
                anchor.href = story.href + '#' + anchor.id;
            }
        } 
    }
}

CommentSpy.getComments = function(timestamp) 
{ 
    var ts = (typeof(timestamp) == 'number') ? timestamp : null;

    Digg.request('stories/comments', CommentSpy.maxComments, 'processCommentsResponse', 0, ts); 
}

CommentSpy.showComments = function(comments, timestamp)
{
    var storyIds = new Array();

    if (!comments) comments = new Array();

    for (var i = 0, comment; comment = comments[i]; i++)
    {
        if (document.getElementById(comment.id) == null)
        {
            $('comments').innerHTML = CommentSpy.renderComment(comment) + $('comments').innerHTML;

            storyIds.push(comment.story);

            var numComments = $('comments').childNodes.length;

            if (numComments / 2 > CommentSpy.maxComments)
            {
                Element.remove($('comments').childNodes[numComments - 1]);
                Element.remove($('comments').childNodes[numComments - 2]);
                Element.remove($('comments').childNodes[numComments - 3]);
                Element.remove($('comments').childNodes[numComments - 4]);
            }
        }
    }

    Digg.request('stories/' + storyIds.join(), 100, 'processStoriesResponse'); 

    setTimeout('CommentSpy.getComments(' + timestamp + ')', 5000);
}

CommentSpy.renderComment = function(comment)
{
    var replyClass = (comment.replyto) ? 'reply' : ''; 

    return '\
<div class="comment context" id="cc' + comment.story +'">\
    <div class="story contents">\
        <div class="bubbleHeading">\
            <div></div>\
        </div>\
        <div class="response"><label></label></div>\
        <div class="bubbleFooting">\
            <div></div>\
        </div>\
    </div>\
</div>\
 \
<div id="cs' + comment.story + '" class="' + replyClass + ' comment">\
    <img src="http://sandbox.sourcelabs.com/commentspy/userImage.php?user=' + comment.user + '" height="32" width="32" class="avatar">\
    <div class="contents">\
        <div class="bubbleheading">\
            <div></div>\
        </div>\
        <div class="bubbleIndicator"></div>\
        <div class="response">\
            <label id="' + comment.id + '">\
                <a target="_blank" class="senderName" href="" id="c'+ comment.id +'">' + comment.user + ':</a>\
            </label> ' + 
            comment.content.replace(/[\n]/g, '<br>') + '</div>\
        <div class="bubbleFooting">\
            <div></div>\
        </div>\
    </div>\
</div>';
}


Event.observe(window, 'load', CommentSpy.getComments);

