$(document).ready(function() {
	// Mark JS as enabled:
	$('body.nojs').removeClass('nojs');
	
		/* IE fixes */
	if ($.browser.msie) {
		// 8/7/6
		if (parseFloat($.browser.version) < 9) {
			$('#menu ul ol li:first-child').addClass('first-child');
			$('#menu ul ol li:last-child').addClass('last-child');
		}
	}

	// Open external links in new window:
	$('body')
		.on('click', 'a[rel = external], a.external', function() {
			window.open($(this).attr('href'));

			return false;
		});
	
		
	// Make footer fit screen
	var browserHeight = $(window).height();
	var webpageHeight = $('body').height();
	
	if(webpageHeight < browserHeight) {
		var difference = (browserHeight - webpageHeight) + 71;
		$('#footer').css('height', difference);
	}
	
	

	// Toggle focused case study:
	$('ol.service-features')
		.on('click', 'li.case-study:not(.target) h3 a', function() {
			var $item = $(this) .closest('li').addClass('target'),
				$list = $item.find('dl'),
				height = $list.height();

			$list.css({
				'height': 'auto'
			});

			height = $list.height();

			$list
				.css({
					'height': '0'
				})
				.animate({
					'height': height + 'px'
				});

			return false;
		})

		.on('click', 'li.case-study.target h3 a', function() {
			var $item = $(this) .closest('li').removeClass('target'),
				$list = $item.find('dl');

			$list.css({
				'height': 'auto'
			});

			$list.animate({
				'height': '0'
			});

			return false;
		});

	// Show the first service feature:
	$('ol.service-features li.case-study:first-child dl')
		.closest('li')
		.addClass('target');

	// Position the CTA button:
	if ($('p.cta-button').length) (function() {
		var $cta = $('p.cta-button'),
			$content = $('#content > div.content'),
			cta_top = $cta.offset().top;

		$(window)
			.on('scroll resize', function() {
				if ($(window).scrollTop() > cta_top - 100) {
					$cta.css({
						'left':			$content.offset().left
											+ $content.width()
											- $cta.width() - 10 + 'px',
						'position':		'fixed',
						'top':			'100px'
					});
				}

				else {
					$cta.css({
						'left':			'auto',
						'position':		'absolute',
						'right':		'8px',
						'top':			'auto'
					});
				}
			})

			.trigger('scroll');
	})();
});
