﻿var current_media_id = 0;
var current_profile_id = 0;
var current_blog_id = 0

var current_comment_id = 0;
var current_profile_comment_id = 0;
var current_blog_comment_id = 0;

var current_reply_id = 0;

function CommentPoster() { }

function PostMediaComment(sessionid, mediaid, doneFunction)
{
    var text = document.getElementById(CommentPoster.TextAreaID).value;
    PostComment(sessionid, 'media.comment.save', text, mediaid, doneFunction);
}

function PostBlogComment(sessionid, blogpostid, doneFunction)
{
    var text = document.getElementById(CommentPoster.TextAreaID).value;
    PostComment(sessionid, 'blog.comment.save', text, blogpostid, doneFunction);
}

function PostProfileComment(sessionid, profileid)
{
    var text;
    var ta = document.getElementById(CommentPoster.TextAreaID);
    if(ta == null)
    {
        alert('Invalid form.');
        return false;
    }

    text = ta.value;
    PostComment(sessionid, 'profile.comment.save', text, profileid, CommentPoster.PostComplete);
}

function PostComment(sessionid, method, txt, parentid, doneFunction)
{
    var ta = document.getElementById(CommentPoster.TextAreaID);
    if(ta !=  null) ta.disabled = true;
    
    var vars = new Array();
    var a = null;
    
    vars[0] = 'text=' + txt;
    vars[1] = 'item-id=' + parentid.toString();
    
    a = new AjaxObject(method, vars);
    a.sessionid = sessionid;
    
    if(typeof(doneFunction) == 'undefined') doneFunction = CommentPoster.PostComplete;
    a.Send(doneFunction);
}

function Posting()
{

}

function PostComplete(resulttext)
{
    var e = document.getElementById(CommentPoster.NewCommentDivID);
    var ta = document.getElementById(CommentPoster.TextAreaID);
    
    if(e != null)
    {
        e.innerHTML = resulttext + e.innerHTML;
        if(ta != null)
        {
            ta.value = '';
            ta.disabled = false;
        }
        
        $('div#' + CommentPoster.MessageBoxID).show();
        $('div#profile-form-area').fadeOut(400);
        setTimeout("$('div#" + CommentPoster.MessageBoxID + "').fadeOut(300)", 1000);
        
    }
    else
    {
        alert('Err Result : ' + resulttext);
    }
}

CommentPoster.Posting = Posting;
CommentPoster.PostMediaComment = PostMediaComment;
CommentPoster.PostProfileComment = PostProfileComment;
CommentPoster.PostBlogComment = PostBlogComment;
CommentPoster.PostComplete = PostComplete;
CommentPoster.NewCommentDivID = 'NewCommentsDiv';
CommentPoster.FormID = 'CommentForm';
CommentPoster.TextAreaID = 'CommentText';
CommentPoster.MessageBoxID = 'ResultText';


//----------------- Reply Poster -----------------------

function CommentReplyPoster() { }

function PostMediaCommentReply(sessionid, commentid)
{
    PostCommentReply(sessionid, commentid, 'media.comment.reply.save');
}

function PostBlogCommentReply(sessionid, commentid)
{
    PostCommentReply(sessionid, commentid, 'blog.comment.reply.save');
}

function PostProfileCommentReply(sessionid, commentid)
{
    PostCommentReply(sessionid, commentid, 'profile.comment.reply.save');
}

function PostCommentReply(sessionid, commentid, method)
{
    if(current_comment_id > 0) return false;
    
    var vars = new Array();
    var ta = document.getElementById('ReplyTextArea' + commentid.toString());
    if(ta == null) return false;
    if(ta.value.toString().length <= 0) return false;
    
    ta.disabled = true;
    
    vars[0] = 'text=' + ta.value;
    vars[1] = 'commentid=' + commentid.toString();
    
    current_comment_id = commentid;
    
    var a = new AjaxObject(method, vars)
    a.sessionid = sessionid;
    a.Send(CommentReplyPoster.CommentReplyComplete);
}

