/**
 * A reference to elements used throughout the site
 */
var objElements = {
	$html: null,
	$window: null,
	$body: null,

	init: function() {
		objElements.$html = $('html');
		objElements.$window = $(window);
		objElements.$body = $('body');
	}
};

/**
 * Ctm object literal
 *
 * Keeps the global namespace clean
 */
var Ctm = {
	System: null,
	Polyfills: null
};

/**
 * Ctm.System
 */
Ctm.System = {
	init: function() {
		objElements.init();

		// Setup default AJAX values
		$.ajaxSetup({ type: 'POST' });

		// Setup browser polyfills
		Ctm.Polyfills.init();

		// Setup default Ajax functions
		Ctm.Ajax.init();
	}
};

/**
* Polyfills
*/

Ctm.Polyfills = {
	init: function() {
		Ctm.Polyfills.formValidation();
		Ctm.Polyfills.placeholder();
	},

	// Polyfill for HTML5 Forms
	formValidation: function() {

		if(!Modernizr.input.required || ( $.browser.webkit && parseInt($.browser.version, 12) < 600)) {
		Ctm.Ajax.polyfill = true;
			$('[required=""]').attr('required', 'required');
			$.getScript('/assets/js/jquery.tools.min.js', function(data, textStatus) {
				$('form').validator({
					messageClass: 'form-error',
					position: 'bottom center',
					singleError: true,
					offset: [-5, 20]
				});
			});
		}
	},

	// Polyfill for input placeholder text
	placeholder: function() {
		if(!Modernizr.input.placeholder){
			$('[placeholder]').focus(function() {
				var input = $(this);
				if (input.val() == input.attr('placeholder')) {
					input.val('');
					input.css('color', '');
					input.removeClass('placeholder');
				}
			}).blur(function() {
				var input = $(this);
				if (input.val() === '' || input.val() == input.attr('placeholder')) {
					input.css('color', '#999');
					input.addClass('placeholder');
					input.val(input.attr('placeholder'));
				}
			}).blur();
			$('[placeholder]').parents('form').submit(function() {
				$(this).find('[placeholder]').each(function() {
					var input = $(this);
					if (input.val() == input.attr('placeholder')) {
						input.val('');
					}
				});
			});
		}
	}
};

/**
 * Ctm.Ajax
 *
 * Global Ajax functions.
 */
Ctm.Ajax = {
	init: function() {
		Ctm.Ajax.forms();
	},

	// Default AJAX form submission
	forms: function() {

		// Add asterisk to required form fields
		$('form *[required]')
			.attr('required', 'required')
			.each(function() {
				var fieldName = $(this).attr('name');
				$('label[for="'+fieldName+'"]').append(' <em>*</em>').parent().addClass('required');
			});

		// Add whiteout element for when the form is being submitted
		$('form.ajax').append('<div class="whiteOut hide">&nbsp;</div>');

		// Hijack form submission
		$('form.ajax').live('submit', function(e) {
			$form = $(this);


			e.preventDefault();

			// Don't submit the form if form has been invalidated by polyfill
			if (Ctm.Ajax.polyfill === true && $form.data('validator').checkValidity() === false) {
				return false;
			} else {
				$('.error').remove();

				$.ajax({
					url: 		$(this).attr('action'),
					dataType:	'json',
					data: 		$(this).serialize(),

					beforeSend: function() {
						$form.children('.whiteOut').show().addClass('clearfix');
					},

					success: function(data) {
						$form.replaceWith(data.data);
					},

					error: function() {
						alert('There was an error submitting the form');
					}

				});
			}
		});
	}
};

