var JXComments=new Class({
	getOptions:function(){return{};},
	initialize:function(options){
		this.setOptions(this.getOptions(),options);
		if(this.options.initialize)
			this.options.initialize.call(this);
		this.respondTrigger=$E('#leave-response a');
		if($chk(this.respondTrigger)){
			this.respondTrigger.addEvent('click',function(e){
				new Event(e).stop();
				data=Json.evaluate(this.rel.match(/{[^}]*}/)[0]);
				Comments.getform(data);
			});
		}else{
			if($chk($('respond-form'))){
				this.decorateform();
			}
		}
	},
	getcomments:function(){},
	getform:function(data){
		if($chk(this.formSlide)){
			this.formSlide.toggle();
			return true;
		}
		var url=data.base+'index.php?option=com_comments&task=comment.getForm&protocol=ajax&thread_id='+data.thread_id;
		var request=new Ajax(url,{method:'get',update:$('respond-container'),onComplete:function(r){
			if($chk($('respond-form'))){
				this.decorateform();
			}}.bind(this)}).request();},
			decorateform:function(){
				this.formContainer=$('respond-container');
				this.formSlide=new Fx.Slide(this.formContainer);
				this.form=$('respond-form');
				var query='protocol=json&tmpl=component&format=raw';
				var action=this.form.getProperty('action');
				this.form.setProperty('action',action.contains('?')?action+'&'+query:action+'?'+query);
				refresher=$E('a.captcha-image-refresh',this.form);
				if($chk(refresher)){
					refresher.addEvent('click',function(e){
						new Event(e).stop();
						Comments.refreshcaptcha();
					});
				}
				if((window.Recaptcha!=undefined)){
					Recaptcha.create(cOptions.reCaptchaPubKey,'recaptchaContainer',{theme:'clean'});
				}
				if((window.JXBBCodeEditor!=undefined)){new JXBBCodeEditor();}
				if((window.postEditor!=undefined)){
					new postEditor.create('comment_body','captcha',postlanguage.DEFAULT);
				}
				this.form.addEvent('submit',function(e){
					new Event(e).stop();
					var valid=document.formvalidator.isValid(this);
					if(valid==false){
						if(this.name&&this.name.hasClass('invalid')){
							alert(JXLang.INVALID_NAME_TXT);
						} else if(this.email&&this.email.hasClass('invalid')){
							alert(JXLang.INVALID_EMAIL_TXT);
						}else if(this.body.hasClass('invalid')){
							alert(JXLang.INVALID_BODY_TXT);
						}
						return false;
					}
					var log=$('respond-container').addClass('ajax-loading');
					this.send({onComplete:function(responseText){
							log.removeClass('ajax-loading');
							response=Json.evaluate(responseText);
							replaceTokens(response.token);
							if(response.error==true){
								alert(response.message);
							}else{
								n=$('commentlist').getElements('li').length;
								comment=new Element('li').setProperty('id','comment-'+response.id);
								comment.addClass('comment');
								if(response.position=='top'){
									if($chk($('commentlist').getFirst())){
										comment.addClass($('commentlist').getFirst().hasClass('odd')?'even':'odd');
									}else{
										comment.addClass('odd');
									}
									comment.setHTML(response.body);
									comment.injectTop($('commentlist'));
								}else{
									comment.addClass(((n%2)==0)?'odd':'even');
									comment.setHTML(response.body);
								comment.injectInside($('commentlist'));
								}
								$('respond-form').reset();
								Comments.refreshcaptcha();
								comment.setOpacity(0);
								new Fx.Style(comment,'opacity',{duration:1500}).start(0,1);
								$('comments-num').setHTML(response.num_text);
							}
						},onFailure:function(x){
							log.removeClass('ajax-loading');
							r=Json.evaluate(x.responseText);
							replaceTokens(r.token);
							alert(r.message);
						}
					});
				});},refreshcaptcha:function(){
					if(window.Recaptcha!=undefined){
						Recaptcha.reload();
					}else if(window.cOptions!=undefined&&cOptions.captcha){
						img=$E('img.captcha-image',this.form);
						img.src=img.src+'#';
					}
				},
	submit:function(){}
});
JXComments.implement(new Events);
JXComments.implement(new Options);
function replaceTokens(n){
	$(document).getElements('input').each(function(e){
		if(e.getProperty('name')&&e.getProperty('name').length==32&&e.getProperty('value')=='1'&&e.getProperty('type')=='hidden'){
			e.setProperty('name',n);
		}
	});
}

window.addEvent('domready',function(){Comments=new JXComments();});
