Signup = Class.create({
	initialize: function() {
		this.wrap = $('contact_wrap');
		this.iframe = $('contact_frame').contentWindow.document;
		this.isError = false;
		this.email = this.iframe.getElementById('contact_email');
		this._form = this.iframe.getElementById('contact_form');
		this.submit = $('contact_submit');
		this.errorPopup = $('contact_error');
		
		this.email.observe('focus', this.focus.bind(this));
		this.email.observe('blur', this.blur.bind(this));
		this.submit.observe('click', this.validate.bindAsEventListener(this))
		this.email.observe('keypress', this.handleKeypress.bind(this));
	},
	
	focus: function() {
		if(this.isError) {
			this.isError = false;
			this.errorPopup.fade({duration:0.2});
		}
		if(this.email.hasClassName('grayed')) {
			this.email.value = '';
			this.email.removeClassName('grayed');
		}							
	},
	
	blur: function() {
		if(this.email.value == '') {
			this.email.value = 'Enter Your Email Address';
			this.email.addClassName('grayed');
		}									
	},
	
	handleKeypress: function(e) {
		if(e.keyCode == 13) e.stop();
	},
	
	validate: function(e) {
		e.stop();
		this.email.value.search(/^\s*[\w\-\+_]+(\.[\w\-\+_]+)*\@[\w\-\+_]+\.[\w\-\+_]+(\.[\w\-\+_]+)*\s*$/) != -1 ? this.submitForm() : this.showError();
	},
	
	submitForm: function() {
		this.wrap.addClassName('loading');
		this.submit.hide();
		this.email.style.opacity = 0.5;
		setTimeout(this.showConfirm.bind(this), 2000);
	},
	
	showError: function() {
		if(!this.isError) {
			this.isError = true;
			this.errorPopup.appear({duration:0.3});
		}
	},
	
	showConfirm: function() {
		this._form.submit();
		$('contact_frame').style.display = "none";
		this.wrap.removeClassName('loading');
		$('contact_thankyou').appear({ duration:0.2 });			
	}				
});
