/**
 * 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");
	}
}

var Ctm = {
	Controller: null,
	System: null,
	Home: null
}

/**
 * Run every page through here and feed it only the JS it needs.
 */
Ctm.Controller = {
	init: function() {
		//Always fire SY.System
		Ctm.System.init();

		// Fire off JS based on URI segment
		var arrUri = document.location.pathname.split('/');

		switch (arrUri[1]) {
		case "":
		default:
			break;
		}

		if (arrUri[1] == "") {
			Ctm.Home.init();
		}

		if (arrUri[1] == "contact") {
			//Ctm.Contact.init();
		}
	}
}

/**
 * Homepage JS (create more these for additional pages.
 */
Ctm.Home = {
	init: function() {
		// Homepage slideshow
		$('#banner').innerfade({
			speed: 1000,
			timeout: 4000,
			containerheight: '320px'
		});
	},
}

/**
 * This is for global JS stuff. That is, items that get used
 * throughout the site (or at least on more than one page.
 */

/**
 * 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();

		// Footer
		Ctm.System.alignFooter();
		Ctm.System.stickyFooterFix();

		// Fancybox
		Ctm.System.initFancybox();
	},

	alignFooter: function() {
		width = $('#footNav').width();
		$('#footNav').css('marginLeft', -width/2);
	},

	initFancybox: function() {
		$('a.lightbox').fancybox();
	},

	stickyFooterFix: function() {
		if (objElements.$window.height() > objElements.$body.height() ) {
			objElements.$html.css('height', '100%');
		}
	}};

/**
 * Ctm.Ajax
 *
 * Global Ajax functions.
 */

/**
 * 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();

		// Footer
		Ctm.System.alignFooter();
		Ctm.System.stickyFooterFix();

		// Fancybox
		Ctm.System.initFancybox();
	},

	simpleSlider: function($container) {
		$container.addClass('simpleSlider');
		$('.simpleSlider div:gt(0)').hide();
		setInterval(function(){$('.simpleSlider div:first-child').fadeOut(1000).next('div').fadeIn(1000).end().appendTo('.simpleSlider');}, 5000);
	},

	alignFooter: function() {
		width = $('#footNav').width();
		$('#footNav').css('marginLeft', -width/2);
	},

	initFancybox: function() {
		$('a.lightbox').fancybox();
	},

	stickyFooterFix: function() {
		if (objElements.$window.height() > objElements.$body.height() ) {
			objElements.$html.css('height', '100%');
		}
	}};

/**
 * 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').parent().prev().addClass('required').children().append(' <em>*</em>');

		// 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);

			// Don't submit the form if form has been invalidated by polyfill
			if (!Modernizr.input.required && $form.data('validator') !== undefined && $form.data('validator').checkValidity() === false) {
				return false;
			}
			$('.error').remove();
			e.preventDefault();
			
			/*
			$.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');
				}
			});
			*/
		});
	}
};


Ctm.Polyfills = {
	init: function() {
		Ctm.Polyfills.formValidation();
		Ctm.Polyfills.placeholder();
	},

	// Polyfill for HTML5 Forms
	formValidation: function() {
		if(!Modernizr.input.required) {
			$.getScript('//cdn.jquerytools.org/1.2.6/form/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('');
					}
				});
			});
		}
	}
};
/**
 * On Ready Event
 * Simple: Fire the appropriate init methods based on the url.
 * DO NOT STUFF JS CODE HERE. PUT IT WHERE IT BELONGS
 */

$(document).ready(function() {
	Ctm.System.init();
	Ctm.Controller.init();
});

