(function ($) {
	$(document).ready(function () {

		function isMobileSafari() {
			return (
				(navigator.platform.indexOf("iPhone") != -1) ||
					(navigator.platform.indexOf("iPod") != -1)
				);
		}

		function isIe7() {
			return $.browser.msie && parseInt($.browser.version, 10) == 7;
		}

		function isIe8() {
			return $.browser.msie && parseInt($.browser.version, 10) == 8;
		}

		function isIe9() {
			return $.browser.msie && parseInt($.browser.version, 10) == 9;
		}

		function isIe10() {
			return $.browser.msie && parseInt($.browser.version, 10) == 10;
		}

		function isNonChromeWebkit() {
			return $.browser.webkit && !/chrome/.test(navigator.userAgent.toLowerCase());
		}

		function majorBrowserVersion() {
			return parseInt($.browser.version, 10);
		}

		// add these classes to all list items
		$("li:first-child").addClass("tf-first-child");
		$("li:last-child").addClass("tf-last-child");

		// apply css styles with javascript if ie < 9 or safari (safari media queries exclude scrollbars when calculating viewport width)
		if ((isNonChromeWebkit() && !isMobileSafari()) || isIe7() || isIe8()) {
			// detect ie8 and fix bugs
			if ($.browser.msie && parseInt($.browser.version, 10) == 8) {

				// persistent vertical scrollbar stops ie8 from entering into js resize loops
				$('html').css({'height': '100%', 'overflow-y': 'scroll'});

				themefitApplyStyles($(this).width());

				var delayResize = null;  // resize on timer to slow ie8 jQuery triggering
				var delayTime = 50;

				$(window).resize(function () {
					if (delayResize) {
						clearTimeout(delayResize);
					}

					delayResize = setTimeout(function () {
						themefitApplyStyles($(this).width());
						themefitRefresh();
					}, delayTime);
				});
				// safari or ie7
			} else {

				themefitApplyStyles($(this).width());

				$(window).resize(function () {
					themefitApplyStyles($(this).width());
					themefitRefresh();
				});
			}
			// all other browsers
		} else {

			$(window).resize(function () {
				themefitRefresh();
			});
		}

		// apply styles
		function themefitApplyStyles(width) {
			if (isIe7() || isIe8() || (isNonChromeWebkit() && !isMobileSafari() && majorBrowserVersion() < 7)) {
				width = parseInt(width);
				if (width <= 750) {
					$("#styles-751-970-css").attr("media", "none");
					$("#styles-0-750-css").attr("media", "screen");
				} else if ((width >= 751) && (width <= 970)) {
					$("#styles-751-970-css").attr("media", "screen");
					$("#styles-0-750-css").attr("media", "none");
				} else {
					$("#styles-751-970-css").attr("media", "none");
					$("#styles-0-750-css").attr("media", "none");
				}
			}
		}

		// refresh all dynamicaly sized modules
		function themefitRefresh() {
			$("#main-nav").themefitmobilemenu('refresh');
			$('div.tf-gallery-wrapper').themefitgallery('refresh');
			$('div.tf-slideshow-wrapper').themefitslideshow('refresh');
			$('.showhide').themefitshowhide('refresh');

			refreshVideo();
		}

		// wrap embedded video elements and make responsive
		var videoElementTypes = [
			"iframe[src*='//player.vimeo.com']",
			"iframe[src*='//www.youtube.com']",
			"object",
			"embed"
		];
		var videoElements = $(videoElementTypes.join(', '));
		videoElements.wrap('<div class="video-container" />');

		function refreshVideo() {
			var videoContainer = $(".video-container");
			videoElements.each(function () {
				var container = $(this);
				var w = container.parent().width();
				container.width(w).height(w * container.attr('data-aspect-ratio'));
			});
		}


		// initialize all modules
		function themefitInit() {
			$("#main-nav").themefitmobilemenu();
			$('div.tf-gallery-wrapper').themefitgallery();
			$('div.tf-slideshow-wrapper').themefitslideshow();
			$('.showhide').themefitshowhide();

			// handle responsive videos
			videoElements.each(function () {
				$(this).attr('data-aspect-ratio', this.height / this.width).removeAttr('height').removeAttr('width');
			});
			refreshVideo();
		}

		// ensure resize executes after all css has been loaded
		function cssLoadPrecaution() {
			themefitApplyStyles($(this).width());
			themefitRefresh();
		}

		themefitInit();

		setTimeout(function () {
			cssLoadPrecaution();
		}, 2000);

	});
})(jQuery); 