var jobGroups;
var jobGroupCount;
var jobImgs;
var jobImgCount;
var jobGroupDirection='next';
var selectedJobGroup;
var allJobGroupImages;
var selectedJobGroupImages;
var selectedJobGroupImageContainer;
var prevWorkGroup;
var nextWorkGroup;
var pageLoadGroup;
var pageLoadState;

// set the src for work images
function setWorkImageSrc(group){
	var firstImage = '#workSlideImg'+group+'-1, #workSlideImgZoom'+group+'-1';
	var selectedJobGroupImages = $('#workSlideImg'+group+' img.workSlideImageScript, #workSlideImgZoom'+group+' img.workSlideImageScript,');	
	var allOtherJobGroupImages;
	allOtherJobGroupImages = jQuery.grep(allJobGroupImages, function(i, v){
			if(jQuery.inArray(i, selectedJobGroupImages) == '-1'){return i;}
		})
	jQuery.each(allOtherJobGroupImages, function(i, v) {																						
			$(v).attr({'src':'../assets/img/common/blank.gif'});
		});
	jQuery.each(selectedJobGroupImages, function(i, v){
			if($(v).attr('src') == '../assets/img/common/blank.gif'){
					$(v).attr('src', $(v).attr('origSrc'));
				}
		});
}
	
// animate the job image container height then release height to allow variable text size
function animateJobImageHeight(selectedJobGroupContainer, img){
	var speed = 750;
	$(img).css({'position':'relative'});
	$(selectedJobGroupContainer).animate({'height': $(img).innerHeight()}, speed, 'easeInQuart',
			function(){
					$(selectedJobGroupContainer).css({'height':''});
					animateWorkPageContainerHeight('job');
				}
		);
}
	
// this moves work client directory 
function animateWorkDir(act, group){
	var selectedJobGroup = '#contentSlide'+group;
	// if displaying the work directory do this
	if(act == 'open'){
			$('#workPageContainer').css({'height': $(selectedJobGroup).innerHeight()});	
			$('.workSlideCopy').css({'height': '800px'});	
			$('#contentWorkContainer').css({'position':'absolute'});	
			//$('#workOpenJobList').css({'display':'none'});
			$(selectedJobGroup).css({'position':'absolute'});
			$(selectedJobGroup).animate({left: '-809px'}, 500, 'easeOutQuart');
			$('#workJobListContainer')
					.css({'position':'relative', 'display':''})
					.animate({top:'0px'}, 500, 'easeOutCubic', function(){animateWorkPageContainerHeight('dir');
						});				
			// set the image srcs to blank
			allJobGroupImages.attr({src:'../assets/img/common/blank.gif'});
		}
	// if hiding the work directory do this
	if(act == 'close'){
			$('#workPageContainer').css({'height':$('#workPageContainer').innerHeight()});
			$('.workSlideCopy').css({'height': '850px'});	
			$('#contentWorkContainer').css({'position':'absolute'});
			$(selectedJobGroup).css({'zIndex':'5200', 'left':'809px', 'position':'absolute'});
			$('#workJobListContainer').css({'position':'absolute'})
					.animate({top: $('#workPageContainer').innerHeight()+20}, 500, 'easeOutQuart', function(){
								$('#workJobListContainer').css({'display':'none'}); animateJobSlides(group, false);
								$('#contentWorkContainer').css({'position':'relative'});
						});
		}
}

$(document).ready(function(){
//Set default open/close settings
	$('.acc_container').hide(); //Hide/close all containers
	//$('.acc_trigger:first').addClass('active').next().show(); //Add "active" class to first trigger, then show/open the immediate next container
	$('.acc_trigger').click(function(){
		if( $(this).next().is(':hidden') ) { //If immediate next container is closed...
			$('.acc_trigger').removeClass('active').next().slideUp(); //Remove all .acc_trigger classes and slide up the immediate next container
			$(this).toggleClass('active').next().slideDown(); //Add .acc_trigger class to clicked trigger and slide down the immediate next container
		} else {
			$(this).toggleClass('active').next().slideUp();
		}
		return false; //Prevent the browser jump to the link anchor
	});
});

function workButtonHover() {
	var workButton = '#workJobList a.workJobLink';
	$(workButton).children('.workJobLinkBg').css({'opacity':'0'});
	$(workButton).hover(function() { 	
		$(this).children('.workJobLinkBg').stop(true, true).animate({'opacity':'1'}, 500);
		$(this).children('.jobType, .jobName').stop(true, true).hide();
		 }, function() {
		$(this).children('.workJobLinkBg').stop(true, true).animate({'opacity':'0'}, 500);	
		$(this).children('.jobType, .jobName').stop(true, true).fadeIn('500');					
	});
}

