var Register = Class.create(
{
	initialize: function(form)
	{
		this.id					= $(form);
		
		//this.id.select('input').invoke('setAttribute', 'autocomplete', 'off');
		this.id.observe('submit',	 function(e){ this.send(); return false; }.bind(this));
	},
	send: function()
	{
		load(this.id);
		$('login').fade({to:.01});
		error = false;
		
		new Ajax.Request('james/register', {parameters:this.id.serialize(),onComplete:function(){ this.recive(); }.bind(this)});
	},
	recive: function(transport)
	{
		if(error)
		{
			this.id.firstChild.hide();
			this.id.select('[name='+error[0].value+']')[0].focus();
			
			error.each(function(o)
			{
				new Effect.Highlight(this.id.select('[name='+o.value+']')[0], {startcolor:'#ffaabb',duration:1.5});
				this.id.select('[name='+o.value+']')[0].up().previous().update(o.message);
				this.id.select('[name='+o.value+']')[0].up().previous().style.color = '#ff2255';
			}.bind(this));
			
			return;
		}
		else
			this.id.down('.overlay').appear().update('<h2 style="margin-top:90px">Willkommen!</h2>Wir haben dir eine eMail mit einem Bestätigungslink geschickt!');
	}
});

var Login = Class.create(
{
	initialize: function(form)
	{
		this.id					= $(form);
		
		this.id.down('input').focus();
		//this.id.observe('mouseover', function(e){ $FE('login'); }.bind(this));
		this.id.observe('submit',	function(e){ e.stop(); this.send(); return false; }.bind(this));
	},
	send: function()
	{
		this.id.adjacent('p')[0].update('');
		load(this.id);
		error = false;

		new Ajax.Request('james/login', {parameters:this.id.serialize(),onComplete:function(){ this.recive(); }.bind(this)});
	},
	recive: function(t)
	{
		if(error)
		{
			this.id.down('.overlay').remove();
			
			if(error.value!='system')
			{
				this.id.select('[name='+error.value+']')[0].focus();
				new Effect.Highlight(this.id.select('[name='+error.value+']')[0], {startcolor:'#ffaabb',duration:1.5});
			}
			
			this.id.adjacent('p')[0].update(error.message);
	
			return false;
		}

		if($$('#login .overlay')[0])
			$$('#login .overlay')[0].update('<h3 class="cF" style="margin-top:16px">Du wirst weitergeleitet <big class="cM">!</big></h3>').setOpacity(.9);
		window.location.href = t ? t.responseText : '/main';
	}
});

domReady(function(){ new Register('register'); new Login('login'); });