var App = (function($){
	var that = {};
	
	that.init_validation = function(){
		var rules = {
			'User[first_name]': 'required',
			'User[last_name]': 'required',
			'Address[address1]': 'required',
			'Address[city]': 'required',
			'Address[state]': 'required',
			'Address[zip5]': {
				required: true,
				digits: true
			},
			'User[email]': {
				required: true,
				email: true
			}
		};
		
		$('#account-create-form').validate({
			rules: rules,
			submitHandler: function(form){
				// track the submit
				_gaq.push(['_trackEvent', 'submit', 'success', 'create_user']);
				_gaq.push(['_trackPageview', '/start_checkout']);
				form.submit();
			},
			invalidHandler: function(form, validator){
				// track the error
				_gaq.push(['_trackEvent', 'submit', 'error', 'create_user']);
				alert('Please correct the red fields.');
				$('.disclaimer').addClass('error');
			},
			errorPlacement: function(error, element){
				element.addClass('error').prev('label').addClass('invalid');
			},
			debug: true
		});
	};
	
	that.init_links = function() {
		$('#footer a').click(function(){
			var href = $(this).attr('href');
			window.open(href, 'pop', 'menubar=0,location=0,toolbar=0,width=980,height=600,scrollbars=1');
			return false;
		});
	};
	
	that.init_social_tracking = function() {
		$('.social').click(function(){
			_gaq.push(['_trackEvent', 'social', $(this).attr('id').replace(/\-button/i, ''), 'social']);
		});
	};
	
	return that;
}(jQuery));

jQuery(function(){
	App.init_validation();
	App.init_links();
	App.init_social_tracking();
});