var Contact = {
	_sending:		false,
	_cache:		null,
	
	initialize: function () {
		this.Verify.All = this.Verify.All.bind(this);
		
		this._cache = {
			btnSend: {
				click: this.Send.bind(this)
			},
			name: {
				blur: this.Verify.Name
			},
			email: {
				blur: this.Verify.Email
			},
			comments: {
				blur: this.Verify.Comments
			}
		};
		
		Event.observe('name', 'blur', this._cache.name.blur);
		Event.observe('email', 'blur', this._cache.email.blur);
		Event.observe('comments', 'blur', this._cache.comments.blur);
		Event.observe('btnSend', 'click', this._cache.btnSend.click);
		
		Event.observe(window, 'unload', this.deinitialize.bind(this));
	},
	
	deinitialize: function () {
		Event.stopObserving('name', 'blur', this._cache.name.blur);
		Event.stopObserving('email', 'blur', this._cache.email.blur);
		Event.stopObserving('comments', 'blur', this._cache.comments.blur);
		Event.stopObserving('btnSend', 'click', this._cache.btnSend.click);

		this._cache = null;
	},
	
	Send: function () {
		if (this._sending || !this.Verify.All())
			return;
		
		this._sending = true;
		this._updateForm();
		
		this._transmit();
	},
	
	Verify: {
		All: function () {
			var fail = !this.Verify.Name();
			fail = !this.Verify.Email() || fail;
			fail = !this.Verify.Comments() || fail;
			return !fail;
		},
		
		Name: function () {
			$('name').value = $('name').value.trim();
			var name_failed = ($('name').value.length == 0);
			$('v_name').style.color = (name_failed ? 'red' : '');
			return !name_failed;
		},
		
		Email: function () {
			$('email').value = $('email').value.trim();
			var email_failed = (!/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/.test($('email').value));
			$('v_email').style.color = (email_failed ? 'red' : '');
			return !email_failed;
		},
		
		Comments: function () {
			$('comments').value = $('comments').value.trim();
			var comments_failed = ($('comments').value.length == 0);
			$('v_comments').style.color = (comments_failed ? 'red' : '');
			return !comments_failed;
		}
	},
	
	_transmit: function () {
		new Ajax.Request('/ajax/contact/', {
				method: 'post',
				parameters: {
					name:		$('name').value,
					email:		$('email').value,
					phone:		$('phone').value,
					address:	$('address').value,
					city:		$('city').value,
					state:		$('state').value,
					comments:	$('comments').value
				},
				requestHeaders: {Accept: 'application/json'},
				onFailure: function () {
					alert('There was an issue while processing your request.\n\nPlease try again later.');
					this._sending = false;
					this._updateForm();
				}.bind(this),
				onSuccess: function (transport) {
					var requestFailed = false;
					try {
						json = transport.responseText.evalJSON();
					} catch (e) {
						requestFailed = true;
					}
					
					if (requestFailed) {
						alert('There was an issue while processing your request.\n\nPlease try again later.');
						this._sending = false;
						this._updateForm();
					} else {
						if (json.success) {
							this._success();
						} else {
							this.Verify.All();
							if (json.message) {
								alert(json.message);
							} else {
								alert('There was an issue while processing your request.\n\nPlease try again later.');
							}
							this._sending = false;
							this._updateForm();
						}
					}
				}.bind(this)
			}
		);
	},
	
	_updateForm: function () {
		$('name').disabled = this._sending;
		$('email').disabled = this._sending;
		$('phone').disabled = this._sending;
		$('address').disabled = this._sending;
		$('city').disabled = this._sending;
		$('state').disabled = this._sending;
		$('comments').disabled = this._sending;
		$('btnSend').writeAttribute('src', '/images/buttons/' + (this._sending ? 'contactus_send_d.gif' : 'contactus_send.gif'));
		$('btnSend').writeAttribute('srcorg', '/images/buttons/' + (this._sending ? 'contactus_send_d.gif' : 'contactus_send.gif'));
		$('btnSend').writeAttribute('srcover', '/images/buttons/' + (this._sending ? 'contactus_send_d-h.gif' : 'contactus_send-h.gif'));
		$('btnSend').setStyle({cursor:(this._sending ? '' : 'pointer')});
	},
	
	_success: function () {
		$('contactus-container').setStyle({height: $('contactus-body-content').getHeight()+'px'});
		new Effect.Fade('contactus-body-content');
		new Effect.Appear('contactus-body-thankyou', {queue: 'end'});
	}
}
Event.observe(window, 'load', Contact.initialize.bind(Contact));

// Google Maps //
function initializeGoogleMaps() {
	if (GBrowserIsCompatible()) {
		var map = new GMap2($('map_canvas'));
		var center = new GLatLng(40.511674, -80.104784);
		map.setCenter(center, 16);
		map.setMapType(G_NORMAL_MAP);
		map.addOverlay(new GMarker(center));
		map.addControl(new GSmallMapControl());
        map.addControl(new GMapTypeControl());
	}
}
Event.observe(window, 'load', initializeGoogleMaps);
Event.observe(window, 'unload', GUnload);