﻿// Make sure /__js/ajax.js and /__js/jquery.js are included.

function Comment(itemid, textareaid, method)
{
    this.itemid = itemid;
    this.textareaid = textareaid;
    this.method = method;
    this.newcommentsdiv = 'new-comments';
    this.profileimage = '';
    this.profilename = '';
    this.profileid = 0;
    this.lineid = 'comment-item-';
    
    this.PostComment = PostComment;
    this.PostCommentReply = PostCommentReply;
    this.DeleteComment = DeleteComment;
}

function PostCommentReply()
{
    var t = document.getElementById(this.textareaid);
    if(t == null)
    {
        alert('Invalid textarea.')
        return false;
    }
    
    var params = new Array();
    var cid = this.itemid.toString();
    
    params[0] = 'text=' + t.value;
    params[1] = 'item-id=' + cid;
    
    var a = new AjaxCall(this.method, params);
    var newcommentsdiv = this.newcommentsdiv;
    var profileimage = this.profileimage;
    var profilename = this.profilename;
    var profileid = this.profileid;
    
    a.method = this.method;
    a.resulttype = 'text';
    
    a.Send(function(result)
    {
        var reply_item = CreateDiv('reply-item-' + result);
        var reply_name = CreateDiv('reply-name-' + result);
        var reply_text = CreateDiv('reply-text-' + result);
        var new_replies = document.getElementById(newcommentsdiv);
        
        reply_item.className = 'reply-item';
        reply_name.className = 'reply-name';
        reply_text.className = 'reply-text';
        
        reply_name.innerHTML = '<a href="/' + profileid.toString() + '"><img src="' + profileimage + '" /></a><a href="/' + profileid.toString() + '">' + profilename + '</a> <span class="date">Just now!</span>';
        reply_text.innerHTML = t.value;
        
        reply_item.appendChild(reply_name);
        reply_item.appendChild(reply_text);
        
        new_replies.appendChild(reply_item);
        t.value = '';
        $('#reply-form-div-' + cid).fadeOut(200);
    });
}

function PostComment()
{
    var t = document.getElementById(this.textareaid);
    if(t == null)
    {
        alert('Invalid textarea.')
        return false;
    }
    
    var params = new Array();
    
    params[0] = 'text=' + t.value;
    params[1] = 'item-id=' + this.itemid.toString();
    
    var a = new AjaxCall(this.method, params);
    var newcommentsdiv = this.newcommentsdiv;
    var profileimage = this.profileimage;
    var profilename = this.profilename;
    var profileid = this.profileid;
    
    a.method = this.method;
    a.resulttype = 'text';
    
    a.Send(function(result)
    {
        try
        {
            result = parseInt(result);
        }
        catch(ex)
        {
            alert('Comment posted. You\'ll have to wait for it to be approved.\n' + result);
            return;
        }
         
        var comment_item = CreateDiv('new-comment-item-' + result);
        var comment_image = CreateDiv('new-comment-image-' + result);
        var comment_info = CreateDiv('new-comment-info-' + result);
        var comment_name_date = CreateDiv('new-comment-name-date-' + result);
        var comment_text = CreateDiv('new-comment-text-' + result);
        var new_comments = document.getElementById(newcommentsdiv);
        var clear = CreateDiv('new-clear-div-' + result);
        var txt = t.value;
        
        t.value = '';
        
        comment_item.className = 'comment-item';
        comment_image.className = 'image';
        comment_info.className = 'info';
        comment_name_date.className = 'name-date';
        comment_text.className = 'text';
        clear.className = 'clear';
        
        comment_name_date.innerHTML = 'By <a href="/' + profileid.toString() + '">' + profilename + '</a> Just Now!';
        comment_text.innerHTML = txt;
        comment_image.innerHTML = '<img src="' + profileimage + '" />';
        
        comment_info.appendChild(comment_name_date);
        comment_info.appendChild(comment_text);
        
        comment_item.style.display = 'none';
        comment_item.appendChild(comment_image);
        comment_item.appendChild(comment_info);
        comment_item.appendChild(clear);
        
        new_comments.appendChild(comment_item);
        $('div#new-comment-item-' + result).fadeIn(500, function()
        {

        });
    });
}

function DeleteComment(commentid)
{
    var params = new Array();
    
    params[0] = 'comment-id=' + commentid.toString();
    
    var lineid = this.lineid + commentid.toString();
    var a = new AjaxCall(this.method, params);

    a.sessionid = 'xx-xx';
    a.resulttype = 'xml';
    
    a.Send(function(result)
    {
        var line = document.getElementById(lineid);
        if(line != null)
        {
            $('#' + lineid).fadeOut(200, function()
            {
                line.parentNode.removeChild(line);
            });
        }
    });
}

function CreateDiv(divid)
{
    var n = document.createElement('div', divid);
    n.id = divid;
    
    return n;
}