// set the direction of content animation and call animation
function animateJobSlideNext(group){
	var g=group;
	jobGroupDirection = 'next';
	animateJobSlides(g, false);
}

// set the direction of content animation and call animation
function animateJobSlidePrev(group){
	var g=group;
	jobGroupDirection = 'prev';
	animateJobSlides(g, false);
}

// job switch animation
function animateJobSlides(group, init){
	// check if the work directory is open
	if($('#workJobListContainer').css('display')!='none' && init == false){animateWorkDir('close', group);}
	else {
		// this is used to detect if an animation is currently running and prevent jumping of the page
		$('#workPageContainer').stop(false, false);
		
		if($('#workPageContainer').attr('style').match('height') == null){
				$('#workPageContainer').css({'height':'850px'});
			}
		else{
				$('#workPageContainer').css({'height':'850px'});
			}
		$('#workPageContainer').queue("fx", []);
		setOpenCloseLinks(group);
		if(init == true){
			setOpenCloseLinks('1');
			setPrevNextLinks('0');
			}
		else{
			setPrevNextLinks(group);
			setOpenCloseLinks(group);
			}				
		selectedJobGroup = '#contentSlide'+group;
		setWorkImageSrc(group);		
		var speed = 700;
		var ease = 'easeOutQuart';
		jobGroups.css({'position':'absolute'});
		// show the clicked slide
		$(selectedJobGroup).queue("fx", []);
		$(nextWorkGroup).queue("fx", []);
		$(prevWorkGroup).queue("fx", []);
		if(init == false){$(selectedJobGroup).css({'position':'relative', 'zIndex':'5200'});}
		else{$(selectedJobGroup).css({'zIndex':'5200', 'left':'0px'});}
		if(jobGroupDirection=='next'){	
			$('#contentSlide'+nextWorkGroup).css({'zIndex':'5050'});
			$('#contentSlide'+nextWorkGroup).animate({left: '809px'}, 0);
			$('#contentSlide'+prevWorkGroup).animate({left: '-809px'}, speed-100, ease);
			}
		if(jobGroupDirection=='prev'){	
			$('#contentSlide'+prevWorkGroup).css({'zIndex':'5050'});
			$('#contentSlide'+prevWorkGroup).animate({left: '-809px'}, 0);
			$('#contentSlide'+nextWorkGroup).animate({left: '809px'}, speed-100, ease);
			}
		if(init == false){if($(selectedJobGroup).attr('left')!='0px'){$(selectedJobGroup).animate({left: '0px'}, speed, ease, animateJobImages('1', group));}}
		jQuery.each(jobGroups, function(i) {												 
			var iGroup = ((i)+1);
			var thisGroup = '#contentSlide'+iGroup;
			if(thisGroup != selectedJobGroup){ 
					if((iGroup!=prevWorkGroup && iGroup!=nextWorkGroup)){
						$(thisGroup).css({'zIndex':'5050'});
						$(thisGroup).css({'left': '-809px'});
						}
				}
			});

		}
}

// work slide img animation
function animateJobImages(img, group){
	var selectedJobGroupImageContainer = '#workSlideImg'+group;
	var selectedGroupJobImage = selectedJobGroupImageContainer+'-'+img;
	var divLink = '#workLink'+group+'-'+img;
	var speed = 750;
	var displayedSlideImg = $(".workSlideImageScript[name='"+group+"']");
	var selectedJobGroupImages = $(selectedJobGroupImageContainer+' > img');
	// this is used to detect if an animation is currently running and prevent jumping of the page
	$('#workPageContainer').stop(false, false);
	if($('#workPageContainer').attr('style').match('height') == null){
			$('#workPageContainer').css({'height':$('#workPageContainer').innerHeight()});
		}
	else{
			$('#workPageContainer').css({'height':$('#workPageContainer').css('height')});
		}
	$('#workPageContainer').queue("fx", []);
	//$(selectedJobGroupImageContainer).css({'height':$(displayedSlideImg).innerHeight()});
	// collect all the images & hide any that are visable
	jQuery.each(selectedJobGroupImages, function(i) {												 
		var thisImg = '#workSlideImg'+group+'-'+(i+1);
		var thisLink = '#workLink'+group+'-'+(i+1);
		$(thisImg).queue("fx", []);
		$(thisImg).css({'position':'absolute'});
		$(thisImg).attr({'name':'N'});
		if(thisImg != selectedGroupJobImage){ 
			$(thisImg).animate({top:'300px',left:'0'}, speed-100, 'easeInOutBack');
		}
	});	
	// show the clicked image
	$(selectedGroupJobImage).attr({'name':group});
	$(selectedGroupJobImage).animate({top:'30px',left:'0'}, speed-100, 'easeInOutBack');
	
	//control animations
	var imageSelect = '.imageZoom a'
	$(imageSelect).click(function() { 
		$(this).stop().animate({'height':'30px','width':'42px'}).css({'background-color':'#718401', 'color':'#fff'});
		$(this).children('p').show();
		$(this).siblings().stop().animate({'height':'14px','width':'14px'}).css({'background-color':'', 'color':''});
		$(this).siblings().children('p').hide(); 
	});
}

