
Comments = {
    config: {}
}

var setupComments = function(id, config)
{
    
    Comments.config = config;
    
    var commentslist = $('commentslist'+id);
    var commentcontrols = $('commentcontrols'+id);    
    var commentform = $('commentform'+id);
    var commenttitleform = $('commenttitleform'+id);
    
    commentform.hide();
    
    if(commenttitleform)
    {
        commenttitleform.hide();
    }
    
    commentcontrols.show();
    
    commentslist.select('li.comment').each(function(li)
        {
            var span = li.down('div.user_reply');
            var img = span.down('a.user');
            
            if(span.hasClassName('deleted'))
            {
                return;    
            }
            
            
            var id = li.id.substring('discussioncomment'.length);
            
            if(span.hasClassName('mycomment'))
            {
                var edit = new Element('a', {'href':'#', 'class': 'edit'}).update('Edit');
                edit.observe('click', function(e) { showNewCommentForm('edit', id); Event.stop(e); });
                
                img.insert({after : edit});
            }
            
            var reply = new Element('a', {'href':'#', 'class': 'reply'}).update('Reply');
            reply.observe('click', function(e) { showNewCommentForm('add', id);  Event.stop(e);  });
            img.insert({after : reply});
            
        });
}

var showCommentTitleForm = function(form)
{
    Modalbox.show($(form), { title: 'Update Discussion Title' });    
}

var showNewCommentForm = function(process, commentid)
{
    params = Comments.config;
	
	params.h_process = process;
	
	if(commentid)
	{
	    params.h_commentid = commentid;    
	}
	
	
	if(process == 'edit')
    {
        var li = $('discussioncomment'+commentid);
        
        var spanTitle = li.down('span.title');
        var title = spanTitle ? spanTitle.innerHTML : '';
        var content = li.down('div.comment_text').innerHTML;
        
        params.title = title;
        params.content = content;
    }
	
	
	title = 'Comments';
    
    Modalbox.show('/cognoti/helper/post.nn', 
        {   
            title: title,
            width: 600,
            overlayClose: false,
            params: params
        });
    
    return false;
}

var deleteComment = function(link, commentblock)
{
    
    confirmBubble(link, 'Are you sure you want to delete this comment?', ['Yes', 'No'], 
        function(val)
    {
        if(val == 'No') { return; }
        var href = link.href.split("?");
        
        var target = href[0];
        var params = href[1]+"&ajax=true";
        
        new Ajax.Request(target, 
        {
            method: 'post',
            parameters: params,
            onSuccess: function(transport)
            {
                var root = transport.responseXML.documentElement;
                var status = root.getAttribute("status");
                
                var message = root.getElementsByTagName("message")[0].childNodes[0].nodeValue;
                
                if(status == "success")
                {
                    commentblock = $(commentblock);
                    if(commentblock)
                    {
                        new Effect.SlideUp(commentblock, 
                            { duration: 0.5, 
                            afterFinish: function() { commentblock.remove(); }
                            });
                    }
                }
                else if(status == "partialdelete")
                {
                    commentblock = $(commentblock);
                    if(commentblock)
                    {
                        commentblock.down('div.comment_text').update('*Deleted*');
                    }
                }
                else
                {
                    popupBubble(link, message);        
                }
                
            }
        });
    });
    
    return false;        
}


