var w = this, BA = {}, clicky_custom = {};

(BA.FormValidation = function () {
	var required = {
			'contact': ['Name', 'Email'],
			'quick-contact': ['Name', 'Email'],
			'detailed-contact': ['Name', 'Email', 'Phone', 'Event', 'Description']
		},
		alerts = {
			Name: 'Please enter your name.\nThis will help us know who to contact.',
			Email: 'Please enter your email address.\nThis will help us get in touch with you.',
			Phone: 'Please enter your phone number.\nThis will help us get in touch with you.',
			Event: 'Please enter the event date.\nThis will help us determine the statute of limitations.',
			Description: 'Please enter a description.\nThis will help us understand your claim.'
		};
	
	function log(path, g) {
		return; // temp
		if (w.pageTracker && g) {
			w.pageTracker._trackPageview(path);
		}
		if (w.clicky) {
			w.clicky.log(path);
		}
	}
	
	function alog(path) {
		w.setTimeout(
			function () {
				log(path);
			}, 1);
	}
	
	function fieldCall(field) {
		field = w.$(field);
		field.value = field.val();
		field.each(
			function () {
				field.form = w.$(this.form);
				if (this.defaultValue) {
					field.defaultValue = this.defaultValue;
				}
			}
		);
		field.clearDefaults = function () {
			if (this.value === this.defaultValue) {
				this.val('');
			}
		};
		
		return field;
	}
	
	
	function validate() {		
		var form = w.$(this), id = form.attr('id'),
			field, invalid = [], name = '', isHandlerActive = false, formClone, i;
		
	
				
		function clearDefaults() {
			field = fieldCall(this);
			field.clearDefaults();
		}
		
		log(w.location.pathname + '#' + id + '/submit', true);
		
		for (i = 0; i < required[id].length; i += 1) {
			field = fieldCall(form.find('[name=' + required[id][i] + ']'));
			
			if (!field.value || field.value === field.defaultValue) {
				invalid[invalid.length] = required[id][i];
				field.addClass('required');
			}
		}
		
		if (invalid.length) {
			log(w.location.pathname + '#' + id + '/submit/invalid');
			w.$('#alert').remove();
			form.prepend('<p id="alert" class="alert">Missing Fields: ' + invalid.join(', ') + '.</p>');
			
			w.alert(alerts[invalid[0]]);
			form.find('[name=' + invalid[0] + ']').focus();
			
			return false;
		}
		else {
			form.find('input:text, textarea').each(clearDefaults);	
			
			form.each(
				function () {	
									
					w.$(this).attr('target', '_self');
					//w.$(this).attr('action', '/contact.cgi');
					w.$(this).attr('action', 'http://www.beasleyallen.com/listener.cfm');
				}
				
				
			);
			
			log(w.location.pathname + '#' + id + '/submit/valid', true);
			
			name = form.find('[name=Name]').val();
			name = name.replace(/[^0-9A-Z \-]/gi, '').replace(/ /g, '_');			
			clicky_custom.session = { username: name, group: 'contacts' };
				
			return true;
		}
	}
	
	function fieldFocus() {
		var field = fieldCall(this);
		field.clearDefaults();
		alog(w.location.pathname + '#' + field.form.attr('id') + '/' + field.attr('name'));
	}
	
	function fieldBlur() {
		var field = fieldCall(this);
		
		if (field.value) {
			field.addClass('valid');
			alog(w.location.pathname + '#' + field.form.attr('id') + '/' + field.attr('name') + '/valid');
		}
		else {
			field.removeClass('valid').val(field.defaultValue);
			alog(w.location.pathname + '#' + field.form.attr('id') + '/' + field.attr('name') + '/invalid');
		}
	}
	
	function selectForms() {
		var forms = '', i;
		
		for (i in required) {
			if (typeof required[i] !== 'function') {
				if (forms) {
					forms += ', ';
				}
				forms += '#' + i;
			}
		}
		
		return w.$(forms);
	}
	
	function init() {
		var forms = selectForms();
		
		forms.submit(validate).find('input:text, select, textarea').blur(fieldBlur).focus(fieldFocus);
	}
	
	return {init: init};
}());

(BA.QuickContactNotice = function () {
	var form;
	
	function displayNotice(notice, error) {
		form.prepend('<p id="alert"' + (error ? ' class="alert"' : '') + '>' + notice + '<\/p');
	}
	
	function init() {
		if (w.location.search === '?alert') {
			form = w.$('#quick-contact');
			
			if (w.location.pathname === '/thank-you/') {
				displayNotice('Thank you!');
			}
			else {
				displayNotice('Missing fields!', true);
			}
		}
	}
	
	return {init: init};
}());

(BA.PersonalDetails = function () {
	var relation, details;
	
	function relationshipCheck() {
		if (relation.val() === 'Self') {
			details.slideUp('normal',
				function () {
					details.find('input, select').each(
						function () {
							w.$(this).removeClass('valid').val(this.defaultValue ? this.defaultValue : '');
						}
					);
				}
			);
			
		}
		else {
			details.slideDown();
		}
	}
	
	function init() {
		relation = w.$('#contact-relationship');
		details = w.$('#contact-full-name, #contact-gender, #contact-dob').parent().parent();
		
		details.hide();
		relation.change(relationshipCheck);
	}
	
	return {init: init};
}());

w.$(w.document).ready(
	function () {
		BA.FormValidation.init();
		BA.QuickContactNotice.init();
		BA.PersonalDetails.init();
	}
);
