// Top Nav
$(function () {
	if ($.browser.msie) {
		$('#nav-primary ul li').hover(function () {
			$(this).addClass('hover');
		}, function () {
			$(this).removeClass('hover');
		});
	}
	
	$('#nav-primary ul li ul li:first').addClass('first');
});

// Equalize height on content columns on the home page
$(function () {
	var secondary = $('body.home #content-secondary-middle').height('auto');
	var tertiary = $('body.home #content-tertiary-middle').height('auto');
	
	// Make the heights equal
	if (secondary.length && tertiary.length) {
		var maxHeight = secondary.height() >= tertiary.height() ?
			secondary.height() :
			tertiary.height();

		secondary.height(maxHeight);
		tertiary.height(maxHeight);
	}
});
/*
// Equalize height on content columns on the content pages
$(function () {
	setTimeout(function () {
		var primary = $('body.content-page #content-primary').height('auto');
		var secondary = $('body.content-page #content-secondary').height('auto');
	
		primary.append('<div class="clear"></div>');
		secondary.append('<div class="clear"></div>');

		// Make the heights equal
		if (primary.length && secondary.length) {
			var maxHeight = primary.height() >= secondary.height() ?
				primary.height() :
				secondary.height();

			primary.height(maxHeight);
			secondary.height(maxHeight);
		}
	}, 250);

	return;
});
*/
// Our Services tabs on home page
$(function () {
	var ourServices = $('body.home #our-services');
	var nav = ourServices.find('ul.nav');
	
	nav.find('a').click(function () {
		nav.find('li.on').removeClass('on');
		$(this).parents('li').addClass('on');
		
		ourServices.find('div.section').hide();
		ourServices.find('#' + $(this).attr('href').split('#')[1]).show();
		
		return false;
	});
});

// Set up carousel
$(function () {
	var carousel = $('body.home div.carousel');
	var carouselSet = [];
	var carouselSets = [];
	
	var activeSet = 0;
	
	// Adjust li height to auto
	var maxHeight = 0;
	carousel.find('li').height('auto');
	
	// Group and get max height
	carousel.find('li').each(function (index) {
		// Get max height
		if ($(this).height() > maxHeight) {
			maxHeight = $(this).height();
		}
		
		// Build sets of three
		if (index % 3 === 0 && index !== 0) {
			carouselSets.push(carouselSet);
			carouselSet = [];
		}
		
		carouselSet.push(this);
		
		// Add the last set if there is an incomplete one
		if (index + 1 === carousel.find('li').length) {
			carouselSets.push(carouselSet);
		}
	});
	
	// Set Max height
	carousel.find('ul, li').height(maxHeight);

	// Next/previous buttons
	carousel.find('a.previous, a.next').click(function () {
		// Hide the current set
		carousel.find('li').hide();

		// Account for next on last and previous on first
		if ($(this).hasClass('previous')) {
			activeSet = (activeSet === 0) ? carouselSets.length - 1 : activeSet - 1;
		} else if ($(this).hasClass('next')) {
			activeSet = (activeSet === carouselSets.length - 1) ? 0 : activeSet + 1;
		}

		// Show the next set
		for (var i = 0; i < carouselSets[activeSet].length; i++) {
			$(carouselSets[activeSet][i]).show();
		}

		return false;
	});
});


