var EmailForm = Class.create({
	initialize: function (element) {
		this.element = $(element);
		
		this.options = new Hash({
			link_href: '../_pdf/send.asp',
			form_class: 'email_form',
			input_class: 'text'
		});
		
		this.paragraph = new Element('p');
		this.anchor = new Element('a', {
			href: this.options.get('link_href')
		});
		
		this.form = new Element('form', {
			action: this.options.get('link_href'),
			method: 'post',
			className: this.options.get('form_class')
		});
		
		this.email_input = new Element('input', {
			type: 'text',
			name: 'email',
			value: 'Enter email address',
			className: this.options.get('input_class')
		});

		this.mess_input = new Element('input', {
			type: 'text',
			name: 'mess',
			value: 'Enter short message',
			className: this.options.get('input_class')
		});

		this.observe();
		
		return this;
	},
	
	observe: function () {
		this.anchor.observe('click', function (event) {
			event.stop();
			$$(this.options.get('form_class')).invoke('remove');
			this.form_wrapper = this.form.wrap('div', { style: 'position: relative;' });
			
			this.paragraph.insert({
				after: this.form_wrapper.hide()
			});
			Effect.Appear(this.form_wrapper);
		}.bind(this));
		
		this.email_input.observe('focus', function () {
			if ($F(this) === this.defaultValue) { this.setValue(''); }
		});
		
		this.mess_input.observe('focus', function () {
			if ($F(this) === this.defaultValue) { this.setValue(''); }
		});
		
		$(document.body).observe('keypress', function (e) {
			if (e.keyCode === Event.KEY_ESC) {
				Effect.Fade(this.form_wrapper, {
					afterFinish: function () {
						this.form_wrapper.remove();
					}.bindAsEventListener(this)
				});
			}
		}.bindAsEventListener(this));
		
		this.form.observe('submit', function (event) {
			event.stop();
			if ($F(this.email_input).match(/\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*([,;]\s*\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*)*/)) {
				this.form.request({
					onSuccess: function () {
						Effect.Fade(this.form_wrapper, {
							afterFinish: function () {
								this.form_wrapper.remove();
							}.bindAsEventListener(this)
						});
					}.bindAsEventListener(this)
				});
			} else {
				alert('You must enter a valid email address');
			}
		}.bindAsEventListener(this));
		
		return this;
	},
	
	create: function () {
		this.form.insert(new Element('input', { type: 'hidden', name: 'pdf', value: this.element.readAttribute('href') }));
		this.form.insert(this.email_input);
		this.form.insert(this.mess_input);
		this.form.insert(new Element('input', { type: 'submit', value: 'Send CV' }));
		this.form.insert(new Element('input', { type: 'hidden', name: 'title', value: document.title }));
		
		this.anchor.insert(new Element('strong').update('Email Full CV'));
		this.paragraph.insert(this.anchor);
		
		this.element.up('p').insert({
			after: this.paragraph
		});
		
		return this;
	}
});

document.observe('dom:loaded', function () {
	if (typeof Vx === 'undefined') {
		var pdf_links = $$('.member-details .details_left .profile-pdf a[href$=.pdf]');
		pdf_links.each(function (link, i) {
			if (i === 0) { new EmailForm(link).create(); }
		});
	}
});
