/**
*	Handles the form submitting stuff
*
**/

window.addEvent('domready', function()
{
	new EmailSignUp();
	new ContactUs();
});

var EmailSignUp = new Class(
{
	initialize: function()
	{
		this.form = $('emailsignup');
		if (this.form == null) return;
		
		this.form.addEvent('submit', this.onSubmit.bind(this));
		this.form.action = TEMPLATE_URL + '/form_handler.php';
		
		this.slider = new Fx.Slide(this.form);
		
		var email = $(this.form.email);
		email.value = 'your email';
		email.setStyle('color', '#666666');
		
		email.addEvent('focus', function()
		{ 
			if (email.value == 'your email' || email.value == 'thank you') email.value = ''; 
			email.setStyle('color', '#000000');
		});
		email.addEvent('blur', function()
		{ 
			if (email.value == '') 
			{
				email.value = 'your email'; 
				email.setStyle('color', '#666666');
			}
		});
	},
	
	onSubmit: function()
	{
		if (!this.validate(this.form.email.value))
		{
			this.form.email.set('tween', {transition:Fx.Transitions.Cubic.easeOut, duration:700});
			this.form.email.tween('background-color', ['#FF0000', '#FFFFFF']);
			return false;
		}
		
		this.slider.slideOut();
		(function()
		{
			this.form.send();
			this.form.email.value = 'thank you';	
			this.form.email.setStyle('color', '#666666');
			this.slider.slideIn();
		}).delay(1000, this);
		
		return false;
	},
	
	validate: function(s)
	{
		try{
		var regex = new RegExp('^(("[\\w-\\s]+")|([\\w-]+(?:\\.[\\w-]+)*)|("[\\w-\\s]+")([\\w-]+(?:\\.[\\w-]+)*))(@((?:[\\w-]+\\.)*\\w[\\w-]{0,66})\\.([a-z]{2,6}(?:\\.[a-z]{2})?)$)|(@\\[?((25[0-5]\\.|2[0-4][0-9]\\.|1[0-9]{2}\\.|[0-9]{1,2}\\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\\]?$)','i');
		return regex.test(s);
		}
		catch(e){ return false;}
	}
});

var ContactUs = new Class(
{
	initialize: function()
	{
		this.form = $('contactForm');
		if (this.form == null) return;
		
		this.tweenOpts = {transition:Fx.Transitions.Cubic.easeOut, duration:500};
		
		this.form.addEvent('submit', this.onSubmit.bind(this));
		this.form.action = TEMPLATE_URL + '/form_handler.php';
		this.form.set('tween', this.tweenOpts);
		this.form.set('send', {onComplete: this.onDone.bind(this), evalScripts: false});
		
		this.progress = new Element('img',{'src':TEMPLATE_URL + '/images/loader1.gif', style:'top:200px;left:70px;position:absolute;visibility:hidden',width:32, height:32});
		this.progress.set('tween', this.tweenOpts);
		$('content-area').grab(this.progress);
		
		this.msg = new Element('div',{'class':'form_msg', style:'position:absolute;top:300px;left:0;width:300px;visibility:hidden'});
		this.msg.set('tween', this.tweenOpts);
		this.form.getParent().grab(this.msg);
	},
	
	onSubmit: function()
	{
		this.fade(this.form, 0);
		
		if (this.validate())
		{
			this.fade(this.progress, 1);
			this.form.send();
			this.fade.delay(1000, this, [this.progress, 0]);
			this.fade.delay(1000, this, [this.msg, 1]);
			this.form.reset();
		}
		else
		{
			this.fade.delay(400, this, [this.msg, 1]);
		}
		
		this.fade.delay(2700, this, [this.msg, 0]);
		this.fade.delay(2700, this, [this.form, 1]);

		return false;
	},
	
	fade: function(el, val)
	{
		return el.get('tween').start('opacity', val);
	},
	
	validate: function()
	{
		//this.msg.set('text','You did something wrong!');
		//return false;
		
		this.msg.set('text', 'Thank you.');
		return true;
	}, 
	
	onDone: function(txt)
	{
		//console.log('done');
	}
});