function CommentReplyComplete(responsetext)
{
    var textid = new String('NewCommentReplies' + current_comment_id.toString());
    var ta = document.getElementById('ReplyTextArea' + current_comment_id.toString());
    var e = document.getElementById(textid);
    
    if(e != null)
    {
        $("div#ReplyForm" + current_comment_id.toString()).fadeOut(500);
        $("div#ajax-form-" + current_comment_id.toString()).fadeOut(500);
        e.innerHTML += responsetext;
    }
    else
    {
        alert('Note:\nYour reply was successfully posted, but you must refresh this page to view it.');
    }

    if(ta != null)
    {
        ta.disabled = false;
        ta.value = '';
    }

    current_comment_id = 0;
}

CommentReplyPoster.PostMediaCommentReply = PostMediaCommentReply;
CommentReplyPoster.PostBlogCommentReply = PostBlogCommentReply;
CommentReplyPoster.CommentReplyComplete = CommentReplyComplete;
CommentReplyPoster.PostProfileCommentReply = PostProfileCommentReply;


//---- Approve Comments

function CommentApproval() { }

function ApproveMediaComment(sessionid, commentid)
{
    var vars = new Array();
    vars[0] = 'comment-id=' + commentid.toString();
    
    var a = new AjaxObject('media.comment.approve', vars, 'ajax');
    a.sessionid = sessionid;
    
    current_comment_id = commentid;
    
    a.Send(ApproveMediaCommentComplete);
}

function ApproveProfileComment(sessionid, commentid)
{
    var vars = new Array();
    vars[0] = 'comment-id=' + commentid.toString();
    
    var a = new AjaxObject('profile.comment.approve', vars, 'ajax');
    a.sessionid = sessionid;
    
    current_profile_comment_id = commentid;
    
    a.Send(ApproveProfileCommentComplete);
}

function ApproveMediaCommentComplete(txt)
{
    var did = new String('comment-item-' + current_comment_id.toString());
    var e = document.getElementById(did);
    
    e.innerHTML = txt;
    $('div#approved-' + current_comment_id.toString()).fadeIn(500);
    current_comment_id = 0;
}

function DeleteMediaComment(sessionid, commentid)
{
    var vars = new Array();
    vars[0] = 'comment-id=' + commentid.toString();
    
    var a = new AjaxObject('media.comment.delete', vars, 'ajax');
    a.sessionid = sessionid;
    
    current_comment_id = commentid;
    
    a.Send(ApproveMediaCommentComplete);
}

function ApproveProfileCommentComplete(txt)
{
    var did = new String('comment-item-' + current_profile_comment_id.toString());
    var e = document.getElementById(did);
    
    e.innerHTML = txt;
    $('div#approved-' + current_profile_comment_id.toString()).fadeIn(500);
    current_profile_comment_id = 0;
}

CommentApproval.DeleteMediaComment = DeleteMediaComment;
CommentApproval.ApproveMediaComment = ApproveMediaComment;
CommentApproval.ApproveProfileComment = ApproveProfileComment;


function ApproveMediaCommentReply(sessionid, replyid)
{
    ApproveReply(sessionid, replyid, 'media.comment.reply.approve')
}

function ApproveProfileCommentReply(sessionid, replyid)
{

}

function ApproveReply(sessionid, replyid, method)
{
    var vars = new Array();
    vars[0] = 'reply-id=' + replyid.toString();

    var a = new AjaxObject(method, vars)

    a.sessionid = sessionid;
    a.resulttype = 'ajax';
    current_reply_id = replyid;
    a.Send(ReplyApproveComplete);
}

function ReplyApproveComplete(txt)
{
    var e = document.getElementById('unapproved-reply-' + current_reply_id);
    e.innerHTML = 'Approved!';
    setTimeout("$('div#unapproved-reply-" + current_reply_id + "').fadeOut(300)", 1500);
}

CommentApproval.ApproveMediaCommentReply = ApproveMediaCommentReply;
//CommentApproval.ApproveProfileCommentReply = 
//CommentApproval.DeleteMediaCommentReply = 
//CommentApproval.DeleteProfileCommentReply = 

