// Normalize Carousel Heights - pass in Bootstrap Carousel items.
$.fn.carouselHeights = function() {

	var items = $(this), //grab all slides
		heights = [], //create empty array to store height values
		tallest; //create variable to make note of the tallest slide

	var normalizeHeights = function() {

		items.each(function() { //add heights to array
			heights.push($(this).height());
		});
		tallest = Math.max.apply(null, heights); //cache largest value
		items.each(function() {
			$(this).css('min-height',tallest + 'px');
		});
	};

	normalizeHeights();

	$(window).on('resize orientationchange', function () {
		//reset vars
		tallest = 0;
		heights.length = 0;

		items.each(function() {
			$(this).css('min-height','0'); //reset min-height
		});
		normalizeHeights(); //run it again
	});

};

jQuery(function($) {

	$('.lightbox').fancybox({
		groupAttr: 'data-rel',
		prevEffect: 'none',
		nextEffect: 'none',
		closeBtn: true,
		helpers: {
			title: {
				type: 'inside'
			}
		},
		beforeShow : function() {
			var title = this.element.find('img').attr('title');
			this.inner.find('img').attr('title', title);
			this.title = title;
		}
	});

	$('.sticky').each(function () {
		var stickyTop = $('.sticky').offset().top;
		$(window).scroll(function () {
			var windowTop = $(window).scrollTop();
			if (stickyTop < windowTop) {
				$('.sticky').addClass('sticked');
			}
			else {
				$('.sticky').removeClass('sticked');
			}
		});
	});

	// Handle newsletter subscription categories
	function updateCategories() {
		var selectedCategories = [];
		$('#choose-categories :checked').each(function () {
			selectedCategories.push($(this).val());
		});
		$('#fp_categories').val(selectedCategories)
	}
	$(function () {
		$('#choose-categories input:checkbox').change(updateCategories);
		// updateCategories();
	});

	try {
		if (WURFL.is_mobile) {
			$('body').addClass('mobile');
		}
		else {
			$('body').addClass('no-mobile');
		}
	}
	catch(e) {
		$('body').addClass('no-mobile');
	}

	$(window).on('load', function(){
		$('.normalizeHeight .item').carouselHeights();
	});
});
