// JavaScript Document
	
//preloader
var a=new Array();
a[0]=new Image();	
a[0].src='../images/logo.png';
a[1]=new Image();
a[1].src='../images/gray_bg.png';
a[1]=new Image();
a[1].src='../images/bg.jpg';

//state monitors for Homepage slider, testimonials, and Insights slideshow
var state=0;
var stateNew=1;
var pause=false;
var tState=0;
var iState=0;

	//starts all functions when document is ready
	$(document).ready(function(){
		$('#right .col:even').css('margin-left',0); //corrects a column layout issue
		testimonials();
		blueBoxHeaders();
		linkRewrite();
		headerSpace();
		bioImg();
		sortClients();
		insightsBlueBoxes();
		pdfNewWindow();
		introTextFix();
		mailingForm();
		siteMapFix();
		});
	$(window).load(function(){
		imgIn();
		sliderShow();
		var slide1=setTimeout(function(){subMediaSlideshow();},8000);
		matchHeight();
		});	
	
	//waits till images are loaded, then fades them in, checks left and right column heights, and starts Insights slideshow	
	function imgIn(){
		$('div.category-desc img, .col > img').css({'visibility':'visible','display':'none'});
		$('div.category-desc img, .col > img').not('div.institute_insights img, div.clarion_calls img').fadeIn(300);}	
		
	function matchHeight(){
		if($('#gallery').length) $('#gallery').css('height',$('#gallery div').height());
		for(var i=0; i<2; i++){
			var lH=$('#left').height();
			var rH=$('#right').height();
			if(rH >= lH) $('#left').css('height',rH);
			else $('#left').css('height','auto');}}		
		
	//Homepage slideshow functions (creates controls and begins slideshow)
	function sliderShow(){
		if($('#mediaPane').length){
			var h=$('#mediaPane').offset().top;
			var txt='<div id="lArrow"><\/div><div id="middle"><\/div><div id="rArrow"><\/div>';
			var L=$('#mediaPane > div').length;
			$(txt).appendTo('#control');
			$('#control').css('top',h);
			$('#control').css({'display':'none','visibility':'visible'});	
			$('#control').fadeIn();
			loop();
			$('#mediaPane, #control').mouseenter(function(){$('#middle').animate({width:873});});
			$('#nav, .contentblock1, .contentblock2, #footer').mouseenter(function(){$('#middle').animate({width:780});});	
			$('#lArrow').click(function(){
				pause=true; 
				$('#mediaPane > div').eq(state).fadeOut(); 
				state=(state==0)?L-1: state-1;
				loop();});
			$('#rArrow').click(function(){
				pause=true;
				$('#mediaPane > div').eq(state).fadeOut(); 
				state=(state==L-1)?0: state+1;
				loop();});}}
	
	//basic functions of slideshow effects and navigation
	function loop(){
			var e=$('#mediaPane > div').eq(state);
			$(e).css({'visibility':'visible','display':'none'});
			$(e).fadeIn(function(){
				var e1=$(e).children('div');
				var h1=$(e1).height();
				if(stateNew && (navigator.appName != 'Microsoft Internet Explorer')) {
					$(e1).animate({'top':(325-h1)/2,'opacity':1},1000);
					}
				else if(stateNew && (navigator.appName == 'Microsoft Internet Explorer')) {
					$(e1).css('display','block');
					$(e1).animate({'top':(325-h1)/2},1000, function(){
						$(e1).css({'opacity':1,'filter':''});
						});
					}
				});
			var slide=setTimeout(function(){
				if(!pause){
					$(e).fadeOut();
					if($(e).next().length){
						$(e).next().fadeIn(function(){
							var e2=$(e).next().children('div');
							var h2=$(e2).height();
							if(stateNew && (navigator.appName != 'Microsoft Internet Explorer')) $(e2).animate({'top':(325-h2)/2,'opacity':1},1000);
							else if(stateNew && (navigator.appName == 'Microsoft Internet Explorer')) {
								$(e2).css('display','block');
								$(e2).animate({'top':(325-h2)/2},1000, function(){$(e2).css({'opacity':1,'filter':''});});}});
						state++;}
					else {
						$(e).siblings().first().fadeIn(function(){
							var e3=$(e).siblings().first().children('div');
							var h3=$(e3).height();
							if(stateNew && (navigator.appName != 'Microsoft Internet Explorer')) $(e3).animate({'top':(325-h3)/2,'opacity':1},1000);
							else if(stateNew && (navigator.appName == 'Microsoft Internet Explorer')) {
								$(e3).css('display','block');
								$(e3).animate({'top':(325-h3)/2},1000, function(){$(e3).css({'opacity':1,'filter':''});});}});
						state=0;
						stateNew=0;}
					loop();}},6000);}	
	
	//testimonial slideshow functions		
	function testimonials(){
		if($('#testimonials').length){
			var eH=0;
			$('#testimonials > div').each(function(){eH=($(this).height()>eH)?$(this).height():eH;});
			$('#testimonials').css('height',eH);
			var initH=$('#left').height();
			$('#left').css('min-height',initH+40);
			matchHeight();
			testimonialsTwo();}}
							
	function testimonialsTwo(){
		var e=$('#testimonials > div').eq(tState);
		$('#testimonials').animate({'height':$(e).height()});
		$(e).css({'display':'none','visibility':'visible'});
		$(e).fadeIn();
		var slide=setTimeout(function(){
			$(e).fadeOut();
			if($(e).next().length) tState++;
			else tState=0;
			testimonialsTwo();
			},12000);}				
	
	//image swapping on Bio pages	
	function bioImg(){
		if($('.col img').length){
			var e=$('.col img').eq(1);
			$(e).hover(function(){$(e).fadeOut(200);});
			$('.col img').eq(0).hover(function(){},function(){$(e).fadeIn(200);});}}	
	
	//client list functions (parses lists, creates nav links, creates alphabetized list, and controls back-to-top button)		
	function sortClients(){
		if($('#sortIndustry').length){
			var alpha=['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'];
			var clients=new Array();
			var txt='';
			var txt3='';
			$('#sortIndustry li:first-child').each(function(index, element){
				txt=txt+'<li><a href="#sortIndustry-'+index+'">'+$(this).text()+'</a></li>';});
			$('#sortIndustry ul').each(function(index, element){$(this).attr('id','sortIndustry-'+index);});
			$('<ul>'+txt+'</ul>').insertBefore('#sortIndustry ul:first-child');
			$('#sortIndustry ul li').not('#sortIndustry ul:first-child li, #sortIndustry ul li:first-child').each(function(index, element){
                clients.push($(this).text());
				clients.sort();});	
			for(i=0; i<alpha.length; i++){
				var txt2='';
				for(i2=0; i2<clients.length; i2++){
					if(clients[i2].slice(0,4)=='The '){
						if(clients[i2][4]==alpha[i]) txt2=txt2+'<li>'+clients[i2]+'</li>';}
					else if(clients[i2][0]==alpha[i]) txt2=txt2+'<li>'+clients[i2]+'</li>';}
				$('#sortAlpha').append('<ul id="sortAlpha-'+alpha[i]+'"><li>'+alpha[i]+'</li>'+txt2+'</ul>');
				txt3=txt3+'<a href="#sortAlpha-'+alpha[i]+'">'+alpha[i]+'</a>';
				txt3=(alpha[i]=='N')?txt3+'<br />':txt3;}	
			$('<ul><li>'+txt3+'</li></ul>').insertBefore('#sortAlpha ul:first-child');
			matchHeight();
			$('#byIndustry').click(function(){
				$('#sortAlpha').hide();
				$('#sortIndustry').show();
				$('.stateOne, .stateTwo').toggle();
				matchHeight();
				});	
			$('#byAlpha').click(function(){
				$('#sortIndustry').hide();
				$('#sortAlpha').show();
				$('.stateOne, .stateTwo').toggle();
				matchHeight();
				});	
			$(document).scroll(function(){
                if($(window).scrollTop()>400) $('#sortTop').fadeIn();
				else $('#sortTop').fadeOut();});}}		
	
	//slideshow functions for Insights (or other) page	
	function subMediaSlideshow(){
		var e=$('#submediaPane img');
		var len=$(e).length;
		$(e).fadeOut();
		$(e).eq(iState).fadeIn();
		if(iState+1 < len) iState++;
		else iState=0;
		var slide=setTimeout(function(){subMediaSlideshow();},8000);}		
		
	function linkRewrite(){
		$('#left a, #right a').each(function(){
			var txt=$(this).text();
			if(txt.indexOf('Read More')!=-1){
				$(this).text('Read More');
				$(this).attr('class','orange button');
				$(this).after('<div class="clear"></div>');}});}		
				
	function blueBoxHeaders(){
		var regE=/^\s*(business strategy|organizational structure|culture alignment|leadership effectiveness|business transformation|talent)/i;
		var regE2=/^\s*mergers & acquisitions/i;
		$('ul.darkblue p').each(function(){
			if($(this).text().search(regE)==0) $(this).text($(this).text().replace(regE,''));
			if($(this).text().search(regE2)==0) $(this).text($(this).text().replace(regE2,''));});}			
			
	function headerSpace(){
		if(($('#right img').length==0)&&($('div.item-page').length)){
			$('div.item-page').css('margin-top','40px');
			$('div.item-page h2').css('margin-bottom','10px');}}		
		
	function insightsBlueBoxes(){
		$('div.lightblue div.institute_insights a, div.lightblue div.clarion_calls a').unwrap().unwrap().unwrap().wrap('<p></p>');
		$('div.lightblue div.institute_insights p, div.lightblue div.clarion_calls p').each(function(){
			if($(this).children().length==0) $(this).hide();});}	
		
	function pdfNewWindow(){
		$('a').each(function(){
            if($(this).attr('href').search(/pdf$/i) != -1) $(this).attr('target','_blank');});}	
		
	function introTextFix(){
		$('p.mod-articles-category-introtext').each(function(){
			var intro=$(this).text();
            if(intro.length>=300){
				$(this).text(intro.slice(0,intro.lastIndexOf(' ')));
				$(this).text($(this).text()+'...');}
			var intro2=$.trim($(this).text());	
			if(intro2.slice(0,10)=="Challenge.") $(this).html("<strong>Challenge.</strong>"+intro2.slice(10));});}
				
	function mailingForm(){
		$('li.mailing').click(function (){$('li.mailingForm').slideToggle();});
		var init='Your name...';
		var init2='Your email...';
		$('#newsletterName').attr('value',init);
		$('#newsletterEmail').attr('value',init2);
		$('#newsletterName').focus(function(){
			if($('#newsletterName').attr('value')==init) $('#newsletterName').attr('value','');});
		$('#newsletterName').blur(function(){
			if($('#newsletterName').attr('value')=='') $('#newsletterName').attr('value',init);});
		$('#newsletterEmail').focus(function(){
			if($('#newsletterEmail').attr('value')==init2) $('#newsletterEmail').attr('value','');});
		$('#newsletterEmail').blur(function(){
			if($('#newsletterEmail').attr('value')=='') $('#newsletterEmail').attr('value',init2);});	
		$('#mailingSubmit').click(function(){
			$.ajax({
				type: 'POST',
				data: 'name='+$('#newsletterName').val()+'&email='+$('#newsletterEmail').val(),
				url: '../../../mailing.php',
				success: function(data) {$('p.status').text(data);}});});}			
				
	function siteMapFix(){
		$('<h4>Home</h4>').insertBefore($('ul.category-module-sitemap > li:first-child'));
		$('ul.category-module-sitemap h4').each(function(){
			var e=$(this);
			//include any page titles to exclude from the sitemap in the array below (comma-separated)
			var exclude=['thank you'];
			for(ex=0; ex<exclude.length; ex++){
				if($(this).text()==exclude[ex]) $(this).parent().remove();}
			var e2=$('ul.category-module-sitemap h5');
			for(i=0; i<$(e2).length; i++){
				if($.trim($(e).text())==$.trim($(e2).eq(i).text()) && $.trim($(e).text())!=('privacy statement' || 'about us' || 'contact us')) {
					$(e).parent().children('ul').clone().appendTo($(e2).eq(i).parent());
					$(e).parent().remove();}}});
		$('ul.category-module-sitemap h4').each(function(){
            var ob=$(this);
			for(i=0; i<$('#nav a').length; i++){
				var a=$('#nav a').eq(i);
				var aLink=$(a).attr('href');
				if($.trim($(ob).text())==$.trim($(a).text())) {
					$(ob).contents().wrap('<a href="'+aLink+'"></a>');
					}}});}			
