var postGroups;
var postGroupCount;
var postGroupDirection='next';
var selectedPostGroup;
var prevPostGroup;
var nextPostGroup;
var curLoadingBg;
var timer

// See LW Culture Fade effect
/*$(document).ready(function() {
	$('#clickstart').hover(function() {
		$('.opac').stop().animate({ opacity: 1.0 }, 500);
	}, function() {
	    $('.opac').stop().animate({ opacity: 0.5 }, 500);
	});
});

$('#navPrevNextContainer').hide();
$('#clickstart').click(function () {
	$('#loading, #employeeBgFadeContainerLoad, #employeeBgFadeLoad, #bottomGradLoad').fadeOut("slow");
	$('#navPrevNextContainer').show();
}); */

function submitForm(formId){$(formId).submit();}
function slideShowTimer(){
		clearTimeout(timer);
		timer=setTimeout('animatePostSlideNext(nextPostGroup)', 12000)
}

// set the src for home page background images
function setEmployeeBgImageSrc(group){
	var allEmployeeBg = $('.employeeBg div');
	var thisEmployeeBg = '#employeeBg'+group;
	var prevEmployeeBg = '#employeeBg'+prevPostGroup;
	var nextEmployeeBg = '#employeeBg'+nextPostGroup;
	var thisEmployeeBgImage = thisEmployeeBg+' div';
	var thisEmployeeBgFade = '#employeeBgFade'+group+', #bottomGrad'+group;
	var nextEmployeeBgFade = '#employeeBgFade'+nextPostGroup+', #bottomGrad'+nextPostGroup;
	var prevEmployeeBgFade = '#employeeBgFade'+prevPostGroup+', #bottomGrad'+prevPostGroup;
	var thisEmployeeContent = '#contentSlide'+group;
	var thisEmployeeIEfix = '#contentSlide'+group+' .titleText, #contentSlide'+group+' .homeSlideComments, #contentSlide'+group+' .homeSlideInfo';
	var thisEmployeeBgSrc = $(thisEmployeeBgImage).attr('origsrc');
	jQuery.each($('.employeeBg div'), function(i, v) {$(v).unbind('load');});
	var img = new Image();
	if(group != 0){
	$(img).load(function(){
		if(curLoadingBg == group){
			jQuery.each(allEmployeeBg, 
				function(i, v){
					if((i+1)==(prevPostGroup) || (i+1)==group || (i+1)==(nextPostGroup)){
						$(v).css('backgroundImage', $(v).attr('origsrc')); 
					}
				});
				$(thisEmployeeBg).animate({left:'0'}, 700, function () {
					$(thisEmployeeBgFade).fadeIn(700, function() { $(prevEmployeeBgFade).fadeOut(700);$(nextEmployeeBgFade).fadeOut(700); });
					$(thisEmployeeIEfix).addClass('white'); //add white bg
					$(thisEmployeeContent).fadeIn('700', function(){ //fade content in
						$(thisEmployeeIEfix).removeClass('white'); if (jQuery.browser.msie)this.style.removeAttribute('filter');}) //remove white bg & filter
					.animate({lw: 1}, 200000, function(){ //wait
						$(thisEmployeeIEfix).addClass('white') }).fadeOut(700) //add white bg and fade out
					}); 
				}
			});
		curLoadingBg = group;
		var imgSrcLength = ((thisEmployeeBgSrc.length)-5);
		$(img).attr({'src':thisEmployeeBgSrc.substr(4,imgSrcLength)});
	} else {
		jQuery.each(allEmployeeBg, 
			function(i, v){
				if((i+1)==(prevPostGroup) || (i+1)==(nextPostGroup)){
					$(v).css('backgroundImage', $(v).attr('origsrc')); 
				}
			});
	}
}
// moves the employee post backgrounds
function animateEmployeeBg(group){
	var thisEmployeeBg = '#employeeBg'+group;
	var prevEmployeeBg = '#employeeBg'+prevPostGroup;
	var nextEmployeeBg = '#employeeBg'+nextPostGroup;
	var thisEmployeeContentFade = '.contentHome';
	var allEmployeeBg = $('.employeeBg');
	var thisEmployeeIEfix = '#contentSlide'+group+' .titleText, #contentSlide'+group+' .homeSlideComments, #contentSlide'+group+' .homeSlideInfo';
	var clearQueue = true; //variable to stop fade queue by MP
	var gotoEnd= true; //variable to go to end of animation by MP
	var pHeight='606px';
	var winWidthPlus = '1005px';
	allEmployeeBg.css({'display':''});
	if(postGroupDirection=='next'){
		$(thisEmployeeContentFade).hide().stop(clearQueue , gotoEnd); //stop fade queue and hide bg if button is clicked by MP
		$(thisEmployeeIEfix).stop(clearQueue , gotoEnd); 
		$(prevEmployeeBg).animate({left:'-'+winWidthPlus}, 600);
		$(nextEmployeeBg).css({'left':winWidthPlus});
	} else{
			$(thisEmployeeContentFade).hide().stop(clearQueue , gotoEnd); //stop fade queue and hide bg if button is clicked by MP
			$(thisEmployeeIEfix).stop(clearQueue , gotoEnd); 
			$(prevEmployeeBg).css({'left':'-'+winWidthPlus});		
			$(nextEmployeeBg).animate({left:winWidthPlus}, 600);
	}
	jQuery.each(allEmployeeBg, function(i){
		var iBg = '#employeeBg'+i;
		if(iBg!=thisEmployeeBg && iBg!=prevEmployeeBg && iBg!=nextEmployeeBg){$(iBg).css({'left':'-'+winWidthPlus});}
		});
}

