ah.contact = {
	init: function(){
		var self = this;
		
		// element references
		self.contactBubble = $('#intro #contactBubble');
		self.stalkBubble = $('#intro #stalkBubble');
		
		// variables
		self.contactBubbleCSS = {
			'cssTop': 212,
			'cssLeft': -1
		}
		self.contactToggled = 0;
		
		self.stalkBubbleCSS = {
			'cssTop':212,
			'cssLeft':-1
		}
		self.stalkToggled = 0;
		self.formContent = '';
		
		// setup
		$("#contactForm").validate({
			submitHandler: function(form) {
				ah.contact.submitForm(form);
			}
		});
				
		// event handlers
		$('.callToAction .contact a.btn').click(function(e){
			e.preventDefault();
			self.showHide_contact(e);
		});
		$('.callToAction .stalk a.btn').click(function(e){
			e.preventDefault();
			self.showHide_stalk(e);
		});
	},
	showHide_contact: function(e){
		var self = this;
		
		//	Hide contact form if it's open
		if(self.stalkToggled == 1){
			self.showHide_stalk();
		}
		//	The animations
		if(self.contactToggled == 0){
			self.contactBubble.stop().css({
				'top': 			(self.contactBubbleCSS.cssTop-20) + 'px',
				'left': 		(self.contactBubbleCSS.cssLeft+20) + 'px',
				'display': 		'block',
				'opacity': 		0
				}).animate({
					'opacity': 	1,
					'top': 		self.contactBubbleCSS.cssTop + 'px',
					'left': 	self.contactBubbleCSS.cssLeft + 'px'
				},250, 'swing', function(){
					self.contactToggled = 1;
				});
		} else {
			self.contactBubble.stop().animate({
					'opacity': 		0,
					'top': 			(self.contactBubbleCSS.cssTop-20) + 'px',
					'left': 		(self.contactBubbleCSS.cssLeft+20) + 'px'
				},250, 'swing',function(){
					self.contactBubble.hide();
					self.contactToggled = 0;
				}
			);
		}
	},
	showHide_stalk: function(e){
		var self = this;
		
		//	Hide contact form if it's open
		if(self.contactToggled == 1){
			self.showHide_contact();
		}
		
		//	The animations
		if(self.stalkToggled == 0){
			self.stalkBubble.stop().css({
				'top': 			(self.stalkBubbleCSS.cssTop - 20) + 'px',
				'left': 		(self.stalkBubbleCSS.cssLeft - 20) + 'px',
				'display': 		'block',
				'opacity': 		0
				}).animate({
					'opacity': 	1,
					'top': 		self.stalkBubbleCSS.cssTop + 'px',
					'left': 	self.stalkBubbleCSS.cssLeft + 'px'
				},250, 'swing', function(){
					self.stalkToggled = 1;
				});
		} else {
			self.stalkBubble.stop().animate({
					'opacity': 		0,
					'top': 			(self.stalkBubbleCSS.cssTop-20) + 'px',
					'left': 		(self.stalkBubbleCSS.cssLeft-20) + 'px'
				},250, 'swing',function(){
					self.stalkBubble.hide();
					self.stalkToggled = 0;
				}
			);
		}
	},
	submitForm: function(form) {
		var self = this;
		var formData = {
			'submit'	: true,
			'name' 		: $('form div.name input').val(),
			'email'		: $('form div.email input').val(),
			'message'	: $('form div.message textarea').val()
		}
		$.ajax({
			type: "POST",
			url: '/system/application/views/themes/austinhappel/_includes/mailer.php',
			data: formData,
			success: function(data) {
				//	save the current form content.
				self.formContent = $('#contactForm').html();
				
				//	fade out, swap the content and fade in. 
				$('#contactForm .formContent').fadeOut(200, function(){
					$(this).html('<h1>Your message has been sent!</h1><a href="" id="formSendAnother">Send another</a>');
				}).fadeIn(200, function(){
					$('a#formSendAnother').click(function(e){
						e.preventDefault();
						$('#contactForm .formContent').fadeOut(200, function(){
							$(this).html(self.formContent);
						}).fadeIn(200);
					});
				});
			}
		});
	}
}

