var comment = {
	del:function(el,id){
		this.hide();
		if (confirm (el.title+'?')) $.post(el.href,{id:id},function(){
			var d = $(el).parents('.sub_com,.first_com');
			d.addClass('disabled');
			d.click(function(event){event.stopPropagation();});
		})
	},
	hide: function(){
		if (!this.cnt) return;
		this.cnt.hide();
		this.form.hide();
	},
	showForm: function(el,parent){
		var f = this.getForm();
		f.insertAfter(el);
		f.show();
		var offset = $(el).next().offset();
		this.cnt.css({left:offset.left,top:offset.top+25});
		this.cnt.show();		
	},
	rollup:function(ql){
		this.hide();
		$(ql).find(".comment-trunc").hide();
		$(ql).find(".com-avatar,.comment-full").show();
		
	},
	rolldown:function(ql){
		this.hide();
		var trunc = $(ql).find(".comment-trunc"), hide = ".com-avatar";
		trunc.show();
		if (trunc.length!=0) hide+=',.comment-full';
		$(ql).find(hide).hide();
		
	},
	roolldownFirst: function(el){
		this.hide();
		$(el).hide();
		$(el).prev().show();
		var first = $(el).parents('.first_com');
		while (first.next().hasClass('sub_com')) {
			first = first.next();
			this.rolldown(first);
		}
	},	
	full:function(el){
		this.rollup($(el).parents('.sub_com'));
	},
	roollupFirst: function(el){
		this.hide();
		$(el).hide();
		$(el).next().show();
		var first = $(el).parents('.first_com');
		while (first.next().hasClass('sub_com')) {
			first = first.next();
			this.rollup(first);
		}
	},
	getForm: function(){
		if(!this.form) { 
			this.mce = new tinymce.Editor('comment-area', this.MCEcnf );
			this.mce.render();
			this.form = $('#comment-form');
			this.cnt = $('#cnt-comment-area');
			this.isLast = $('#notlast').length==0;
			var com = this;
			$('#comment-send').click(function(){
				var v = com.mce.getContent();
				if (v =='') {
					alert("Комментарий не может быть пустым!")
					return;
				}
				var captha = $(this.form).find('[name=captcha]'); 
				captha_v = captha.length!=0?captha[0].value:null;
				if(captha_v=='') captha_v=null;
				var name = $(this.form).find('[name=name]'); 
				name_v = name.length!=0?name[0].value:null;
				if(name_v=='') name_v=null;
				
				var email = $(this.form).find('[name=email]');
				if(email.val()&&email.val()!=''&&!xp.form.validators.pregs.email.test(email.val())) {
					email.focus()
					alert("Не верно указан E-mail!")
					return
				}
				if(name.length!=0&&!name_v) {
					name.focus()
					alert("Укажите имя!")
					return;				
				}
				if(captha.length!=0&&!captha_v){
					captha.focus()
					alert("Укажите код на картинке!")
				}
				$(this).hide();
				$(this).next().show();
				var but = this;
				var root = $(this.form).prev()[0].innerHTML=='root';
				var anchor ={
					append:root&&com.isLast?$('#comtent_list'):false,
					after: !root?$(this.form).parent():false,
					last: root&&!com.isLast?"?p=last":false
				}
				var avatar =$(this.form).find('[name=avatar_id]');
				avatar=avatar.length!=0?avatar[0].value:0;
				$.ajax({
					type:'POST',
					url:this.src,
					data:{parent:$(this.form).prev()[0].id,value:v, email:email.val(), avatar_id:avatar,captcha:captha_v,name:name_v},
					complete:function(){
						var captha = $('.a-captcha img').trigger('click');
						$(but).next().hide();
						$(but).show();
					},
					success:function(data){
						com.hide();
						com.mce.setContent('');
						$('#comtent_list').show();
						if(anchor.append) anchor.append.append( data );
						if(anchor.after) anchor.after.after( data );
						if (anchor.last) window.location.href = anchor.last;
					},
					error:function(o){
						if (o.responseText.length<100) alert(o.responseText);
						return;						
					}
				});
			})
		}
		return this.form;
	},
	MCEcnf:{
		mode : "textareas",
		editor_selector : "mceEditor",
		theme : "advanced",
		theme_advanced_buttons1: "bold, italic, underline, strikethrough, undo,redo, link, unlink",
		theme_advanced_buttons2: "",	
		theme_advanced_buttons3: "",		
		theme_advanced_toolbar_location : "top",
		theme_advanced_statusbar_location : "bottom",
			
		theme_advanced_resize_horizontal : false,
		theme_advanced_resizing : false,
	    
	    language: 'ru',
		
		convert_urls : false	
	}	
}

//$(window).resize(function(){ comment.hide();});