// work slide img animation zoom
function animateJobImagesZoom(img, group){
	var selectedJobGroupImageContainer = '#workSlideImgZoom'+group;
	var selectedGroupJobImage = selectedJobGroupImageContainer+'-'+img;
	var divLink = '#workLinkZoom'+group+'-'+img;
	var speed = 750;
	var selectedJobGroupImages = selectedJobGroupImageContainer+' img';
	// this is used to detect if an animation is currently running and prevent jumping of the page
	$('#workPageContainer').stop(false, false);
	$('#workPageContainer').queue("fx", []);
	// collect all the images & hide any that are visable
	jQuery.each(selectedJobGroupImages, function(i) {												 
		var thisImg = '#workSlideImgZoom'+group+'-'+(i);
		var thisLink = '#workLinkZoom'+group+'-'+(i);
		$(thisImg).queue("fx", []);
		$(thisImg).css({'position':'absolute'});
		$(thisLink).css({'color':''});
		if((thisImg != selectedGroupJobImage)){ 
			$(thisImg).stop(true, true).fadeOut(speed-100);
		}
	});	
	// show the clicked image
	$(divLink).css({'color':'#fff'});
	$(selectedGroupJobImage).stop(true, true).fadeIn(speed);
}

// next prev for image?
function setPrevNextImgs(img, group){
	prevImgGroup = img;
	nextImgGroup = img;
	var selectedJobGroupImageContainer = '#workSlideImgZoom'+group;
	var prevImgGroup = selectedJobGroupImageContainer+' img'- 1;
	var nextImgGroup = selectedJobGroupImageContainer+' img'+ 1;
	if(img > 1){prevImgGroup = (--prevImgGroup);} else {prevImgGroup = jobGroupCount;} 
	if(img < jobGroupCount){nextImgGroup = (++nextImgGroup);} else {nextImgGroup = 1;} 
	$('.modalPrevious a').unbind('click');
	$('.modalNext a').unbind('click');
	$('.modalPrevious a').bind('click', function(){animateJobImagesZoom(prevImgGroup); return false;});
	$('.modalNext a').bind('click', function(){animateJobImagesZoom(nextImgGroup); return false;});
}

//modal for zoomed images
function enlargeImage(img, group) {	
	var masks = '#mask, #maskInside'+group;
	$(masks).css({opacity:'0'});
	var maskHeight = $(document).height();
	var maskWidth = $(window).width();
	$('p[title=Zoom Image]').click(function(e) {
		e.preventDefault();
		var thisIndex = $('p').index(this);
		var id = $(this).attr('class');
		var maskHeight = $(document).height();
		var maskWidth = $(window).width();
		$('.workLinkZoom').css({'color':''});
		$('#mask').css({'width':maskWidth,'height':maskHeight,display:'block'});
		$('.maskInside').css({'height':maskHeight,display:'block'});
		$(masks).animate({opacity:'0.9'}, 500);	
		$(id).fadeIn(2000); 	
		$('.window').stop(true, true).fadeIn(1500);
		$('#' + id).stop(true, true).show(); 
		$('#workLinkZoom'+thisIndex+'-'+group).css({'color':'#fff'});
		$('#navPrevNextContainer').fadeOut(400);
		var newSrc = $('img#footerRibbonL').attr('src').replace('orig', 'dark');
		$('img#footerRibbonL').attr("src", newSrc);
		var newSrc = $('img#footerRibbonR').attr('src').replace('orig', 'dark');
		$('img#footerRibbonR').attr("src", newSrc);
	});
	$('p[title=Zoom Image]').hover(function(e) {
		$(this).css({color:'#ffffff'}) 
			}, function () {  
		$(this).css({color:'#dbeb4c'}) 								  
	});
	$('.close, #mask, .maskInside').click(function (e) {
		e.preventDefault();
		$('#mask, .maskInside, .window').hide();
		$('#navPrevNextContainer').show();
		$('.workSlideImageZoom').hide();
		var newSrc = $('img#footerRibbonL').attr('src').replace('dark', 'orig');
		$('img#footerRibbonL').attr("src", newSrc);
		var newSrc = $('img#footerRibbonR').attr('src').replace('dark', 'orig');
		$('img#footerRibbonR').attr("src", newSrc);
	});	
}

