var GLOBAL = {};

(function() {
	var thumbs, emailTrigger, emailVisible = false, emailWrapper, imageWrapper, response;
	
	function toggleEmailWrapper(override) {
		if (emailVisible || override === 1) {
			thumbs.show();
			imageWrapper.show();
			emailWrapper.hide();
			emailVisible = false;
		} else {
			thumbs.hide();
			imageWrapper.hide();
			emailWrapper.show();
			emailVisible = true;
		}
	}
	
	function bindFormSubmit() {
		
		$('form').submit(function() {
			$.ajax({
				type 	: 'post',
				url		: 'index.php/home/email',
				data	: $(this).serialize(),
				success : function(data) {
					response = eval('(' + data + ')');
					
					if (response.length) {
						$(response).each(function() {
							$('#' + this).show();
						});
					} else {
						$('.error').hide();
					}
				}, 
				complete: function() {
					if (response.length === 0) {
						toggleEmailWrapper();
					}
				}
			});
			
			return false;
		});
	}
	
	$(document).ready(function() {
		emailTrigger = $('#email');
		emailWrapper = $('#email_wrapper');
		imageWrapper = $('#image_wrapper, #imageNav');
		thumbs = $('#thumbs');
		
		GLOBAL.toggle = toggleEmailWrapper;
		
		BrandAnimation.go(function() {
			NavAnimation.go(function() {
				NavAnimation.select(0);
				emailTrigger.fadeIn();
				
				emailTrigger
					.mouseover(function() {
						$(this).find('img').attr('src', 'app/webroot/img/email_fire.gif');
					})
					.mouseout(function() {
						$(this).find('img').attr('src', 'app/webroot/img/email.gif');
					})
					.click(toggleEmailWrapper);			
			});
		});
	
		bindFormSubmit();
	});
})();