/*
	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)
{	var th=this;
	this.AddComment=function()
	{		var name=Trim(Id('name_'+u).value);
		var text=Trim(EDITOR.GetValue('text_'+u));
		var email=Id('email'+u) ? Id('email'+u).value : '';
		var check=Id('check_'+u);
		if(check)
		{
			check=check.value;
			var sess_id=Id(u+'_id').value;
		}
		else
		{
			var sess_id='';
			check=false;
		}
		var parent=Id(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;
		}

		Ajax(
				m,
				{					'event':'comments',
					'comments':
					{						'type':'add',
						'v':v,
						'check':check,
						'sess_id':sess_id,
						'u':u,
						'parent':parent,
						'text':text,
						'name':name,
						'email':email,
						'last':this.last,
						'view':view,
						'comm_n':this.comm_n,
						'link':l,
						'end_l':end_l,
						'load':this.pages==this.page ? 1 : 0					}				},
				function(result)
				{					if(result['error_n'])
					{						if(result['error_n']==2)
							Id(u+'_img').onclick();					}
					else
					{						th.PostCleanAll();
						if(th.pages!=th.page && th.pages)
							th.GoToPage(0);
						else if(result['comments'])
						{
							if(result['type']=='str')
							{								if(view==2)
									Id("posts_"+u).innerHTML+=result['comments'];
								else
									Id("posts_"+u).innerHTML=result['comments']+Id("posts_"+u).innerHTML;
							}
							else if(result['type']=='ent')
								th.div.innerHTML=result['comments'];
							else if(result['type']=='arr')
								for(var i in result['comments'])
									if(i==0)
										Id("posts_"+u).getElementsByTagName('ul')[0].innerHTML+=result['comments'][i];
									else
										Id(u+'_l'+i).innerHTML+=result['comments'][i];
							if(result['comm_n'])
							{
								th.comm_n=result['comm_n'];
								th.last=result['lastid'];
								window.location.hash='comment'+th.last;
							}
						}
					}
					if(result['message'])
						$('#'+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;
		Ajax(
				m,
				{
					'event':'comments',
					'comments':
					{
						'type':'delete',
						'v':v,
						'id':id
					}
				},
				function(result)
				{
					if(result=='ok')
					{						var c=Id('d_comment_'+id);
						if(view==3)
							c=c.parentNode;
						c.parentNode.removeChild(c);					}
				},
			false
		);
		return false;	}

	this.Edit=function(id)
	{		Ajax(
				m,
				{
					'event':'comments',
					'comments':
					{
						'type':'edit',
						'u':u,
						'v':v,
						'id':id
					}
				},
				function(result)
				{
					var c=Id(u+'_t'+id);
					c.old_html=c.innerHTML;
					$(c).html(result['html']);
				},
			false
		);
		return false;	}

	this.Save=function(id)
	{		var text=Trim(EDITOR.GetValue(u+'_edit'+id));
		if(!text)
		{
			alert(el_lang['comment_text_empty']);
			return false;
		}
		Ajax(
				m,
				{
					'event':'comments',
					'comments':
					{
						'type':'save',
						'u':u,
						'v':v,
						'id':id,
						'text':text
					}
				},
				function(result)
				{
					$('#'+u+'_t'+id).html(result['html']);
				},
			false
		);
		return false;	}

	this.Cancel=function(id)
	{		var c=Id(u+'_t'+id);
		c.innerHTML=c.old_html;
		return false;
	}

	this.AddKarma=function(id,n)
	{		Ajax(
				m,
				{
					'event':'comments',
					'comments':
					{
						'type':'karma',
						'u':u,
						'v':v,
						'id':id,
						'n':n
					}
				},
				function(result)
				{
					Id('k_'+u+'_'+id).innerHTML=result['html'];
				},
			false
		);
		return false;	}

	this.AnswerTo=function(id,n)
	{		if(id)
		{			Id('ans_'+u).style.display='';
			Id(u+'_parent').value=id;
			Id('ans_'+u).getElementsByTagName('td')[1].innerHTML=el_lang['answer_to_comment']+n;
			window.location.hash='ceditor';
		}
		else
		{			Id('ans_'+u).style.display='none';
			Id(u+'_parent').value='0';		}
		return false;	}

	this.Update=function()
	{		if(this.pages!=this.page && view!=3 && this.pages)
			return this.GoToPage(this.page);
		Ajax(
				m,
				{
					'event':'comments',
					'comments':
					{
						'type':'new_posts',
						'v':v,
						'u':u,
						'last':this.last,
						'view':view,
						'comm_n':this.comm_n,
						'link':l,
						'end_l':end_l
					}
				},
				function(result)
				{
					if(result['no'])
						return;
					if(th.last==0)
						th.div.innerHTML=result['comments'];
					else if(view!=3)
					{						if(view==2)
							Id("posts_"+u).innerHTML+=result['comments'];
						else
							Id("posts_"+u).innerHTML=result['comments']+Id("posts_"+u).innerHTML;
					}
					else
						for(var i in result['comments'])
							if(i==0)
								Id("posts_"+u).getElementsByTagName('ul')[0].innerHTML+=result['comments'][i];
							else
								Id(u+'_l'+i).innerHTML+=result['comments'][i];
					th.comm_n=result['comm_n'];
					th.last=result['lastid'];
					window.location.hash='comment'+th.last;
				},
			false
		);
		return false;
	}

	this.GoToPage=function(n)
	{		Ajax(
				m,
				{
					'event':'comments',
					'comments':
					{
						'type':'page',
						'v':v,
						'u':u,
						'view':view,
						'link':l,
						'end_l':end_l,
						'page':n
					}
				},
				function(result)
				{
					th.div.innerHTML=result['comments'];
					th.pages=result['pages'];
					if(!n || view==2)
						th.last=result['last'];
					th.page=result['page'];
					th.comm_n=result['comm_n'];
					window.location.hash='comments';
				},
			false
		);		return false;	}

	this.PostCleanAll=function()
	{		$('#'+u+'_message').hide();
		EDITOR.SetValue('','text_'+u);
		var check=Id('check_'+u);
		if(check)
		{
			check.value='';
			Id(u+'_img').onclick();
		}	}

	this.MarkSpam=function(id)
	{		Ajax(
				m,
				{
					'event':'comments',
					'comments':
					{
						'type':'mark_spam',
						'v':v,
						'id':id
					}
				},
				function(result)
				{
					if(result=='ok')
					{
						$('#'+u+'_ms'+id).hide();
						$('#'+u+'_ums'+id).show();
					}
				},
			false
		);
		return false;	}

	this.UnMarkSpam=function(id)
	{
		Ajax(
				m,
				{
					'event':'comments',
					'comments':
					{
						'type':'unmark_spam',
						'v':v,
						'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');
		Ajax(
				m,
				{
					'event':'comments',
					'comments':
					{
						'type':'set_status',
						'v':v,
						'id':id,
						'status':status
					}
				},
				function(result)
				{
					if(result=='ok' && obj)
					{
						$(obj).attr('disabled','');
						alert('Ok');
					}
				},
			false
		);
		return false;
	}

	this.page=p;
	this.last=last;
	this.pages=pages;
	this.div=Id('d_'+u);
	this.comm_n=comm_n;
}