// hover for expertise links
function linkHover() {
	if (jQuery.browser.msie) { // show the hover in ie without fade
		$('.expertiseList ul li a').hover(function() { 
		$('div.expertiseList div.active').hide().stop(true, true);
		$(this).siblings('div.active').show();
	}, function() { 
		$(this).siblings('div.active').hide();
	});
	} else { // if not ie, fade the hover
		$('.expertiseList ul li a').hover(function() { 
		$('div.expertiseList div.active').hide().stop(true, true);
		$(this).siblings('div.active').fadeIn(500);
	}, function() { 
		$(this).siblings('div.active').fadeOut(300);
	}); }
}
	
// animate the content area height then release height to allow variable text size
function animateWorkPageContainerHeight(container){
	$('#workPageContainer').queue("fx", []);
	var finalHeight;
	var speed;
	var ease;
	if(container == 'dir'){finalHeight = $('#workJobListContainer').innerHeight(); speed=300; ease='easeOutBack';}
	if(container == 'job'){finalHeight = $('#contentWorkContainer').innerHeight(); speed=700; ease='easeInOutBack';}
	if(finalHeight == 0){finalHeight = 550};
	$('#workPageContainer').animate({'height': finalHeight+'px'}, speed, ease);
	$('.workSlideCopy').animate({'height': '850px'}, speed, ease);
	$('#workPageContainer').queue("fx", function(){$('#workPageContainer').css({'height':''});});
}

// set the open directory and close directory links after content is changed
function setOpenCloseLinks(group){
	$('#workCloseJobList a').unbind('click');
	$('#workOpenJobList a').unbind('click');
	$('#workCloseJobList a').bind('click', function(){animateWorkDir('close', group); return false;});
	$('#workOpenJobList a').bind('click', function(){animateWorkDir('open', group); return false;});
}

// set the previous and next links after content is changed
function setPrevNextLinks(group){
	prevWorkGroup = group;
	nextWorkGroup = group;
	var numJobs = $('.workJobLink').size();
	if(group > 1){prevWorkGroup = (--prevWorkGroup);} else {prevWorkGroup = numJobs;} 
	if(group < numJobs){nextWorkGroup = (++nextWorkGroup);} else {nextWorkGroup = 1;} 
	$('#navPrev a').unbind('click');
	$('#navNext a').unbind('click');
	$('#navPrev a').bind('click', function(){animateJobSlidePrev(prevWorkGroup); return false;});
	$('#navNext a').bind('click', function(){animateJobSlideNext(nextWorkGroup); return false;});
}

// initialize the page
function initWork(){
	// set the work slides to the left
	$('#contentWorkContainer').css({'position':'absolute'});
	$('.imageZoom a:first-child').css({'height':'30px','width':'42px'}).css({'background-color':'#718401', 'color':'#fff'});
	$('.imageZoom a:first-child').children('p').text('+ enlarge');
	jobGroups = $('.contentWork')
	jobGroupCount = jobGroups.length;
	jobGroups.css({'left':'-809px'}).css({'position':'absolute'});
	jobImgs = $('.workSlideImageZoom')
	jobImgCount = jobImgs.length;
	workButtonHover();
	linkHover();
	enlargeImage();
	// hide the work slide images
	allJobGroupImages = $('.workSlideImg img')
	$('#navPrev').bind('mouseover', function(){navMouseAnimate('#navPrev','-90px');});
	$('#navPrev').bind('mouseout', function(){navMouseAnimate('#navPrev','-100px');});
	$('#navNext').bind('mouseover', function(){navMouseAnimate('#navNext','760px');});
	$('#navNext').bind('mouseout', function(){navMouseAnimate('#navNext','770px');});
	animateJobSlides(pageLoadGroup, pageLoadState);
	if(pageLoadGroup == 0){animateWorkPageContainerHeight('dir');}
	else{animateWorkDir('close', pageLoadGroup);}
}

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