var Comment = new Class({
	Implements: [Options, Events],
	options: {
		cid	: '',
		iid	: '',
		lng	: '',
		dt	: '',
		module	: '',
		
		CommentElementClass	: 'formcheck',
		
		CommentsTarget : '.comments',
		CommentFormTarget	: '.commentform',
		
		connected	: false,
		
		// events
		onGetComments	: function()	{
			// get existing comments
			this._getComments();
			
			// build form
			this._buildCommentForm();
		},
		
		onSubmitForm	: function()	{
			this.formcheck.onSubmit();
			if (this.formcheck.isFormValid()) {
				this.form.send();
				this._emptyForm()
			}
		},
		
		onSignOut	: function()	{
			this._signOut();
		},
	},
	
	/* 
		initialize
		
		class contructor
	*/
	initialize: function(target,options){
		this.setOptions(options);
		
		this.target = target;
		
		this._getParams();
		
		this.Spinner = new Spinner(this.target);
		
		// do nothing if we miss a param
		if (!this.options.cid || !this.options.iid || !this.options.lng) return;
		
		// check if we're connected
		// shows the form and the comments once finished
		this._checkConnected();
	},
	
	/*
		_getParams
		private
		
		gets the params like cid, iid...
	*/
	_getParams : function()	{
		// get cid from body id
		if (!this.options.cid) {
			var bodyid = $(document.body).get('id');
			if (bodyid.match(/page\d+/))
				this.options.cid = bodyid.replace(/page/,'');
		}
		// get iid from target id
		if (!this.options.iid) {
			var targetid = $(this.target).get('id');
			if (targetid.match(/item\d+/))
				this.options.iid = targetid.replace(/item/,'');
		}
		// get lng from meta-tag content-language
		if (!this.options.lng)	{
			var metael = $(document.head).getElement('meta[name=content-language]');
			if (metael)
				this.options.lng = metael.get('content');
		}
		// get dt from main div
		if (!this.options.dt) {
			var maindiv = $(document.body).getElement('.main');
			if (maindiv) {
				if (maindiv.hasClass('albumlist')) {
					this.options.dt = 'album';
					this.options.module = 'album';
				}
				else	{
					this.options.dt = 'news';
					this.options.module = 'info';
				}
			}
		}
	},
	
	/*
		_getComments
		private
		
		gets existing comments of this target
	*/
	_getComments : function()	{
		var url = site.options.cs2+'?server='+site.options.server+'&lang='+this.options.lng+'&action=getajaxitemdetail&datatype='+this.options.dt+'&CID='+this.options.cid+'&IID='+this.options.iid+'&layout=comments';
		new Request.HTML({
			url		: url,
			method	: 'get',
			update	: this.target.getElement(this.options.CommentsTarget),
			onComplete	: function()	{
				IFInterface_Obj = new IFInterface(IF_Options);
			},
			noCache	: true
		}).send();
	},
	
	/*
		_buildCommentForm
		private
		
		builds the form to add a comment
	*/	
	_buildCommentForm : function()	{
		if (this.form) {
			this._emptyForm();
			this.form.destroy();
		}
		this.form = new Element('form',{'class':this.CommentFormClass,'method':'post'});
		
		if (this.options.connected) {
			new Element('input',{'type':'hidden','name':'lng','value':this.options.lng}).inject(this.form);
			new Element('input',{'type':'hidden','name':'m','value':'Comment'}).inject(this.form);
			new Element('input',{'type':'hidden','name':'cid','value':this.options.cid}).inject(this.form);
			new Element('input',{'type':'hidden','name':'iid','value':this.options.iid}).inject(this.form);
			new Element('input',{'type':'hidden','name':'comments_categoryID','value':this.options.cid}).inject(this.form);
			new Element('input',{'type':'hidden','name':'comments_title','value':''}).inject(this.form);
			new Element('input',{'type':'hidden','name':'module','value':this.options.module}).inject(this.form);
			
			var fs = new Element('fieldset');
				new Element('legend',{'html':Dic.AddComment}).inject(fs);
				new Element('label',{'html':Dic.YourComment}).inject(fs);
				new Element('textarea',{'class':'validate[\'required\']','name':'comments_content','cols':'30','rows':'5'}).inject(fs);
				new Element('input',{'type':'button','name':'commentbtn','value':Dic.AddComment})
					.addEvent('click',function(){this.fireEvent('submitForm')}.bind(this)).inject(fs);
				new Element('input',{'type':'button','name':'logoutbtn','value':Dic.Signout})
					.addEvent('click',function(){this.fireEvent('signOut')}.bind(this)).inject(fs);
			fs.inject(this.form);
			this.form.set('send',{url : site.options.cs3, method : 'post',onRequest: function()	{this.Spinner.show();}.bind(this),onComplete : function(){this.Spinner.hide();this._getComments()}.bind(this)});
		}
		else	{
			new Element('input',{'type':'hidden','name':'lng','value':this.options.lng}).inject(this.form);
			new Element('input',{'type':'hidden','name':'m','value':'Account'}).inject(this.form);
			
			var fs = new Element('fieldset');
				new Element('legend',{'html':Dic.Signin}).inject(fs);
				var name = new Element('div',{'class':'name'}).inject(fs);
				new Element('label',{'html':Dic.Email}).inject(name);
				new Element('input',{'class':'validate[\'required\',\'email\']','type':'text','name':'username','value':''}).inject(name);
				var email = new Element('div',{'class':'email'}).inject(fs);
				new Element('label',{'html':Dic.Password}).inject(email);
				new Element('input',{'class':'validate[\'required\']','type':'password','name':'password','value':''}).addEvent('keyup',function(e)	{var unicode=e.event.keyCode? e.event.keyCode : e.event.charCode; if (unicode == '13') this.fireEvent('submitForm')}.bind(this)).inject(email);
				new Element('div',{'style':'clear:both'}).inject(fs);
				new Element('input',{'type':'button','name':'signinbtn','value':Dic.Signin})
					.addEvent('click', function(){this.fireEvent('submitForm')}.bind(this)).inject(fs);
				new Element('input',{'type':'button','name':'registerbtn','value':Dic.Register})
					.addEvent('click',function(){this._buildRegisterForm();}.bind(this)).inject(fs);
				fs.inject(this.form);
			this.form.set('send',
				{	url : site.options.cs3,
					method : 'post',
					onRequest: function()	{
						this.Spinner.show();
					}.bind(this),
					onComplete: function(r){
						this.Spinner.hide();
						if ($('ErrorMsgElement')) $('ErrorMsgElement').destroy();
						var s = JSON.decode(r);
						if (s.return_status == 1) {
							this.options.connected = true;
							this._buildCommentForm();
						}
						else	{
							new Element('div', {
								'id': 'ErrorMsgElement',
								'html': s.return_msg
							}).inject(this.form);
						} 
					}.bind(this)
				});
		}
		
		this.form.inject(this.target.getElement(this.options.CommentFormTarget));
		
		this._initFormCheck();
	},
	
	/*
		_buildRegisterForm
		private
		
		builds the form to register
	*/	
	_buildRegisterForm : function()	{
		if (this.form) {
			this._emptyForm();
			this.form.destroy();
		}
		this.form = new Element('form',{'class':this.CommentFormClass,'method':'post'});
			new Element('input',{'type':'hidden','name':'lng','value':this.options.lng}).inject(this.form);
			new Element('input',{'type':'hidden','name':'m','value':'Account'}).inject(this.form);
			new Element('input',{'type':'hidden','name':'a','value':'register'}).inject(this.form);
			
			var fs = new Element('fieldset');
				new Element('legend',{'html':Dic.Register}).inject(fs);
				var name = new Element('div',{'class':'name'}).inject(fs);
				new Element('label',{'html':Dic.Nickname}).inject(name);
				new Element('input',{'class':'validate[\'required\']','type':'text','name':'username','value':''}).inject(name);
				var email = new Element('div',{'class':'email'}).inject(fs);
				new Element('label',{'html':Dic.Email}).inject(email);
				new Element('input',{'class':'validate[\'required\',\'email\']','type':'text','name':'contact_email','value':''}).inject(email);
				var pw1 = new Element('div',{'class':'name'}).inject(fs);
				new Element('label',{'html':Dic.Password}).inject(pw1);
				new Element('input',{'class':'validate[\'required\']','type':'password','name':'password','value':''}).inject(pw1);
				var pw2 = new Element('div',{'class':'email'}).inject(fs);
				new Element('label',{'html':Dic.ConfirmPassword}).inject(pw2);
				new Element('input',{'class':'validate[\'confirm:password\']','type':'password','name':'password2','value':''}).inject(pw2);
				new Element('div',{'style':'clear:both'}).inject(fs);
				new Element('input',{'type':'button','name':'backbtn','value':Dic.Back})
					.addEvent('click',function(){this._buildCommentForm()}.bind(this)).inject(fs);
				new Element('input',{'type':'button','name':'registerbtn','value':Dic.Register})
					.addEvent('click',function(){this.fireEvent('submitForm')}.bind(this)).inject(fs);
			fs.inject(this.form);
			this.form.set('send',{
				url: site.options.cs3,
				method: 'post',
				onRequest: function()	{
					this.Spinner.show();
				}.bind(this),
				onComplete: function(r){
					this.Spinner.hide();
					if ($('ErrorMsgElement')) $('ErrorMsgElement').destroy();
					var s = JSON.decode(r);
					if (s.return_status == 1) {
						this.options.connected = true;
						this._buildCommentForm();
					}
					else	{
						new Element('div', {
							'id': 'ErrorMsgElement',
							'html': s.return_msg
						}).inject(this.form);
					} 
				}.bind(this)
			});
		
		this.form.inject(this.target.getElement(this.options.CommentFormTarget));
		
		this._initFormCheck();
	},
	
	/*
		_initFormCheck
		private
		
		init formcheck on this.form
	*/
	_initFormCheck : function()	{
		this.formcheck = new FormCheck(this.form,{
			'submit'			: false
		});
	},
	
	/*
		_emptyForm
		private
		
		empty input-flds and textareas
	*/
	_emptyForm : function()	{
		this.form.getElements('input[type=text]').each(function(el){
			//el.value = '';
			if (el.element) el.element.destroy();
		});
		this.form.getElements('input[type=password]').each(function(el){
			el.value = '';
			if (el.element) el.element.destroy();
		});
		this.form.getElements('textarea').each(function(el){
			el.value = '';
			if (el.element) el.element.destroy();
		});
	},
	
	/*
		_signOut
		private
		
		do signout
	*/	
	_signOut : function()	{
		var url = site.options.cs3+'?'+'m=Account&a=signOut'
		new Request.JSON({
			url		: url,
			method	: 'get',
			onRequest: function()	{
				this.Spinner.show();
			}.bind(this),
			onComplete: function()	{
				this.Spinner.hide();
			}.bind(this),
			onSuccess	: function(r){
				if (r.return_status == '1') this.options.connected = false;
				this.fireEvent('getComments');
			}.bind(this),
			noCache	: true
		}).send();
	},
	
	/*
		_checkConnected
		private
		
		checks if user is connected
	*/	
	_checkConnected : function()	{
		var url = site.options.cs3+'?'+'m=Account&a=isConnected'
		new Request.JSON({
			url		: url,
			method	: 'get',
			onSuccess	: function(r){
				if (r.return_status == '1') this.options.connected = true;
				this.fireEvent('getComments');
			}.bind(this),
			noCache	: true
		}).send();
	}
});


