var heMain = (function heMain() {
	
	"use strict";
	
	return {
		
		list: {
			
			init: function init() {
				
				var that = this;
				
				if ($.browser.msie && $.browser.version < 9 && $('span.form-result').length > 0) {
					setTimeout(function () {	
						$('span.form-result').animate({
							opacity: 0,
							height: 0,
							paddingTop: 0,
							paddingBottom: 0,
							marginTop: 0,
							LineHeight: 0
						}, 3000, function () {
							$(this).hide();
						});
					}, 5000);
				}
				
				$.each($('div.tally-input'), function () {
					
					var $curr, $checkbox;
					
					$curr = $(this);
					$checkbox = $('input:checkbox', $curr.parent());
					
					$curr.bind('click', function () {
						$checkbox.attr('checked', !$checkbox.is(':checked'));
						$('input:checkbox', $curr.parent()).trigger('click');
						$checkbox.attr('checked', !$checkbox.is(':checked'));
					});
				});
								
				$.each($('ul.tally-list input:checkbox'), function () {
										
					var $curr, $currParent, $nextParent;
					
					$curr = $(this);
					$currParent = $curr.parent();
					$nextParent = $currParent.next();
					
					if ($curr.is(':checked')) {
						$('div.tally-input', $currParent).removeClass('glowing').addClass('checked');
						$currParent.css({ 'height': $('label', $currParent).outerHeight(true) + 'px' });
						$nextParent.css({ 'height': $('label', $nextParent).outerHeight(true) + 'px' });
					}
					
					$nextParent.addClass('transition');	// add 'transition' here to animate the whole list always (even on page load)
					$curr.bind('click', that.onClick);
				});				
			},
			
			onClick: function onClick(e) {
								
				var $curr, $currParent, $currCheck, $next, $nextParent, $nextLabel;
				
				$curr = $(e.target);
				$currParent = $curr.parent();
				$currCheck = $('div.tally-input', $currParent);
				
				$nextParent = $currParent.next();
				$nextLabel = $('label', $nextParent);
				$next = $('input:checkbox', $nextParent);
				
				// $nextParent.addClass('transition');	// add 'transition' here to animate the whole list only on click (content may flash on page load)
				$('label', $currParent).removeClass('error');
				
				if ($next.is(':checked')) {	
					e.preventDefault();
				} else {	
					if ($curr.is(':checked')) {
						$currCheck.removeClass('glowing').addClass('checked');
						$nextLabel.removeClass('error');
						$nextParent.css({ 'height': $nextLabel.outerHeight(true) + 'px' });
					} else {
						$currCheck.removeClass('checked').addClass('glowing');
						$nextParent.css({ 'height': '0', 'margin': '0' });
					}
				}	
			}
		},
		
		beta: {
			
			init: function init() {
								
				$('#email').bind('focus', function () {
					$(this).removeClass('error');
					if ($(this).val() === 'Be invited to the beta, enter your email here') {
						$(this).val('');
					}					
				});
			}
		}
	};
	
}());
