//<?php header('Content-type: text/javascript'); ?>
//<?php if (substr_count($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip')) ob_start("ob_gzhandler"); else ob_start(); ?>

var postGroups;
var postGroupCount;
var postGroupDirection='next';
var selectedPostGroup;
var prevPostGroup;
var nextPostGroup;
var curLoadingBg;
var timer

function submitForm(formId){$(formId).submit();}
function slideShowTimer(){
		clearTimeout(timer);
		timer=setTimeout('animatePostSlideNext(nextPostGroup)', 10000)
}

// 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;
		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')); 
											}
										/*else {
												if($(v).css('backgroundImage') != 'url(assets/img/common/blank.gif)'){
														$(v).css('backgroundImage', 'url(assets/img/common/blank.gif)');
													}
											}*/
									});
								$(thisEmployeeBg).animate({left:'0'}, 700, function () {
										   $(thisEmployeeBgFade).fadeIn(700).animate({opacity: 1.0}, 8000).fadeOut(700); //fade background gradient in and out
										   $(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}, 8000, function(){ //wait
										   		$(thisEmployeeIEfix).addClass('white', function() { }) }).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')); 
									}
								/*else {
										if($(v).css('backgroundImage') != 'url(../assets/img/common/blank.gif)'){
												$(v).css('backgroundImage', 'url(../assets/img/common/blank.gif)');
											}
									}*/
							});
		}
	}
// moves the employee post backgrounds
function animateEmployeeBg(group){
		var thisEmployeeBg = '#employeeBg'+group;
		var prevEmployeeBg = '#employeeBg'+prevPostGroup;
		var nextEmployeeBg = '#employeeBg'+nextPostGroup;
		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='490px';
		var winWidthPlus = '1005px';
		allEmployeeBg.css({'display':''});

		if(postGroupDirection=='next'){
				$('.employeeBgFade, .contentHome').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{
				$('.employeeBgFade, .contentHome').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();
	}
	
// 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'}, 1200, 'easeOutBounce' );
		$('#navWork').animate({left: '0px'}, 1600, 'easeOutBounce' );
		$('#navExpertise').animate({left: '486px'}, 1800, 'easeOutBounce' );
		$('#navIdeas').animate({left: '606px'}, 1400, 'easeOutBounce' );
}
// 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':'490px'});
	

		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();});