

var firstDisplay = true;

// Homepage case study previwer setup

$(document).ready(function() {

						   
	// Update services carousel to accomodate scroll buttons 
	$('div.psWrap').css({'left':'38px', 'width':'407px'});
	$('div.servicesList ul li').css({'width':'142px'});
	$('div.servicesList').css({'width':'400px'});

	$('.products a.scrollLeft').css({display: 'block'});

	$('.products a.scrollRight').css({display: 'block'});

	$('.products .servicesList').jCarouselLite({

		visible: 3,

		start: 0,

		btnNext: '.products .scrollRight',

		btnPrev: '.products .scrollLeft',

		speed:500,

		circular:true

	});



	$('.message .peopleThumbs').jCarouselLite({

		visible: 4,

		start: 0,

		btnNext: '.message .scrollRight',

		btnPrev: '.message .scrollLeft',

		speed:500,

		circular:true

	});

	$('div.servicesList').css({width:'439px'}); /* Ensure last visible list item does not display full width */

	

	$('.peopleThumbs ul li a').prepend('<div class="overlay"></div>');

	$('.peopleThumbs div.overlay').hide();

	

	$('.peopleThumbs ul li a').bind('mouseover', function(e){

		$(this).parent().find('div').show();

	});

	$('.peopleThumbs ul li a').bind('mouseout', function(e){

		$(this).parent().find('div').hide();

	});



	$('.peopleThumbs ul li a').each(function(i) {

    	$(this).click(function() {

			var index = $(this).attr('class');

			deselectMessage();

			var target = $('ul.portraitImageList li.' + index + ' img');

  			$(target).fadeIn(400, function() { $(target).addClass('show'); updateBannerText(index); });

			$('.headline h1').hide();

		});

  	});

});



function deselectMessage()

{

	$('ul.portraitImageList li img').each(function(n) {

		if ($(this).hasClass('show')) {

			$(this).hide();

			$(this).removeClass('show');

		}

	});

	if (!$('.headline div').hasClass('hidden'))

	{

		$('.headline div span').slideToggle(200, function(){$('.headline div span').css('color', '#000')});

	}

	$('p.statement').hide();

}



function updateBannerText(index)

{

	var caption = bannerCaptions[index-1];

	var teamMember = bannerPeople[index-1];

	var hasTitle = (teamMember.length > 0) ? true: false;

	var heading = '.headline h1';

	var headingLink = 'h1 a';

	var statement = 'p.statement';

	$(headingLink).html(caption);

	$(headingLink).attr('href', bannerUrls[index-1]);

	if (hasTitle) {

		$('.headline span').html(teamMember);

		$('.headline div').removeClass('hidden');

	}

	else

	{

		$('.headline span').empty();

		$('.headline div').addClass('hidden');

	}

	

	$(statement).html(bannerStatements[index-1]);

	

	var lineHeight = getLineHeight(caption, '<br />');

	if (lineHeight >= 3 || getMaxWordLength(caption, 7))

	{

		if (!$(heading).hasClass('small')) { $(heading).addClass('small') };

	}

	else

	{

		if ($(heading).hasClass('small')) { $(heading).removeClass('small') };

	}

	

	Cufon.replace(heading, { fontFamily: 'Akkurat-Bold' });

	Cufon.replace("p.statement", { fontFamily: 'Akkurat' });

	Cufon.replace("p.statement span", { fontFamily: 'Akkurat-Bold' });

	if (!firstDisplay)

	{

		$('h1').delay(100).fadeIn(300, function(){ 

			if (hasTitle) { $('.headline div span').slideToggle(200, function(){$(this).css('color', '#FFF')}); }

		});

	}

	else

	{

		firstDisplay = false;

	}

	$(statement).delay(500).fadeIn(400);

}



function getMaxWordLength(str, maxLength)

{

	str = str.replace(/<br \/>/g, ' '); // Remove html line breaks

	var words = new Array();

	words = str.split(' ');

	var hasInstance = false;

	for (var i = 0; i < words.length; i++)

	{

		if (words[i].length >= maxLength)

		{

			hasInstance = true;

			break;

		}

	}

	return hasInstance;

}



function getLineHeight(str, subStr)

{

	var n = 0;

	for (var i=0; i < str.length; i++)

	{

		if (subStr == str.substr(i, subStr.length))

		n++;

	}

	return n;

}
