var comment_to_add_pending = false;
function Comment(updateHtmlElement, id, type, self)
{
	this.updateHtmlElement		= updateHtmlElement;
    this.request				= BASE_URL + 'indexajax.php?action=Comment';
    this.id				= id;
    this.type				= type;
    this.self				= self;
    this.success = false;
}

Comment.prototype.list = function(offset, cmc_id) 
{
	var ajaxResponseHtml 	= null;
	var ajaxResponseErrors 	= null;
	var updateHtmlElement 	= this.updateHtmlElement;
	var offset	   			= offset;
	var request = this.request;
	
	request += '&start=listRoots';
	request += '&type=' + this.type;
	request += '&id=' + this.id;
	if(cmc_id != null)
		request += '&cmc_id=' + cmc_id;
	if(offset != null)
		request += '&offset=' + offset;
	if(this.self)
		request += '&self=1';
	$.ajax({
		type: "get",
		url: request,
		dataType: "xml",
		global: false,
		success: function(xml)
		{
			$("response > html", xml).each ( function()	{
				ajaxResponseHtml = ($(this).text());
			});
	
			$("response > error", xml).each ( function() {
				ajaxResponseErrors = ($(this).text());
			});
	
			if(ajaxResponseErrors == 0)	{
				$('#' + updateHtmlElement).html(ajaxResponseHtml);
			}
		}
	});
}

Comment.prototype.getComment = function(id, target) 
{
	var ajaxResponseHtml 	= null;
	var ajaxResponseErrors 	= null;
	var request = this.request;
	var target = target;
	request += '&start=getComment';
	request += '&id=' + id;
	
	$.ajax({
		type: "get",
		url: request,
		dataType: "xml",
		global: false,
		success: function(xml)
		{
			$("response > html", xml).each ( function()	{
				ajaxResponseHtml = ($(this).text());
			});
	
			$("response > error", xml).each ( function() {
				ajaxResponseErrors = ($(this).text());
			});
	
			if(ajaxResponseErrors == 0)	{
				target.val(ajaxResponseHtml);
			}
		}
	});
}

Comment.prototype.reportToModerate = function(id) 
{
	var ajaxResponseHtml 	= null;
	var ajaxResponseErrors 	= null;
	var request = this.request;
	var target = target;
	request += '&start=reportToModerate';
	request += '&id=' + id;
	
	$.ajax({
		type: "get",
		url: request,
		dataType: "xml",
		global: false,
		success: function(xml)
		{
			$("response > html", xml).each ( function()	{
				ajaxResponseHtml = ($(this).text());
			});
	
			$("response > error", xml).each ( function() {
				ajaxResponseErrors = ($(this).text());
			});
	
			if(ajaxResponseErrors == 0)	{
			}
		}
	});
}

Comment.prototype.add = function(comment, commentId) 
{
	var obj		    		= this;
	var argv		    	= arguments;
	var ajaxResponseHtml 	= null;
	var ajaxResponseErrors 	= null;
	var updateHtmlElement 	= this.updateHtmlElement;
	var request = this.request;
	
	if (comment_to_add_pending == true) {
		return;
	} else {
		comment_to_add_pending = true;
	}
	if($.trim($(comment).val()) == '') {
		comment_to_add_pending = false;
		msginfo("Komentarz jest pusty");
		return;
	}
	request += '&start=add';
	obj = this
	$.ajax({
		async : false,
		type: "post",
		url: request,
		data: {
			type : this.type,
			id : this.id,
			cmc_id_parent : commentId,
			comment : $(comment).val()
		},
		dataType: "xml",
		global: false,
		success: function(xml)
		{
			$("response > html", xml).each ( function()	{
				ajaxResponseHtml = ($(this).text());
			});
	
			$("response > error", xml).each ( function() {
				ajaxResponseErrors = ($(this).text());
			});
	
			if(ajaxResponseErrors == 0)	{
				this.success = true;
				$(comment).attr('value', '');
				obj.list();
			} else if(ajaxResponseErrors == 1)	{
				new UserLogin().show(function()
				{
					argv.callee.apply(obj,argv)
				});
			} else if(ajaxResponseErrors == 2)	{
				msgerror("Komentarz zawiera niedozwolone treści!!!");
			} else if(ajaxResponseErrors == 3)	{
				msgerror("Nie spamuj!!!");
			}
			comment_to_add_pending = false;
		},
		error: function(xml)
		{
			comment_to_add_pending = false;
		},
		complete: function()
		{
			if(this.success)
			{
				this.success = false;
			}
			comment_to_add_pending = false;
		}
	});
}
Comment.prototype.enable = function(id) 
{
	var obj		    		= this;
	var argv		    	= arguments;
	var ajaxResponseHtml 	= null;
	var ajaxResponseErrors 	= null;
	var updateHtmlElement 	= this.updateHtmlElement;
	var request = BASE_URL + 'indexajax.php?action=CommentModerate';
	
	request += '&start=enableComment';
	request += '&id=' + id;
	
	$.ajax({
		type: "get",
		url: request,
		dataType: "xml",
		global: false,
		success: function(xml)
		{
			$("response > html", xml).each ( function()	{
				ajaxResponseHtml = ($(this).text());
			});
	
			$("response > error", xml).each ( function() {
				ajaxResponseErrors = ($(this).text());
			});
	
			if(ajaxResponseErrors == 0)	{

			} else if(ajaxResponseErrors == 1) {
				new UserLogin().show(function(){argv.callee.apply(obj,argv)});
			}
		}
	});
}

