
function BxBlgComment (loggedin)
{
	this._loggedin = loggedin;
}

BxBlgComment.prototype.replyComment = function (id)
{
	if ('1' != aBxConfig['loggedin'])
	{
		showLoginForm();
		return false;
	}

	var eDiv = document.createElement('div');

	var sId = 'cmt' + id + 'reply';
    
    if ($(sId)) return;

	eDiv.id = sId;
	eDiv.style.textAlign = "center";
	eDiv.innerHTML = 'loading...';

	var eCmt = $('cmt' + id);
	eCmt.parentNode.insertBefore (eDiv, eCmt.nextSibling);

	var o = new BxContent ();
    o.setForceServerXsl(1);
    o.replace (sId, aBxConfig['urlRoot'] + aBxConfig['moduleUri'] + "comment_reply/?id=" + id + '&date=' + (new Date()), aBxConfig['urlModuleXsl'] + 'comment_reply.xsl');

}

BxBlgComment.prototype.showReplies = function (id, isForceReload, isReplyAdded, handler)
{
	var eDiv;

	var sId = 'cmt' + id + 'replieslist';
        
    if ($(sId) && isForceReload) 
    {
        eDiv = $(sId);
        eDiv.style.display = 'block';
    }
    else
    if ($(sId)) 
    {
        var e = $(sId);
        e.style.display = e.style.display == 'none' ? 'block' : 'none';
        return;
    }
    else
    {
        eDiv = document.createElement('div');
        eDiv.id = sId;
    }


	eDiv.style.textAlign = "center";
	eDiv.innerHTML = 'loading...';

	var eCmt = $('cmt' + id);
	eCmt.parentNode.insertBefore (eDiv, eCmt.nextSibling);

	var o = new BxContent ();
    o.setForceServerXsl(1);
    o.onLoadComplete = function () { 
        if (handler) 
            handler();        
    }
    o.replace (sId, aBxConfig['urlRoot'] + aBxConfig['moduleUri'] + "comment_show_replies/?id=" + id + '&date=' + (new Date()), aBxConfig['urlModuleXsl'] + 'comment_replies.xsl');    

}

BxBlgComment.prototype.editComment = function (id)
	{
	if ('1' != aBxConfig['loggedin'])
	{
		showLoginForm();
		return false;
	}

	var eDiv = document.createElement('div');

	var sId = 'cmt' + id + 'edit';
    
    if ($(sId)) return;

	eDiv.id = sId;
	eDiv.style.textAlign = "center";
	eDiv.innerHTML = 'loading...';

    var eText = $('cmt' + id + 'text');
    eText.style.display = 'none';
	
	eText.parentNode.appendChild (eDiv);

	var o = new BxContent ();
    o.setForceServerXsl(1);
    o.replace (sId, aBxConfig['urlRoot'] + aBxConfig['moduleUri'] + "comment_edit/?id=" + id + '&date=' + (new Date()), aBxConfig['urlModuleXsl'] + 'comment_edit.xsl');
	}


BxBlgComment.prototype.editCancel = function (id)
	{
	var sId = 'cmt' + id + 'edit';
    if (!$(sId)) return;

    var eEditBox = $('cmt' + id + 'edit');
    eEditBox.parentNode.removeChild(eEditBox);

    var eText = $('cmt' + id + 'text');
    eText.style.display = 'block';
	}


BxBlgComment.prototype.updateComment = function (id)
{
    this.addComment (id, 1);
}

