var Subscribe = Class.create({

	initialize: function(fid) {
		this.formid = fid;
		this.email = $('emailform');
		this.errorClass = 'error_message';
		$(this.formid).observe('submit', function() {
			
			if(this.checkEmail(this.email)) {
				this.send();
			} else {
				this.writeError();
			}
			
		}.bind(this));
		
		$('emailform').observe('focus', function() {
			
			$('emailform').value = '';
			
		})
	},
	
	checkEmail: function(email) {
		
		var text = $(email).value;
		
		var pattern = /^[^@]+@[^@]+.[a-z]{2,}$/i;
		var test 	= text.search(pattern);
		if(test == -1) {
			return false;
		} else {
			return true;
		}
		
	},
	
	send: function() {
		
		var url = '/inc/subscribe.php';
		var arguments = {email:$('emailform').value}
		new Ajax.Request(url, {
			method: 'post',
			parameters: arguments,
			onSuccess: function(r) {
				if(r.responseText === '1') {
					closeform();
				} else {
					closeform();
					alert('thanks!')
				}
				
			}

		});
		
	},
	
	writeError: function() {
		alert('your email address is invalid')
	}

})
$('floatingbadge').observe('click', function() {
	var s = new Subscribe('mailform');
	
	var body = document.getElementsByTagName('body')[0];
	$(body).insert({
		top:'<div id="overlay"></div>'
	});
	
	$('mailform').setStyle({
		display: 'block'
	});
	
	$('closebox').observe('click', function() {
		closeform();
	})
});


function closeform() {
	$('mailform').setStyle({
		display: 'none'
	});
	
	$('overlay').remove();
}