Comment.prototype.disable = function(id) 
{
	var obj		    		= this;
	var argv		    	= arguments;
	var ajaxResponseHtml 	= null;
	var ajaxResponseErrors 	= null;
	var updateHtmlElement 	= this.updateHtmlElement;
	var request = BASE_URL + 'indexajax.php?action=CommentModerate';
	
	request += '&start=disableComment';
	request += '&id=' + id;
	
	$.ajax({
		type: "get",
		url: request,
		dataType: "xml",
		global: false,
		success: function(xml)
		{
			$("response > html", xml).each ( function()	{
				ajaxResponseHtml = ($(this).text());
			});
	
			$("response > error", xml).each ( function() {
				ajaxResponseErrors = ($(this).text());
			});
	
			if(ajaxResponseErrors == 0)	{

			} else if(ajaxResponseErrors == 1) {
				new UserLogin().show(function(){argv.callee.apply(obj,argv)});
			}
		}
	});
}

Comment.prototype.remove = function(id) 
{
	var obj		    		= this;
	var argv		    	= arguments;
	var ajaxResponseHtml 	= null;
	var ajaxResponseErrors 	= null;
	var updateHtmlElement 	= this.updateHtmlElement;
	var request = BASE_URL + 'indexajax.php?action=CommentModerate';
	var id 	= id;
	request += '&start=removeComment';
	request += '&id=' + id;
	
	$.ajax({
		type: "get",
		url: request,
		dataType: "xml",
		global: false,
		success: function(xml)
		{
			$("response > html", xml).each ( function()	{
				ajaxResponseHtml = ($(this).text());
			});
	
			$("response > error", xml).each ( function() {
				ajaxResponseErrors = ($(this).text());
			});
	
			if(ajaxResponseErrors == 0)	{
				$('#usun-'+id).parent().parent().parent().parent().find('div.wpis div.txt').html('<span>[Komentarz został usunięty przez użytkownika]</span>');
				$('#usun-'+id).parent().remove();
			} else if(ajaxResponseErrors == 1) {
				new UserLogin().show(function(){argv.callee.apply(obj,argv)});
			}
		}
	});
}

Comment.prototype.changeCommentsActivation = function(checked) 
{
	var obj		    		= this;
	var argv		    	= arguments;
	var ajaxResponseHtml 	= null;
	var ajaxResponseErrors 	= null;
	var updateHtmlElement 	= this.updateHtmlElement;
	var request = BASE_URL + 'indexajax.php?action=USRAccountActivity';
	
	request += '&start=setCommentsActive';
	request += '&value=' + (checked ? '1' : '0');
	
	$.ajax({
		type: "get",
		url: request,
		dataType: "xml",
		global: false,
		success: function(xml)
		{
			$("response > html", xml).each ( function()	{
				ajaxResponseHtml = ($(this).text());
			});
	
			$("response > error", xml).each ( function() {
				ajaxResponseErrors = ($(this).text());
			});
	
			if(ajaxResponseErrors == 0)	{

			} else if(ajaxResponseErrors == 3) {
				new UserLogin().show(function(){argv.callee.apply(obj,argv)});
			}
		}
	});
}

