
$(document).ready(function() {
	
	var ajax_url = function(url) { return url + '&a='; }
	
	var add_class = function(elem, cls) {
		if (!elem) return;
		
		if (!$(elem).hasClass(cls)) {
			$(elem).addClass(cls);
		}
		
		$.each(get_labels(elem), function(index, lbl){add_class(lbl, cls);});
		
	}
	
	var remove_class = function(elem, cls) {
		if (!elem) return;
		
		$(elem).removeClass(cls);
		
		$.each(get_labels(elem), function(index, lbl){remove_class(lbl, cls);});
		
	}
	
	var get_labels = function(elem) {
		
		return elem.id ? $(elem).parents('label').add($('label[for="' + elem.id + '"]')).get() : $(elem).parents('label').get();
		
	}
	
	var initialize_email_form = function() {
		
		var email_form = $('#email_form').get(0);
		if (!email_form) return;
		
		if ($(email_form).find('form').length > 0) $(email_form).find('form').get(0).onsubmit = function() {
			if ($(email_form).find('*.invalid,*.missing').length) {
				$(email_form).find('input[type=submit]').attr('disabled', 'disabled');
				return false;
			} else {
				$(email_form).find('input[type=submit]').removeAttr('disabled');
				return true;
			}
		};
		
		$(email_form).find('p.quit a').first().click(function() { 
			
			$('div.results').children().not('#email_form, #email_form *').stop(true, true);
			$(email_form).remove();
			$('div.results').children().show();
			return false;
		
		});
		
		$(email_form).find('form').first().submit(function() {
			
			$.post(ajax_url(this.action), $(this).serialize(), create_set_email_form($(email_form).get(0)));
			
			return false;
			
		});
		
		$('input[name=from]').keyup(function() {
			if (0 === this.value.length) {
				if (!$(this).hasClass('missing')) add_class(this, 'missing');
				remove_class(this, 'invalid');
			} else {
				remove_class(this, 'missing');
				if (this.value.match(/^(\S+)@(\S+\.\S+\S)$/)) {
					remove_class(this, 'invalid');
				} else if (!$(this).hasClass('invalid')) {
					add_class(this, 'invalid');
				}
			}
			this.form.onsubmit();
		});
		
		$('input[name=from]').each(function(i, input) {
			if (input.title && !input.value) {
				input.value = input.title;
				$(input).addClass('placeholder');
			}
		});
		
		$('input[name=from]').focus(function() {
			if (this.title && this.value && (this.title === this.value)) {
				this.value = '';
				$(this).removeClass('placeholder');
			}
		});
		
		$('textarea[name=message]').keyup(function() {
			if (0 === this.value.length) {
				if (!$(this).hasClass('missing')) add_class(this, 'missing');
				remove_class(this, 'invalid');
			} else {
				remove_class(this, 'missing');
				remove_class(this, 'invalid');
			}
			this.form.onsubmit();
		});
		
		var form_offset = $(email_form).offset();
		
		$(document).scrollTop(form_offset.top);
		$(document).scrollLeft(form_offset.left);
		
		if ($(email_form).find('form').length > 0) $(email_form).find('form').get(0).onsubmit();
		
		$('div.results').children()./*not('h1').*/not('#email_form, #email_form *').hide(2000);
	}
	
	var create_set_email_form = function (elem) {
		
		var setup_email_form = function(data) {
			
			if (1 === $('#email_form').length) { 
				$('#email_form').replaceWith(data);
			} else {
				$('div.results h1').first().after(data);
			}
			
			initialize_email_form();
			
		}
		
		return setup_email_form;
		
	}
	
	$('div.vcard').each(function(i, vcard) {
		
		var contact_button;
		if (1 === $(vcard).find('table.contacts').length) {
			$(vcard).addClass('hide_contacts');
			
			contact_button = document.createElement('a');
			contact_button.onclick = function() {
				$(vcard).toggleClass('hide_contacts').toggleClass('show_contacts');
				return false;
			}
			
		} else {
			
			contact_button = document.createElement('span');
			
		}
		
		contact_button.className = 'contacts_button';
		contact_button.appendChild(document.createTextNode('Contacts'));
		
		$(vcard).find('*.email_url').before(contact_button);
		$(contact_button).after(' ');
		
	});
	
	$('input[name=s]').keyup(function() {
		if (this.value.match(/^(\b\w+\b\W+){5}\b\w+.*/)) this.value = this.value.replace(/^((\b\w+\b\W){5})\W*\b\w+.*/, '$1 ');
	});
	
	$('a.email_url, a.contact_email_url').click(
		function() {
			
			$.get(ajax_url(this.href), create_set_email_form($(this).parents('div.vcard').first()));
			return false;
			
		}
	);
	
	initialize_email_form();
	
});





