gbl.methods.push('CheckUser');

var CheckUser = new Class({
	
	Implements: Options,

	options: {	

		container: false		
		
	},
	
	initialize : function(options){
		
		this.setOptions(options);
		
		if (Browser.Engine.trident) return;
		
		if (!this.options.container) return;
		
		this.els = {
			'email' : this.options.container.getElement('input[name=login_email]') || false,
			'password' : this.options.container.getElement('input[name=login_password]') || false,
			'login' : this.options.container.getElement('input[type=submit]') || false,
			'header' : this.options.container.getElement('h3') || false
		}
		
		this.els.email.set('name','email');
		this.els.password.set('name','password');
		
		this.els.password.setProperty('disabled','disabled');
		
		this.els.header.set('text','Post a comment');
		this.els.login.set('value','Post your comment');		
		
		this.created_els = {}
		
		window.addEvent('reply_to_comment',this.is_reply.bind(this));
		
		//alert(this.els.email.get('value'))
		
		//if (this.els.email.get('value') != '')
		//this.check_user(this.els.email)
		
		//this.els.email.focus();
		
		this.init_check.bind(this).delay(500, this.els.email); // Wait for Safari to load preset values.
		//else this.init_check.bind(this.els.email)(); // Fire anything else.
		
		this.create_elements();
		this.add_events();
		
	},
	
	init_check : function(){
		
		this.check_user(this.els.email,true);
		
	},
	
	
	create_elements : function(){
		
		this.els.comment = new Element('div', { 'class' : 'inline_row' }).set('html',
			'<label>Comment</label>'
		+	'<textarea name="comment" class="default"></textarea>').inject(this.els.email.getParent('div'),'after')
		
		
		this.created_els.forgotten_password = this.options.container.getElement('a.forgotten_password')
		
		this.created_els.forgotten_password.dispose();
	},
	
	add_events : function(){
	
		var t = this;
		
		this.els.email.addEvent('focus',function(e){
			
			if (this.get('title')) this.set('value',this.get('title'));
			
			this.select();
			this.removeClass('highlight');
		
		})
										
		
		this.els.email.addEvent('blur',function(e){
			if (e) e.stop();
			
			t.check_user(this);
		
		})
	},
	
	is_reply : function(user,comment,id){
		
		if ($('reply_comment')) $('reply_comment').dispose();
		
		var row = new Element('div',{ 'class':'inline_row', 'id':'reply_comment' }).set('html','<label>Reply to</label><input type="hidden" name="parent" value="'+id+'" /><div class="reply"><a name="cancel_reply" class="cancel_reply">Oops! I didn\'t mean to reply to this comment.</a></div>')
		
		row.getElement('a[name=cancel_reply]').addEvent('click',function(){
		
			this.getParent('div#reply_comment').dispose();
		
		});
		
		user.inject(row.getElement('.reply'),'bottom');
		comment.inject(row.getElement('.reply'),'bottom');
		user.set('text',user.get('text') +" wrote,");
		
		row.inject(this.els.email.getParent('div'),'before');
					
	},
	
	check_user : function(el,first){
		var t = this;
		var val = el.get('value');
		var reg = new RegExp('^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]$');
		
		t.els.login.removeProperty('disabled');
		
		el.removeProperty('title');
			
			
			if (reg.test(val)) {
		
				function request(){	}
				
				function success(response,xml){
					
					t.destroy_messages();				
					
					if (xml.getElement('nouser'))
					{	t.register(el.get('value')) }
					else 
					{	t.login(xml) }			
			
				}
				function failure(){}
		
				var checkuser = new Request(
				{	url : '/utility/checkuser',
					data : { 'email' : el.get('value') },
					method : 'post',			
					onRequest : request,
					onSuccess : success,
					onFailure : failure
				}).send();
		
			} else {
				t.els.login.setProperty('disabled','disabled');
				
				if (!first) {
				el.addClass('highlight');
				el.set('title',el.get('value'));
				el.set('value','Please enter a vaild email address');
				}
			}		
	},
	
	destroy_messages : function(){
		
		if (this.created_els) {
				$each(this.created_els,function(el,i){
					
					el.dispose();
											   
				})
			this.created_els = {}
		}
	},
		
	register : function(email_address){
		this.els.password.removeProperty('disabled');
		var index = email_address.indexOf('@');
		
		var username = email_address.substring(0,index)
	
		this.created_els.username = new Element('div', { 'class' : 'inline_row' }).set('html',
			'<label>Username</label>'
			+'<input type="text" name="username" value="'+username+'" class="text" />').inject(this.els.password.getParent('div'),'before');
		
		this.created_els.welcome_message = new Element('div').set('html',
			'<h5>It looks like this is your first comment with us!</h5>'
		+	'<p>Please choose a username and password to post your comment</p>').inject(this.created_els.username,'before');
		
		this.els.header.set('text','Please register to post your comment');
		this.els.login.set('value','Register & Post your comment');
		//this.els.password.set('name','password');
		//this.els.email.set('name','email');
		
	},
	
	login : function(xml){
		this.els.password.removeProperty('disabled');
		this.created_els.welcome_message = new Element('div').set('html',
			'<h5>Hey '+xml.getElement('name').get('text')+'! Nice to see you again!</h5>'
		+	'<p>Please enter your password to post your comment</p>').inject(this.els.password.getParent('div'),'before');
		
		this.created_els.forgotten_password = new Element('a', { 'class' : 'forgotten_password', 'href' : '/account/forgotten_password' }).set('text','Have you forgotten your password?').inject(this.els.password,'after')
			
		this.els.header.set('text','Hey '+xml.getElement('name').get('text')+'! Ready to post a comment?');
		
		this.els.login.set('value','Login & Post your comment');
		
		//this.els.password.set('name','password');
		//this.els.email.set('name','email');
	}
});