// set the direction of content animation and call animation
function animatePostSlideNext(group){
	var g=group;
	postGroupDirection = 'next';
	animatePostSlides(g, false);
}

// set the direction of content animation and call animation
function animatePostSlidePrev(group){
	var g=group;
	postGroupDirection = 'prev';
	animatePostSlides(g, false);
}

// content animation
function animatePostSlides(group, init){
	// this is used to detect if an animation is currently running and prevent jumping of the page
	$('#homePageContainer').stop(false, false);
	if($('#homePageContainer').attr('style').match('height') == null){
		$('#homePageContainer').css({'height':$('#homePageContainer').innerHeight()});
	}else{
		$('#homePageContainer').css({'height':$('#homePageContainer').css('height')});
	}
	$('#homePageContainer').queue("fx", []);
	selectedPostGroup = '#contentSlide'+group;
	setPrevNextLinks(group);
	setEmployeeBgImageSrc(group);
	animateEmployeeBg(group);
	var speed = 700;
	var ease = 'easeOutQuart';
	postGroups.css({'position':'absolute'});
	// show the clicked slide
	$(selectedPostGroup).queue("fx", []);
	$(nextPostGroup).queue("fx", []);
	$(prevPostGroup).queue("fx", []);
	$(selectedPostGroup).css({'position':'relative', 'zIndex':'5200'});
	if(postGroupDirection=='next'){	
		$('#contentSlide'+nextPostGroup).css({'zIndex':'5050'});
		$('#contentSlide'+nextPostGroup).animate({left: '1001px'}, 0);
		$('#contentSlide'+prevPostGroup).animate({left: '-1001px'}, speed-100, ease);
	}
	if(postGroupDirection=='prev'){	
		$('#contentSlide'+prevPostGroup).css({'zIndex':'5050'});
		$('#contentSlide'+prevPostGroup).animate({left: '-1001px'}, 0);
		$('#contentSlide'+nextPostGroup).animate({left: '1001px'}, speed-100, ease);
	}
	if($(selectedPostGroup).css('left')!='0px'){$(selectedPostGroup).animate({left: '0px'}, speed, ease, animateHomePageContainerHeight('post', group));}
	jQuery.each(postGroups, function(i) {												 
		var iGroup = ((i)+1);
		var thisGroup = '#contentSlide'+iGroup;
		if(thisGroup != selectedPostGroup){ 
			if((iGroup!=prevPostGroup && iGroup!=nextPostGroup)){
				$(thisGroup).css({'position': 'absolute'});
				$(thisGroup).css({'zIndex':'5050'});
				$(thisGroup).css({'left': '-1001px'});
				}
			}
		});
	slideShowTimer();
	//hide gradient from first slide, show on others
	if(group < 2){
		$('div.homeGradDiv').hide();
		$('#contentHomeContainer').css({'background-image':'none'});
	} else if(group > 1) {
		$('div.homeGradDiv').show();
		$('#contentHomeContainer').css({'background-image':''});
	}
}
		
