// © 2010 by Diego Massanti // www.massanti.com // diego@massanti.com

ConstantContact = new Class({

	Implements: [Options, Events],
	options: {

	},
	
	initialize: function(options) {
		var me = this;
		this.setOptions(options);
		this.emailField = $('cc_email');
		this.sendButton = $('cc_btn_send');
		if ($('cc_btn_send') != null) {
			this.sendButton.addEvent('click', function(ev){
		
				ev.stop();
				me.addContact(me.emailField.value);
				me.emailField.value = '';
			}, this);
		}
		this.req = new Request({
			'url': window.ccurl,
			'method': 'post'
		});
		
		this.req.addEvent('success', function(responseText, responseXML){
			if (responseText == 'success') {
				alert("Your email has been succesfully added to our databases, thank you!");
				me.emailField.value = '';
			} else {
				alert("There was a problem adding your email, please try again in some minutes.");
				me.emailField.setProperty('disabled', false);
				me.emailField.value = me.origValue;
			}
			
		});
		this.req.addEvent('failure', function(xhr){
			alert("There was a problem adding your email, please try again in some minutes.");
			me.emailField.setProperty('disabled', false);
			me.emailField.value = me.origValue;
		});
	},
	
	addContact: function(email) {
		this.emailField.setProperty('disabled', 'disabled');
		this.req.post({ 'email': email});
		this.origValue = email;
		this.emailField.value = 'Please Wait...';
	}
	
});

window.addEvent('domready', function() {
	document.cc = new ConstantContact();
});