BxBlgComment.prototype.deleteComment = function (id)
{
	if (!this._confirm()) return false;

	var $this = this;

	var h = function (r)
	{		
		var o = new BxXmlRequest('','','');			
		var ret = o.getRetNodeValue (r, 'ret');
		if ('remove_child' == ret)
		{
			alert('Remove child commnets first');
			return false;
		}
		else
		if ('1' == ret)
		{
			alert ('Comment deleted.');
			//$this.reload ();
            var e = $('cmt'+id);
            if (!e)
                e = $('cmt'+id+'new');
			e.parentNode.removeChild(e);
            $this.updateCommentCount(-1);
			return false;
		}

		alert ('Operation failed');
		return false;
	}	
	
	new BxXmlRequest (aBxConfig['urlRoot'] + aBxConfig['moduleUri'] + "xml_comment_delete/?id=" + id, h, true);

	return false;
}

BxBlgComment.prototype.addComment = function (id, force_replace)
{
	if ('1' != aBxConfig['loggedin'])
	{
		showLoginForm();
		return false;
	}

    var eDiv;
    var sId;

    if (1 == force_replace)
    {    
        sId = 'cmt' + id;
    	eDiv = $(sId);	    
    
        if (!eDiv)
        {
            sId = 'cmt' + id + 'new';
        	eDiv = $(sId);	    
        }
    
        if (!eDiv)
        {
            alert('Can not update comment');
            return;
        }

        if (eDiv.style.marginLeft) eDiv.style.marginLeft = '0px';

    }
    else
    {    
    	eDiv = document.createElement('div');

	    sId = 'cmt' + id + 'new';
    
        if ($(sId)) return;

	    eDiv.id = sId;
    }

	eDiv.style.textAlign = "center";
	eDiv.innerHTML = 'loading...';

	var eCmts = $('cmts');
    var eNoCmts = $('no_cmts');
    if (eNoCmts) 
        eCmts.removeChild (eNoCmts);

    if (1 != force_replace)
	    eCmts.appendChild (eDiv);

    var $this = this;
	var o = new BxContent ();
    o.setForceServerXsl(1);    
    o.onLoadComplete = function () { 
        if (1 != force_replace)
            $this.updateCommentCount(1);
        $this.runTimers(id); 
    }
    o.replace (sId, aBxConfig['urlRoot'] + aBxConfig['moduleUri'] + "comment_get/?id=" + id + '&date=' + (new Date()), aBxConfig['urlModuleXsl'] + 'comment_new.xsl');
}

BxBlgComment.prototype.addCommentReply = function (id_parent, id)
{
	if ('1' != aBxConfig['loggedin'])
	{
		showLoginForm();
		return false;
	}

    var eDivOld = document.getElementById('cmt' + id_parent + 'reply');

    eDivOld.parentNode.removeChild(eDivOld);
    $this = this;
    var h = function () { 
        $this.updateCommentCount(1);
        $this.runTimers(id); 
        $this.updateRepliesCount(id_parent, 1);
    }
    this.showReplies (id_parent, true, true, h);

/*
	var eDiv = document.createElement('div');

	var sId = 'cmt' + id + 'new';
    
    if ($(sId)) return;

	eDiv.id = sId;
	eDiv.style.textAlign = "center";
	eDiv.innerHTML = 'loading...';

	var eParent = eDivOld.parentNode;
	eParent.replaceChild (eDiv, eDivOld);

    var $this = this;
	var o = new BxContent ();
    o.setForceServerXsl(1);    
    o.onLoadComplete = function () { 
        $this.updateCommentCount(1);
        $this.runTimers(id); 
    }
    o.replace (sId, aBxConfig['urlRoot'] + aBxConfig['moduleUri'] + "comment_get/?id=" + id + '&date=' + (new Date()), aBxConfig['urlModuleXsl'] + 'comment_new.xsl');
*/
}

BxBlgComment.prototype.runTimers = function (id)
{
    var eDel = $('cmt_' + id + '_to_del');
    var eEdit = $('cmt_' + id + '_to_edit');
    if (eDel || eEdit)
    {
        setTimeout ("oBxBlgComment.onTimer('" + id + "');", 1000);
    }          
}

