var dlComment = Class.create ();

dlComment.prototype.replyTo = function (e) {
    var element = e.target;

    var other = eval (this.parentInstance);

    // Meh, not so good, try to eliminate multiple strung calls to parentNode: 
    var node = element.parentNode.parentNode.parentNode.parentNode.parentNode;

    if (openReplies [node.parentNode.parentNode.id] == "open") {return;}

    var reply_name = node.parentNode.parentNode.id;

    openReplies [reply_name] = "open";

    var tr = new Element ("tr", {});

    var td = new Element ("td", {'colspan': '2'});

    var replyForm = other.loadReplyForm (e.target.id.split ('_') [1], false)

    td.insert (replyForm);

    tr.insert (td);

    node.insert (tr);

    replyForm.getElementsByTagName ("textarea") [0].focus ();
}

dlComment.prototype.flagComment = function (e) {

    var element = e.target;

    var that = this;

    e.target.src = "/comments/widgets/commentsAndRatings/images/flagged_comment.gif";

    Event.stopObserving (element, "click", function (e) {
        that.flagComment.call (that, e);
    });

    Event.stopObserving (element, "mouseover", function (e) {
        that.hoverflag.call (that, e);
    });

    Event.stopObserving (element, "mouseout", function (e) {
        that.unhoverflag.call (that, e);
    });

    var req = new dlAjax ();

    var post = {
        'commentId' : element.id.split ('_') [1],
        'status' : 'flagged'
    }

    req.generate (post, "SetCommentStatus");

    req.send ();
}
dlComment.prototype.buildComment = function (indent) {

    var that = this;

    var comment = new Element ('div', {
        'id'    : 'comment_' + that.id
    }); 
        
    if (indent > 1) {
        comment.className = "indentComment";
    } else {
        comment.className = "outdentComment";
    }   
        
    var table = new Element ('table', {
        'class' :  'commentTable'
    });
    
    var tbody = new Element ('tbody', {});

    table.insert(tbody);
        
    var tr = new Element ('tr', {});
        
    var info = new Element ('td', {
        'class' : 'commentInfo', 
        'style' : 'width: 100px;'
    });
    
    var infoName = new Element ('div', {
        'class' : 'commentInfoName'
    }).update (this.userName);
    
    var infoDate = new Element ('div', {
        'class' : 'commentInfoDate'
    }).update (this.dateTime);
    
    info.insert (infoName);
    info.insert (infoDate);
        
    tr.insert (info);

    var contentTd = new Element ('td', {'style' : 'width: 300px;'});

    var ratingContent = new Element ('div', {'class' : 'ratingContent'});

    var commentContent = new Element ('div', {
        'class' : 'commentContent'
    });

    var commentActions = new Element ('div', {
        'class' : 'commentActions'
    });

    var viewRepliesLink = new Element ('div', {
        'class'   : 'viewReplies'
    }).update (this.numberOfReplies == 1 ? this.numberOfReplies + " Reply" : this.numberOfReplies + " Replies");

    replyLink = new Element ('img', {
        'id' : 'repl_' + that.id,
        'class'   : 'commentReplyLink',
        'src' : '/comments/widgets/commentsAndRatings/images/reply_comment.gif'
    });

    flagLink = new Element ('img', {
        'id' : 'flag_' + that.id,
        'class'   : 'commentFlagLink',
        'src' : '/comments/widgets/commentsAndRatings/images/flag_comment.gif'
    });

    Event.observe (flagLink, "mouseover", function (e) {
        that.hoverflag.call (that, e);
    });

    Event.observe (flagLink, "mouseout", function (e) {
        that.unhoverflag.call (that, e);
    });

    if (commentConfig.loggedIn == true) {

        Event.observe (replyLink, "click", function (e) {
            that.replyTo.call (that, e);
        });

        Event.observe (flagLink, "click", function (e) {
            that.flagComment.call (that, e);
        });

    } else {
        Event.observe (replyLink, "click", function (e) {
            that.redirect.call (that, e);
        });

        Event.observe (flagLink, "click", function (e) {
            that.redirect.call (that, e);
        });
    }

    commentActions.insert (flagLink);
    commentActions.insert (replyLink);

    commentContent.insert (this.content);
    commentContent.insert (commentActions);

    var r = new dlRating ();

    if (indent == 1) {
        if (this.rating > 0) {

            ratingContent.insert (r.displayRater (this.rating));
        }
        contentTd.insert (ratingContent);
    }

    contentTd.insert (commentContent);

    tr.insert (contentTd);

    tbody.insert (tr);

    comment.insert (table);

    return comment;
}

dlComment.prototype.redirect = function ()
{
    window.location="/dLife/do/BlogabetesLogin";
}

dlComment.prototype.hoverflag = function (e)
{
    e.target.src = '/comments/widgets/commentsAndRatings/images/flagged_comment.gif';
}

dlComment.prototype.unhoverflag = function (e)
{
    e.target.src = '/comments/widgets/commentsAndRatings/images/flag_comment.gif';
}
