/*
	Copyright © Eleanor CMS
	URL: http://eleanor-cms.ru, http://eleanor-cms.com
	E-mail: support@eleanor-cms.ru
	Developing: Alexander Sunvas*
	Interface: Rumin Sergey
	=====
	*Pseudonym. See addons/copyrights/info.txt for more information.
*/
/*
	Конструктор комментариев:
	m - ид модуля
	v - признак модуля
	u - уникальность
	l - линк
	end_l -окончание ссылки
	p - страница
	last - ид последнего коммента
	view - вид комментариев
	pages - всего страниц
	comm_n - номер последнего комментария.
*/
function Comments(m,v,u,l,end_l,p,last,view,pages,comm_n)
{	this.AddComment=function()
	{		var name=Trim(Id('name_'+this.u).value);
		var text=Trim(EDITOR.GetValue('text_'+this.u));
		var email=Id('email'+this.u) ? Id('email'+this.u).value : '';
		var check=Id('check_'+this.u);
		if(check)
		{
			check=check.value;
			var sess_id=Id(this.u+'_id').value;
		}
		else
		{
			var sess_id='';
			check=false;
		}
		var parent=Id(this.u+'_parent');
		parent=parent ? parent.value : 0;
		if(!name)
		{
			alert(el_lang['name_not_filled']);
			return false;
		}
		if(!text)
		{
			alert(el_lang['comment_text_empty']);
			return false;
		}
		var u=this;
		Ajax(
				this.mid,
				{					'event':'comments',
					'comments':
					{						'type':'add',
						'v':this.value,
						'check':check,
						'sess_id':sess_id,
						'u':this.u,
						'parent':parent,
						'text':text,
						'name':name,
						'email':email,
						'last':this.last,
						'view':this.view,
						'comm_n':this.comm_n,
						'link':this.link,
						'end_l':this.end_l,
						'load':this.pages==this.page ? 1 : 0					}				},
				function(result)
				{					if(result['error_n'])
					{						if(result['error_n']==2)
							Id(u.u+'_img').onclick();					}
					else
					{						u.PostCleanAll();
						if(u.pages!=u.page && u.pages)
							u.GoToPage(0);
						else if(result['comments'])
						{
							if(result['type']=='str')
							{								if(u.view==2)
									Id("posts_"+u.u).innerHTML+=result['comments'];
								else
									Id("posts_"+u.u).innerHTML=result['comments']+Id("posts_"+u.u).innerHTML;
							}
							else if(result['type']=='ent')
								u.div.innerHTML=result['comments'];
							else if(result['type']=='arr')
								for(var i in result['comments'])
									if(i==0)
										Id("posts_"+u.u).getElementsByTagName('ul')[0].innerHTML+=result['comments'][i];
									else
										Id(u.u+'_l'+i).innerHTML+=result['comments'][i];
							if(result['comm_n'])
							{
								u.comm_n=result['comm_n'];
								u.last=result['lastid'];
								window.location.hash='comment'+u.last;
							}
						}
					}
					if(result['message'])
						$('#'+u.u+'_message').html(result['message']).show();
				},
			false
		);
		return false;	}

	this.Delete=function(id)
	{		if(!confirm(el_lang['are_you_sure_to_del_comment']))
			return false;
		var u=this;
		Ajax(
				this.mid,
				{
					'event':'comments',
					'comments':
					{
						'type':'delete',
						'v':this.value,
						'id':id
					}
				},
				function(result)
				{
					if(result=='ok')
					{						var c=Id('d_comment_'+id);
						if(u.view==3)
							c=c.parentNode;
						c.parentNode.removeChild(c);					}
				},
			false
		);
		return false;	}

	this.Edit=function(id)
	{		var u=this;
		Ajax(
				this.mid,
				{
					'event':'comments',
					'comments':
					{
						'type':'edit',
						'u':this.u,
						'v':this.value,
						'id':id
					}
				},
				function(result)
				{
					var c=Id(u.u+'_t'+id);
					c.old_html=c.innerHTML;
					$(c).html(result['html']);
				},
			false
		);
		return false;	}

	this.Save=function(id)
	{		var text=Trim(EDITOR.GetValue(this.u+'_edit'+id));
		if(!text)
		{
			alert(el_lang['comment_text_empty']);
			return false;
		}
		var u=this;
		Ajax(
				this.mid,
				{
					'event':'comments',
					'comments':
					{
						'type':'save',
						'u':this.u,
						'v':this.value,
						'id':id,
						'text':text
					}
				},
				function(result)
				{
					$('#'+u.u+'_t'+id).html(result['html']);
				},
			false
		);
		return false;	}

	this.Cancel=function(id)
	{		var c=Id(this.u+'_t'+id);
		c.innerHTML=c.old_html;
		return false;
	}

	this.AddKarma=function(id,n)
	{		var u=this;
		Ajax(
				this.mid,
				{
					'event':'comments',
					'comments':
					{
						'type':'karma',
						'u':this.u,
						'v':this.value,
						'id':id,
						'n':n
					}
				},
				function(result)
				{
					Id('k_'+u.u+'_'+id).innerHTML=result['html'];
				},
			false
		);
		return false;	}

	this.AnswerTo=function(id,n)
	{		if(id)
		{			Id('ans_'+this.u).style.display='';
			Id(this.u+'_parent').value=id;
			Id('ans_'+this.u).getElementsByTagName('td')[1].innerHTML=el_lang['answer_to_comment']+n;
			window.location.hash='ceditor';
		}
		else
		{			Id('ans_'+this.u).style.display='none';
			Id(this.u+'_parent').value='0';		}
		return false;	}

	this.Update=function()
	{		if(this.pages!=this.page && this.view!=3 && this.pages)
			return this.GoToPage(this.page);
		u=this;
		Ajax(
				this.mid,
				{
					'event':'comments',
					'comments':
					{
						'type':'new_posts',
						'v':this.value,
						'u':this.u,
						'last':this.last,
						'view':this.view,
						'comm_n':this.comm_n,
						'link':this.link,
						'end_l':this.end_l
					}
				},
				function(result)
				{
					if(result['no'])
						return;
					if(u.last==0)
						u.div.innerHTML=result['comments'];
					else if(u.view!=3)
					{						if(u.view==2)
							Id("posts_"+u.u).innerHTML+=result['comments'];
						else
							Id("posts_"+u.u).innerHTML=result['comments']+Id("posts_"+u.u).innerHTML;
					}
					else
						for(var i in result['comments'])
							if(i==0)
								Id("posts_"+u.u).getElementsByTagName('ul')[0].innerHTML+=result['comments'][i];
							else
								Id(u.u+'_l'+i).innerHTML+=result['comments'][i];
					u.comm_n=result['comm_n'];
					u.last=result['lastid'];
					window.location.hash='comment'+u.last;
				},
			false
		);
		return false;
	}

	this.GoToPage=function(n)
	{		var u=this;
		Ajax(
				this.mid,
				{
					'event':'comments',
					'comments':
					{
						'type':'page',
						'v':this.value,
						'u':this.u,
						'view':this.view,
						'link':this.link,
						'end_l':this.end_l,
						'page':n
					}
				},
				function(result)
				{
					u.div.innerHTML=result['comments'];
					u.pages=result['pages'];
					if(!n || u.view==2)
						u.last=result['last'];
					u.page=result['page'];
					u.comm_n=result['comm_n'];
					window.location.hash='comments';
				},
			false
		);		return false;	}

	this.PostCleanAll=function()
	{		$('#'+this.u+'_message').hide();
		EDITOR.SetValue('','text_'+this.u);
		var check=Id('check_'+this.u);
		if(check)
		{
			check.value='';
			Id(this.u+'_img').onclick();
		}	}

	this.MarkSpam=function(id)
	{		var u=this.u;
		Ajax(
				this.mid,
				{
					'event':'comments',
					'comments':
					{
						'type':'mark_spam',
						'v':this.value,
						'id':id
					}
				},
				function(result)
				{
					if(result=='ok')
					{
						$('#'+u+'_ms'+id).hide();
						$('#'+u+'_ums'+id).show();
					}
				},
			false
		);
		return false;	}

	this.UnMarkSpam=function(id)
	{
		var u=this.u;
		Ajax(
				this.mid,
				{
					'event':'comments',
					'comments':
					{
						'type':'unmark_spam',
						'v':this.value,
						'id':id
					}
				},
				function(result)
				{
					if(result=='ok')
					{
						$('#'+u+'_ums'+id).hide();
						$('#'+u+'_ms'+id).show();
					}
				},
			false
		);
		return false;
	}

	this.SetStatus=function(id,status,obj)
	{		if(obj)
			$(obj).attr('disabled','disabled');
		var u=this;
		Ajax(
				this.mid,
				{
					'event':'comments',
					'comments':
					{
						'type':'set_status',
						'v':this.value,
						'id':id,
						'status':status
					}
				},
				function(result)
				{
					if(result=='ok' && obj)
					{
						$(obj).attr('disabled','');
						alert('Ok');
					}
				},
			false
		);
		return false;
	}

	this.mid=m;
	this.value=v;	this.u=u;
	this.link=l;
	this.end_l=end_l;
	this.page=p;
	this.last=last;
	this.view=view;
	this.pages=pages;
	this.div=Id('d_'+u);
	this.comm_n=comm_n;
}