BxBlgComment.prototype.onTimer = function (id)
{
    var eDel = $('cmt_' + id + '_to_del');
    var eEdit = $('cmt_' + id + '_to_edit');

    var iDel = eDel ? parseInt(eDel.innerHTML) : -1;
    var iEdit = eEdit ? parseInt(eEdit.innerHTML) : -1;

    if (iDel > 0)
    {
        eDel.innerHTML = iDel - 1;
    }
    else
    {
        var eDelBtn = $('cmt_' + id + '_btn_del');
        if (eDelBtn) eDelBtn.parentNode.removeChild(eDelBtn);
    }

    if (iEdit > 0)
    {
        eEdit.innerHTML = iEdit - 1;
    }
    else
    {
        var eEditBtn = $('cmt_' + id + '_btn_edit');
        if (eEditBtn) 
        {
            eEditBtn.parentNode.removeChild(eEditBtn);
            this.editCancel(id);
        }
    }

    if (eDel || eEdit)
    {
        setTimeout ("oBxBlgComment.onTimer('" + id + "');", 1000);
    }          
}

BxBlgComment.prototype.updateCommentCount = function (i)
{
    var e = $('blg_count_comments');
    if (!e) return;
    var ii = parseInt(e.innerHTML);
    ii += i;
    e.innerHTML = ii;

}

BxBlgComment.prototype.updateRepliesCount = function (iCommentId, i)
{
    var eDiv = $('toggle_reply_' + iCommentId);
    if (!eDiv) return;

    var e = eDiv.getElementsByTagName('b')[0];

    var ii = parseInt(e.innerHTML);
    ii += i;
    e.innerHTML = ii;
}

BxBlgComment.prototype.toggleViewReplies = function (iCommentId)
{
    var eDiv = $('toggle_reply_' + iCommentId);
    var eSpan = eDiv.getElementsByTagName('span')[0];
    var sText = eSpan.innerHTML;
    
    if ('View' == sText)
        eSpan.innerHTML = 'Hide';
    else
        eSpan.innerHTML = 'View';

    this.showReplies (iCommentId, false, false, null);
}

BxBlgComment.prototype.toggleCommentText = function (iCommentId)
{
    var eDiv = $('cmt' + iCommentId + 'threshold');
    var eText = $('cmt' + iCommentId + 'text');
    var eSpan, sText;
    if (eDiv)        
    {   
        eSpan = eDiv.getElementsByTagName('span')[0];
        sText = eSpan.innerHTML;
        if ('Show' == sText)
        {
            eSpan.innerHTML = 'Hide';
            eDiv.style.borderBottom = '1px solid #CFCFCF';
            eDiv.style.paddingBottom = '5px';
            eDiv.style.marginBottom = '5px';
        }
        else
        {
            eSpan.innerHTML = 'Show';
            eDiv.style.borderBottom = 'none';
            eDiv.style.paddingBottom = '0';
            eDiv.style.marginBottom = '0';
        }
    }
    
    eText.style.display = eText.style.display == 'block' ? 'none' : 'block';
}

BxBlgComment.prototype.showDiscussions = function (e, iStart, iEntryId)
{
    var eDiv = e.parentNode;
    var sDivId = eDiv.id = 'cmt' + iEntryId + 'dsc' + iStart;

    eDiv.innerHTML = '<span>loading...</span>';

    var $this = this;
	var o = new BxContent ();
    o.setForceServerXsl(1);    
    o.onLoadComplete = function () { 
        $(sDivId).parentNode.removeChild($(sDivId));
    }
    o.add ('cmts', aBxConfig['urlRoot'] + aBxConfig['moduleUri'] + "comment_show_discussions/?entry_id=" + iEntryId + '&start=' + iStart, aBxConfig['urlModuleXsl'] + 'comment_discussions.xsl');

}

BxBlgComment.prototype._confirm = function ()
{
	return window.confirm ('Are you sure?');
}

BxBlgComment.prototype.reload = function ()
{
	document.location = document.location;
}