// set the previous and next links after content is changed
function setPrevNextLinks(group){
	prevPostGroup = group;
	nextPostGroup = group;
	if(group > 1){prevPostGroup = (--prevPostGroup);} else {prevPostGroup = postGroupCount;} 
	if(group < postGroupCount){nextPostGroup = (++nextPostGroup);} else {nextPostGroup = 1;} 
	$('#navPrev a').unbind('click');
	$('#navNext a').unbind('click');
	$('#navPrev a').bind('click', function(){animatePostSlidePrev(prevPostGroup); return false;});
	$('#navNext a').bind('click', function(){animatePostSlideNext(nextPostGroup); return false;});
}

// animate the content area height then release height to allow variable text size
function animateHomePageContainerHeight(container, group){
	$('#homePageContainer').queue("fx", []);
	var finalHeight=350;
	var speed=800;
	var ease='easeInOutBack';
	selectedPostGroup = '#contentSlide'+group;
	if(container == 'post'){finalHeight = $(selectedPostGroup).innerHeight(); ease='easeInOutBack';}
	if(finalHeight == 0){finalHeight = 350};
	$('#homePageContainer').animate({'height': finalHeight+'px'}, speed, 'easeOutBack');
	//$('#homePageContainer').queue("fx", function(){$('#homePageContainer').css({'height':''});}); //removed so footer does not move with every post		
}

// opening main nav animation
function navAnimate(){
	$('#navCompany').animate({left: '-120px'}, 0, '' );
	$('#navWork').animate({left: '0px'}, 0, 'easeOutBounce' );
	$('#navExpertise').animate({left: '486px'}, 0, 'easeOutBounce' );
	$('#navIdeas').animate({left: '606px'}, 0, 'easeOutBounce' );
}

//countdown
jQuery.fn.countDown = function(settings,to) {
	settings = jQuery.extend({
		startFontSize: '36px',
		endFontSize: '24px',
		duration: 1000,
		startNumber: 10,
		endNumber: 0,
		callBack: function() { }
	}, settings);
	return this.each(function() {		
		if(!to && to != settings.endNumber) { to = settings.startNumber; }
		$(this).animate({'opacity': settings.endFontSize},settings.duration,'',function() {
			if(to > settings.endNumber + 1) {
				$(this).css('fontSize',settings.startFontSize).text(to - 1).countDown(settings,to - 1);
			}else{
				//settings.callBack(this);
				$('#showStart').text('Showtime')
			}
		});		
	});
};

// initialize the page
function initHome(){
	// Hide the navigation
	$('#navCompany').css({'left':'-322px'});
	$('#navWork').css({'left':'-322px'});
	$('#navExpertise').css({'left':'1000px'});
	$('#navIdeas').css({'left':'1000px'}); 
	// set the content slides to the left
	$('#contentHomeContainer').css({'position':'relative'});
	$('#employeeBgContainer').css({'height':'606px'});

	postGroups = $('.contentHome')
	postGroupCount = postGroups.length;
	postGroups.css({'left':'-1001px'}).css({'position':'absolute'});
	// set the previous & next mousover / out animations
	$('#navPrev').bind('mouseover', function(){navMouseAnimate('#navPrev','-80px');});
	$('#navPrev').bind('mouseout', function(){navMouseAnimate('#navPrev','-100px');});
	$('#navNext').bind('mouseover', function(){navMouseAnimate('#navNext','784px');});
	$('#navNext').bind('mouseout', function(){navMouseAnimate('#navNext','804px');});
	// launch animations
	navAnimate();
	animatePostSlides('1', false);
	animateHomePageContainerHeight('post', '1');
}

//When DOM is ready initialize the page
$(document).ready(function(){initHome();});
