function BxBlgComment(iLoggedIn, sOrder, sLayout){
	this._iLoggedIn = iLoggedIn;
	this._sOrder = sOrder;
	this._sLayout = sLayout;
	
	this._aTimers = new Array();
	this.runTimers();

	this._sAnimEffect = 'fade';
	this._sAnimSpeed = 'slow';
}

BxBlgComment.prototype.seeMore = function(oLink) {
	jQuery(oLink).bx_anim('hide', this._sAnimEffect, 'fast', function() {
		jQuery(this).next('span').show().prev('a:hidden').remove();
	});
};

BxBlgComment.prototype.onSubmitComment = function(oForm) {
	var iTextLen = jQuery(oForm).find("[name = 'comment_text']").val().length;
	if(iTextLen == 5 || iTextLen > 20000) { 
		alert('No more than 20000 characters per comment, please.');
		return false;
	}

	return true;
};

BxBlgComment.prototype.replyComment = function(id, oLink) {
	if(this._iLoggedIn != 1) {
		showLoginForm();
		return false;
	}

	var sId = 'cmt-reply-' + id;
	if(jQuery('#' + sId).length > 0) {
		jQuery('#' + sId).toggle();
		return;
	}

	jQuery(oLink).parents('.cmt-pst:first').after("<div id='" + sId + "'>loading...</div>");

	var onLoad = function() {
		jQuery('#cmt-reply-' + id + ' .cmt-textarea-text').autoResize();
	}

	var oBxContent = new BxContent();
	oBxContent.setForceServerXsl(1);
	oBxContent.replace(sId, aBxConfig['urlRoot'] + aBxConfig['moduleUri'] + "comment_reply/?id=" + id + '&date=' + (new Date()), aBxConfig['urlModuleXsl'] + 'comment_reply.xsl', onLoad);
};

BxBlgComment.prototype.addComment = function(iId) {
	if(this._iLoggedIn != 1) {
		showLoginForm();
		return false;
	}

    if(jQuery('#cmt' + iId).length > 0)
    	return;

    jQuery('#cmts > #no_cmts').remove();

    var $this = this; 
    var oDate = new Date();
    jQuery.get(
    	aBxConfig['urlRoot'] + aBxConfig['moduleUri'] + "comment_get/" + iId + "/" + this._sOrder,
    	{
    		time: oDate.getTime()
    	},
    	function(oData) {
    		if(jQuery('#cmts > .cmt:not(.cmt-create, .cmt-more)').length > 0)
    			jQuery('#cmts > .cmt:not(.cmt-create, .cmt-more):first').before(oData.content);
    		else
    			jQuery('#cmts > .cmt-create').after(oData.content);
	    	jQuery('#comment_form_0 #comment_text').val('');
	
	        $this.runTimers();
    	},
    	'json'
    );
};

BxBlgComment.prototype.updateComment = function(iId) {
	var sId = 'cmt' + iId + 'edit';
    if(jQuery('#' + sId).length != 0) {
    	jQuery('#' + sId).bx_anim('hide', this._sAnimEffect, this._sAnimSpeed, function() {
        	jQuery(this).remove();
    	});
    }

    var oDate = new Date();
    jQuery.get(
    	aBxConfig['urlRoot'] + aBxConfig['moduleUri'] + "comment_get/" + iId + "/" + this._sOrder,
    	{
    		time: oDate.getTime()
    	},
    	function(oData) {
    		jQuery('#cmt' + iId).replaceWith(oData.content);
    	},
    	'json'
    );
};

BxBlgComment.prototype.editComment = function(iId) {
	if (this._iLoggedIn != 1) {
		showLoginForm();
		return false;
	}

	var sId = 'cmt' + iId + 'edit';
	if($(sId)) 
		return;

    jQuery('#cmt' + iId + 'text').hide().parent().append('<div id="' + sId + '">loading...</div>');

    var oBxContent = new BxContent();
    oBxContent.setForceServerXsl(1);
    oBxContent.replace(sId, aBxConfig['urlRoot'] + aBxConfig['moduleUri'] + "xml_comment_edit/" + iId + '?date=' + (new Date()), aBxConfig['urlModuleXsl'] + 'comment_edit.xsl');
};

BxBlgComment.prototype.editCancel = function (iId) {
	var sId = 'cmt' + iId + 'edit';
    if(jQuery('#' + sId).length == 0)
    	return;

    var $this = this;
    jQuery('#' + sId).bx_anim('hide', this._sAnimEffect, this._sAnimSpeed, function() {
    	jQuery(this).remove();
    	jQuery('#cmt' + iId + 'text').bx_anim('show', $this._sAnimEffect, $this._sAnimSpeed);
    })
};

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) {
            var e = $('cmt'+id);

			e.parentNode.removeChild(e);
			return false;
		}

		alert ('Operation failed');
		return false;
	};

	new BxXmlRequest (aBxConfig['urlRoot'] + aBxConfig['moduleUri'] + "xml_comment_delete/" + id + '?date=' + (new Date()), h, true);
	return false;
}

BxBlgComment.prototype.addCommentReply = function (iParentId, iId) {
	if(this._iLoggedIn != 1){
		showLoginForm();
		return false;
	}
	$this = this;

    jQuery('#cmt-reply-' + iParentId).remove();    
    this.showReplies(iParentId, 1);
};

