jQuery(function ($) {
	var contact = {
		message: null,
		init: function () {
			$('a.contact').click(function (e) {
				e.preventDefault();

				// load the contact form using ajax
				$.get("/data/contact.php", function(data){
					// create a modal dialog with the data
					$(data).modal({
						maxWidth: 500,
						minHeight: 600,
						position: ["15%",],
						overlayId: 'contact-overlay',
						containerId: 'contact-container',
						onOpen: contact.open,
						onShow: contact.show,
						onClose: contact.close
					});
				});
			});
		},
		open: function (dialog) {

			var title = $('#contact-container .contact-title').html();
			$('#contact-container .contact-title').html('Loading...');
			dialog.overlay.fadeIn(200, function () {
				dialog.container.fadeIn(200, function () {
					dialog.data.fadeIn(200, function () {
						$('#contact-container .contact-title').html(title);
						$('#contact-container form').fadeIn(200, function () {
							$('#contact-container #name').focus();
						});
					});
				});
			});
		},
		show: function (dialog) {
			$('#contact-container .submit').click(function (e) {
				e.preventDefault();
				// validate form
				if (contact.validate()) {
					var msg = $('#contact-container .contact-message');
					msg.fadeOut(function () {
						msg.removeClass('contact-error').empty();
					});
					$('#contact-container .contact-title').html('Sending...');
					$('#contact-container form').fadeOut(200);
					$('#contact-container .contact-content').animate({
						height: '80px'
					}, function () {
						$('#contact-container .contact-loading').fadeIn(200, function () {
							$.ajax({
								url: '/data/contact.php',
								data: $('#contact-container form').serialize() + '&action=send',
								type: 'post',
								cache: false,
								dataType: 'html',
								success: function (data) {
									$('#contact-container .contact-loading').fadeOut(200, function () {
										$('#contact-container .contact-title').html('Thank you!');
										msg.html(data).fadeIn(200);
									});
								},
								error: contact.error
							});
						});
					});
				}
				else {
					$('#contact-container .contact-title').html('Oops!');
					if ($('#contact-container .contact-message:visible').length > 0) {
						var msg = $('#contact-container .contact-message div');
						msg.fadeOut(200, function () {
							msg.empty();
							contact.showError();
							msg.fadeIn(200);
						});
					}
					else {
						$('#contact-container .contact-message').animate({
							height: '30px'
						}, contact.showError);
					}
					
				}
			});
		},
		close: function (dialog) {
			$('#contact-container .contact-message').fadeOut();
			$('#contact-container .contact-title').html('Goodbye...');
			$('#contact-container form').fadeOut(200, function () {
				dialog.data.fadeOut(200, function () {
					dialog.container.fadeOut(200, function () {
						dialog.overlay.fadeOut(200, function () {
							$.modal.close();
						});
					});
				});
			});
		},
		error: function (xhr) {
			alert(xhr.statusText);
		},
		validate: function () {
			contact.message = '';
			has_errors = false;
			if (!$('#contact-container #name').val()) {
				has_errors = true;
			}
			if (!$('#contact-container #telephone').val()) {
				has_errors = true;
			}
			if(has_errors) {
				contact.message += '<p><strong class="error">Please make sure you\'ve filled in all of the information on this form. We need these details so that we can call you at a time of your convenience.</strong></p>';
			}
			if (has_errors) {
				return false;
			}
			else {
				return true;
			}
		},
		showError: function () {
			$('#contact-container .contact-message')
				.html($('<div class="contact-error"></div>').append(contact.message))
				.fadeIn(200);
		}
	};

	contact.init();

});

$(document).ready( function() {
	$('a.virtual-house-button').click( function (){
		$('div#virtual-house').flash({
			swf: '/virtualhouse/virtual-house.swf',
			width: 825,
			height: 780
		});
		$('div#virtual-house').modal({
			overlayId: 'virtual-house-overlay',
			overlayClose: true
		});
		return false;
	});
});


















	
