var EmailCollector = new Class({
	Implements: [Options],

	options : {
		description: "Please enter your email address for further notifications.",
		product: "general"
	},

	element : null,
	overlay : null,
	input : null,


	initialize: function(options) {
		this.setOptions(options);

		var blackout = new Element('div', {
			id : 'ECblackout',
			styles : {
					'background' : 'transparent',
					'position': 'fixed',
					'width': '100%',
					'top': '0px',
					'z-index' : '200',
					'opacity' : '0',
			}
		});

		var el = new Element('div', {
			id : 'ECcontent',
			styles : {
				'position': 'relative',
				'top' : '200px',
				'background': 'white',
				'width' : '400px',
				'margin' : 'auto',
				'padding' : '20px',
				'overflow' : 'visible',
				'z-index' : '201',
				'border' : '2px solid #333',
				'-webkit-box-shadow' : '0px 0px 50px #333',
				'-moz-box-shadow' : '10px 10px 50px #333',
				'-webkit-border-radius' : '4px',
				'-moz-border-radius' : '4px',
				'text-align' : 'center'
			}
		});
		el.set('text', this.options.description);

		var inp = new Element('input', {
			'name' : 'email', 
			'styles' : {
				'width' : '350px',
				'margin' : 'auto',
				'margin-top' : '10px',
				'margin-bottom' : '5px',
				'padding' : '5px',
				'font-size' : '1.1em'
			}
		});

		var submit = new Element('a', { 'text' : 'Submit', 'href' : '#' });
		var cancel = new Element('a', { 'text' : 'No Thanks', 'href' : '#' });

		submit.setStyle('margin-right', '10px');

		el.appendChild(inp);
		el.appendChild(submit);
		el.appendChild(cancel);

		el.appendChild(new Element('span', {
			'text' : "I take your grant of this address seriously and will not abuse or distribute it in any way.",
			'styles' : {
				'font-size': '.9em',
				'color' :  '#999',
				'display' : 'block',
				'margin-top' : '5px'
			}
		}));

		blackout.appendChild(el);
		blackout.inject(document.body, 'top');

		this.overlay = blackout;
		this.element = el;
		this.input = inp;

		this.hide();

		$(document.body).addEvent('click', function(e) {
			var is_content = (e.target.id == 'ECcontent' || e.target.getParent('#ECcontent'));
			if(blackout.get('opacity') == 1 && !is_content) {
				this.hide();
			}
		}.bind(this));

		window.addEvent('keydown',function(e) { 
			if(e.key == 'esc') { 
				this.hide();
			} 
			
			if(e.key == 'enter' || e.key == 'return') { 
				this.submit();
			}

		}.bind(this)); 

		submit.addEvent('click', function(e) { this.submit(); e.stop(); }.bind(this));
		cancel.addEvent('click', function(e) { 
			Cookie.write('email_collected', 0, {duration: 3650});
			this.hide(); 
			e.stop(); 
		}.bind(this));

	},

	show : function() {
		if(!this.check_cookie()) {
			this.overlay.fade('in');
			this.input.focus.delay(1000, this.input);
		}
	},

	hide : function() {
		this.overlay.fade('out');
		this.input.blur(); 
	},

	submit : function() {
		var email = this.input.value;
		if(!email.length) {
			this.element.setStyle('border' , '2px solid #a33');
			return;
		}

		new Request.JSON({
			'url' : 'email_collector.php',
			'onSuccess' : function() {
				this.element.setStyle('border' , '2px solid #3a3');
				this.element.set('text', 'Thanks!');
				this.hide.delay(1000, this);
				Cookie.write('email_collected', 1, {duration: 3650});
			}.bind(this),
			'onFailure' : function() {
				this.element.setStyle('border' , '2px solid #a33');
				this.element.highlight('#a33');
			}.bind(this)
		}).post({email: email, product: this.options.product});
	},

	check_cookie : function() {
		var is_set = Cookie.read('email_collected');
		return (is_set !== null);
	}
});