BxBlgComment.prototype.showMore = function(iEntryId, iStart) {
    var sHeaderId = jQuery('#cmts').parents('.sys_bl_header:first').attr('id');
    toggleLoading(sHeaderId);

    var $this = this; 
    var oDate = new Date();
    jQuery.get(
    	aBxConfig['urlRoot'] + aBxConfig['moduleUri'] + "comment_show_more/" + iEntryId + "/" + iStart + "/" + this._sOrder + "/" + this._sLayout,
    	{
    		time: oDate.getTime()
    	},
    	function(oData) {
    		jQuery('#cmts > .cmt-more').remove();
    		jQuery('#cmts > .cmt:not(.cmt-create, .cmt-more):last').after(oData.content);

    		toggleLoading(sHeaderId);
    	},
    	'json'
    );
};

BxBlgComment.prototype.showReplies = function (iId, iShort, handler) {
	var $this = this;
	var sId = 'cmt-replies-' + iId;

	var oBxContent = new BxContent();
	oBxContent.setForceServerXsl(1);
	oBxContent.onLoadComplete = function () {
		jQuery('#' + sId).show();

		$this.runTimers();

        if(handler)
            handler();
    }
	oBxContent.replace(sId, aBxConfig['urlRoot'] + aBxConfig['moduleUri'] + "comment_show_replies/?id=" + iId + '&short=' + iShort + '&date=' + (new Date()), aBxConfig['urlModuleXsl'] + 'comment_replies.xsl');
};

BxBlgComment.prototype.runTimers = function() {
	var $this = this;
	var aItems = new Array(
		{prefix:'cmt-edit', func:'onTimerEdit'},
		{prefix: 'cmt-delete', func: 'onTimerDelete'}
	);

	jQuery.each(aItems, function(iIndex, oItem) {
		if(jQuery('.' + oItem.prefix).length != 0)
	    	jQuery.each(jQuery('.' + oItem.prefix), function(iObject, oObject){
	    		var sId = jQuery(oObject).attr('id');
	    		if($this._aTimers[sId] != undefined) {
	    			clearInterval($this._aTimers[sId]);
	    			$this._aTimers[sId] = undefined;
	    		}
	    		$this._aTimers[sId] = setInterval("oBxBlgComment." + oItem.func + "('" + sId + "');", 1000);
	    	});
	});	
};

BxBlgComment.prototype.onTimerEdit = function(sId) {
	var oTimer = jQuery('#' + sId);
    var iCounter = oTimer.length > 0 ? parseInt(oTimer.html()) : -1;

    if(iCounter > 0)
    	oTimer.html(iCounter - 1);
    else {
    	oTimer.parents('.' + oTimer.attr('class') + '-btn').remove();
    	this.editCancel(parseInt(sId.replace('cmt-edit-', '')));
    }

    if(iCounter <= 0) {
    	clearInterval(this._aTimers[sId]);
    	this._aTimers[sId] = undefined;
    }
};

BxBlgComment.prototype.onTimerDelete = function(sId) {
	var oTimer = jQuery('#' + sId);
    var iCounter = oTimer.length > 0 ? parseInt(oTimer.html()) : -1;

    if(iCounter > 0)
    	oTimer.html(iCounter - 1);
    else
    	oTimer.parents('.' + oTimer.attr('class') + '-btn').remove();

    if(iCounter <= 0) {
    	clearInterval(this._aTimers[sId]);
    	this._aTimers[sId] = undefined;
    }
};

BxBlgComment.prototype._confirm = function () {
	return window.confirm ('Are you sure?');
};





/**
 * 
 * THE FOLLOWING METHODS WERE NOT UPDATED
 * 
 */
BxBlgComment.prototype.subscribeComments = function(id) {
	if(this._iLoggedIn != 1) {
		showLoginForm();
		return false;
	}

	var $this = this;
	var h = function (r) {		
		var o = new BxXmlRequest('','','');			
		var ret = o.getRetNodeValue (r, 'ret');
		if(ret == '1') {
			alert('Operation was done.');
            $this.reload();
            return false;
		}

		alert ('Operation failed');
		return false;
	};

	new BxXmlRequest(aBxConfig['urlRoot'] + aBxConfig['moduleUri'] + "xml_comment_subscribe/?id=" + id, h, true);
	return false;
};
BxBlgComment.prototype.hideComment = 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(ret == '1') {
			alert('Operation was done.');
            $this.reload();
            return false;
		}

		alert ('Operation failed');
		return false;
	};
	
	new BxXmlRequest (aBxConfig['urlRoot'] + aBxConfig['moduleUri'] + "xml_comment_hide/?id=" + id, h, true);
	return false;
};

BxBlgComment.prototype.unhideComment = 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(ret == '1') {
			alert('Operation was done.');
			$this.reload();
			return false;
		}

		alert ('Operation failed.');
		return false;
	};
	
	new BxXmlRequest (aBxConfig['urlRoot'] + aBxConfig['moduleUri'] + "xml_comment_unhide/?id=" + id, h, true);
	return false;
};




BxBlgComment.prototype.reload = function () {
	var iPosition = document.location.href.lastIndexOf('#');
	if(iPosition != -1)
		document.location.href = document.location.href.substr(0, iPosition);
	else
		document.location = document.location;
};
