jQuery.easing.def = 'easeOutCubic';
var speed = 600;

var browserHeight = $(window).height();
var browserWidth = $(window).width();
var browserMiddle = ($(window).width())/2;

var maxHeight = $(window).height()-135;
var maxWidth = $(window).width()*.5;
var current = 0;

var lastWidth = 0;
var nextWidth = 0;
var storedW = 0;

var standardFontAdj = 560;

readyFunctions();

function refreshFunctions() {
	$(document).ready(function() {
		$('.scroll').jScrollPane();
	});
}

function readyFunctions() {
	$(document).ready(function() {
							   
		if($('table.images tr td').size() < 2) {
			$('.leftNav').hide();
			$('.rightNav').hide();
		}
		
		
		$('.page-item-378').click(function() {
			$(this).find('a').attr('target','_blank');								   
		});
		
		var homeLeftW = browserWidth - 600;
		$('.home-left').css('width',homeLeftW);
		
		$('.learn-more').click(function() {
			var clickedElement = $(this).attr('id');
			
			if($(this).text() == 'Learn More >>') {
				$(this).text('X Close');
			} else {
				$(this).text('Learn More >>');	
			};
			
			$('.'+clickedElement).slideToggle();
			return false;
		});
		
		$('.portfolio-images').css('marginTop',70);
		
		$('.images').attr('height',maxHeight);
		
		$('.innerleft_panel a').click(function() {
			//return false;
		});
		
		$('.images td DIV img').click(function() {
			imgCLICK(this);							   
		});
		
		$('.leftNav').css('top',(browserHeight/2)-26);
		$('.rightNav').css('top',(browserHeight/2)-26);
		
		$('.leftNav').click(function() {
			moveImages('left');						 
		});
		$('.rightNav').click(function() {
			moveImages('right');						 
		});
		$('.rightNav').hide();
		$('.leftNav').hide();
		
		$('.close-toggle').click(function() {
			toggleClose();
			return false;								  
		});
		
		var menuContainerHeight = $('.experience-wrapper').height() + $('.work-wrapper').height();
		var adjustedMenuHeight = (browserHeight - $('#guest_acc').height()) - menuContainerHeight;
		
		$('.client-wrapper').css('height',adjustedMenuHeight - 105);
		refreshFunctions();
		
		
		$('.images td span').click(function() {
			var clickedID = $('.images td:eq('+$(this).find('a').attr('class')+')');
			var imgElement = $('.images td:eq('+$(this).find('a').attr('class')+') DIV');
			var oldH = imgElement.find('img').attr('height');
			storedW = imgElement.find('img').attr('width');
			$('.images td:eq('+$(this).find('a').attr('class')+') span').removeClass('current-thumb');
			$(this).addClass('current-thumb');
			
			
			imgElement.append('<img class="thumbs" style="display:none;" id="'
							  +$('.images td:eq('+$(this).find('a').attr('class')+') DIV img').attr('id')+'" src="'+$(this).find('a').attr('href')+'" />');
			
			imgElement.find('img:visible').fadeOut(function() {
				$(this).remove();
				browserHeight = $(window).height();
				browserWidth = $(window).width();
				browserMiddle = browserWidth/2;
				
				maxHeight = $(window).height()-190;
				
				var adjDIM = maxHeight/imgElement.find('img').attr('height');
				var newH = imgElement.find('img:hidden').attr('height')*adjDIM;
				var newW = imgElement.find('img:hidden').attr('width')*adjDIM;
					
				imgElement.find('img:hidden').attr('height',newH);
				imgElement.find('img:hidden').attr('width',newW);
				if((newW > lastWidth) || (newH > oldH)) { checkSize(clickedID) };
				
				
				lastWidth = newW;
				imgElement.find('img').fadeIn(speed);
			})
	
			return false;
		});
		
		$(document).keydown(function(event) {
			if(event.keyCode == 39) {
				moveImages('right');	
			} else if(event.keyCode == 37) {
				moveImages('left');	
			}
		});
	});
};
$(window).load(
		function() {
			var i = 0;
			
			$('.images td').each(function() {
				if($(this).find('DIV').hasClass('video')) {
					var video = true;	
					var theELEMENT = 'DIV iframe';
				} else {
				    var video = false;	
					var theELEMENT = 'DIV img';
				}
				
				
				maxWidth = $(window).width()*.5;
				if($(this).find(theELEMENT).hasClass('thumbs')) {
					maxHeight = $(window).height()-190;
				} else if($(this).find('DIV:first').hasClass('text')) {
					maxHeight = $(window).height() - 180;
				} else {
					maxHeight = $(window).height()-135;	
				}
				
				var adjDIM = maxHeight/$(this).find(theELEMENT).attr('height');
				var newH = $(this).find(theELEMENT).attr('height')*adjDIM;
				var newW = $(this).find(theELEMENT).attr('width')*adjDIM;
				
				
				if(video) {
					$(this).find(theELEMENT).attr('height',newH);
					$(this).find(theELEMENT).attr('width',newW);
				}
				
					
				if(i == current) {
					$(this).find(theELEMENT).attr('height',newH);
					$(this).find(theELEMENT).attr('width',newW);
					lastWidth = newW;
					storedW = newW;
					
					if($('.images td:eq('+current+') DIV img').hasClass('thumbs')) { 
						$('.thumb-preloader').html('');
						$('.images td:eq('+current+') span').each(function() {
							var newIMG = "<img src='"+$(this).find('a').attr('href')+"' />";
							$('.thumb-preloader').append(newIMG);
						});
					}
				} else {
					if(newW > maxWidth || newH > maxHeight) {
						checkSize(this);
					} else {
						$(this).find(theELEMENT).attr('height',newH*.8);
						$(this).find(theELEMENT).attr('width',newW*.8);
					}
					$(this).animate({
						opacity: 0.25
					}, 0);
					if(i == current + 1) {
						nextWidth = newW;	
					}
				}
				  
				checkSize(this);
				
				if($(this).find('DIV:first').hasClass('text')) {
					var setTextW = $(this).find(theELEMENT).attr('width');
					var setTextH = $(this).find(theELEMENT).attr('height');
					/*if(standardFontAdj < setTextW) {
						adjFont = setTextW/standardFontAdj;
						$(this).find('.cat-desc-padding').css('fontSize',14*adjFont);
					} else if(standardFontAdj > setTextW) {
						adjFont = standardFontAdj/setTextW;
						$(this).find('.cat-desc-padding').css('fontSize',14/adjFont);	
					}*/
					$(this).find('.cat-desc-padding').css('width',setTextW-40);
					$(this).find('.cat-desc-main').css('height',(setTextH-40)/2);
					$(this).find('.cat-experience').css('width',(setTextW-40)/1.75);
					$(this).find('.cat-experience').css('height',(setTextH-100)/2);
					$(this).find('.cat-experience').css('marginLeft',(setTextW-120)/3);
				}
				
				  i++;
			});
			
			alignImages();
			
			var nextIMG_MIDDLE = (storedW - 222)/2;
			var move = (browserMiddle - nextIMG_MIDDLE);
		
			$('.portfolio-images').css("left",move);
		
			setTimeout(function() { 
					$('#loader').fadeOut(function() {
						$('#images').css('visibility','visible');
						$('#images').hide();
						$('#images').fadeIn(1500);
						$('.leftNav').fadeIn(1500);
						$('.rightNav').fadeIn(1500);
					});
			},500);
			
		}
	);

function checkSize(element) {
	
	if($(element).find('DIV').hasClass('video')) {
		var video = true;	
		var theELEMENT = 'DIV iframe';
	} else {
		var video = false;	
		var theELEMENT = 'DIV img';
	}
	
	if($(element).find(theELEMENT).hasClass('thumbs')) {
		maxHeight = $(window).height()-190;
	} else {
		maxHeight = $(window).height()-135;	
	}
	
	if($(element).find(theELEMENT).attr('width') > maxWidth) {
		if($(element).find(theELEMENT).attr('id') != current) {
			var adjDIM = (maxWidth/$(element).find(theELEMENT).attr('width'))*.8;
		} else {
			var adjDIM = maxWidth/$(element).find(theELEMENT).attr('width');
		}
		var adjW = $(element).find(theELEMENT).attr('width')*adjDIM;
		var adjH = $(element).find(theELEMENT).attr('height')*adjDIM;
		$(element).find(theELEMENT).attr('width',adjW);
		$(element).find(theELEMENT).attr('height',adjH);
		
		if($(element).find(theELEMENT).attr('id') == current) {
			storedW = adjW;	
		}
	}
	
	if($(element).find(theELEMENT).attr('height') > maxHeight) {
				
		if($(element).find(theELEMENT).attr('id') != current) {
			var adjDIM = (maxHeight/$(element).find(theELEMENT).css('height'))*.8;
		} else {
			var adjDIM = maxHeight/$(element).find(theELEMENT).attr('height');
		}
		var adjW = $(element).find(theELEMENT).attr('width')*adjDIM;
		var adjH = $(element).find(theELEMENT).attr('height')*adjDIM;
		$(element).find(theELEMENT).attr('width',adjW);
		$(element).find(theELEMENT).attr('height',adjH);
		
		if($(element).find(theELEMENT).attr('id') == current) {
			storedW = adjW;	
		}
	}	
	alignImages();
	refreshFunctions();
}

function imgCLICK(element) {
	if($(element).attr('id') > current) {
		moveImages('right');
	} else if($(element).attr('id') < current) {
		moveImages('left');
	}
}

function moveImages(direction) {
	$('.portfolio-images').stop(true,true);
	$('.images .current').stop(true,true);
	
	if(direction == "left") {
		var nextIMG = current - 1;	
	} else {
		var nextIMG = current + 1;	
	}
	
	if($('.images .current DIV').hasClass('video')) {
		var video = true;	
		var theCurrent_ELEMENT = 'DIV iframe';
		$('.images .current DIV iframe').attr('src',$('.images .current DIV iframe').attr('src'));
	} else {
		var video = false;	
		var theCurrent_ELEMENT = 'DIV img';
	}
	
	if($('.images td:eq('+nextIMG+') DIV').hasClass('video')) {
		var video = true;	
		var theNext_ELEMENT = 'DIV iframe';
	} else {
		var video = false;	
		var theNext_ELEMENT = 'DIV img';
	}
	
	var currWidth = parseFloat($('.images .current').find(theCurrent_ELEMENT).css('width'));	
	var currHeight = parseFloat($('.images .current').find(theCurrent_ELEMENT).css('height'));	

	var nextWidth = parseFloat($('.images td:eq('+nextIMG+')').find(theNext_ELEMENT).css('width'));	
	var nextHeight = parseFloat($('.images td:eq('+nextIMG+')').find(theNext_ELEMENT).css('height'));	
	
	var adjustSize = (currWidth/.8) - currWidth;

	var position = $('.images td:eq('+nextIMG+')').position();
	var nextIMG_MIDDLE = parseFloat($('.images td:eq('+nextIMG+')').css('width'))/2;
	
	if($('.images td:eq('+nextIMG+')').find(theNext_ELEMENT).hasClass('thumbs')) { 
		$('.thumb-preloader').html('');
		$('.images td:eq('+nextIMG+') span').each(function() {
			var newIMG = "<img src='"+$(this).find('a').attr('href')+"' />";
			$('.thumb-preloader').append(newIMG);
		});
	}
	
	if(direction == "left") {
		var move = ((browserMiddle-position.left) - nextIMG_MIDDLE);
	} else {
		var move = ((browserMiddle-position.left) - nextIMG_MIDDLE) +adjustSize;	
	}
	
	lastWidth = nextWidth/.8;

	
	$('.images .current').find(theCurrent_ELEMENT).animate({
		width: currWidth*.8,
		height: currHeight*.8
	}, speed);
	
	$('.images .current').animate({ opacity: 0.25 }, speed);
	
	if($('.images .current').find('DIV').hasClass('text')) {
		$('.images .current').find('.cat-desc-padding').animate({
			width: (currWidth*.8)-40,
			height: (currHeight*.8)-40																  
		},speed);
		
		
		
		$('.images .current').find('.cat-desc-main').animate({
					height: ((currHeight*.8)-40)/2
		},speed);
		
		$('.images .current').find('.cat-experience').animate({
					width: ((currWidth*.8)-40)/1.75,
					height: ((currHeight*.8)-100)/2
		},speed);
		
		$('.images .current').find('.cat-experience').css('marginLeft',((currWidth*.8)-120)/3);
		
	}
	
	if($('.images td:eq('+nextIMG+')').find('DIV').hasClass('text')) {
		var nextTextW = (nextWidth/.8);
		var nextTextH = (nextHeight/.8);
		
		$('.images td:eq('+nextIMG+')').find('.cat-desc-padding').animate({
			width: nextTextW-40,
			height: nextTextH-40																  
		},speed);
		
		$('.images td:eq('+nextIMG+')').find('.cat-desc-main').animate({
					height: (nextTextH-40)/2
		},speed);
		
		$('.images td:eq('+nextIMG+')').find('.cat-experience').animate({
					width: nextTextW/1.75,
					height: (nextTextH-100)/2
		},speed);
		
		$('.images td:eq('+nextIMG+')').find('.cat-experience').css('marginLeft',(nextTextW-120)/3);
		
	}
	
	$('.images td:eq('+nextIMG+')').animate({ opacity: 1 }, speed);
	
	$('.images td:eq('+nextIMG+')').find(theNext_ELEMENT).animate({
		width: nextWidth/.8,
		height:nextHeight/.8
	}, speed);

	$('.portfolio-images').animate({
		left: move + 30  
	},speed);
	
	
	$('.images td:eq('+current+')').removeClass('current');
	$('.images td:eq('+nextIMG+')').addClass('current');
	
	current = nextIMG;
	refreshFunctions();
	
}


function alignImages() {
	
	if($('.images td:eq('+current+')').find('DIV').hasClass('video')) {
		var video = true;	
		var theELEMENT = 'DIV iframe';
	} else {
		var video = false;	
		var theELEMENT = 'DIV img:visible';
	}
	
	var currWidth = $('.images td:eq('+current+')').find(theELEMENT).attr('width');	
	var currHeight = $('.images td:eq('+current+')').find(theELEMENT).attr('height');
	
	if (currWidth != storedW) {
		if(currWidth > storedW) {
			var adjDIF = -(currWidth - storedW);
		} else if (currWidth < storedW) {
			var adjDIF = storedW - currWidth;
		}
		
		$('.portfolio-images').stop(true,true);
		$('.images .current').stop(true,true);
	
		var position = $('.images td:eq('+current+')').position();
		var nextIMG_MIDDLE = (currWidth - 222)/2;
		
		var move = ((browserMiddle-position.left) - nextIMG_MIDDLE);
		
		lastWidth = nextWidth/.8;
	
		$('.portfolio-images').animate({
			left: move  
		},speed);
	}
	
	refreshFunctions();
	
}

function toggleClose() {
	$('#fs-contact').toggle();
}


$(window).resize(function() {
	browserHeight = $(window).height();
	browserWidth = $(window).width();
	browserMiddle = ($(window).width())/2;
	
	maxHeight = $(window).height()-135;
	maxWidth = $(window).width()*.5;
	
	var i=0;
	
	var homeLeftW = browserWidth - 600;
	$('.home-left').css('width',homeLeftW);
	
	$('.images td').each(function() {
		if($(this).find('DIV img').hasClass('thumbs')) {
			maxHeight = maxHeight - 55;
		} else {
			maxHeight = $(window).height()-135;	
		}
		
		var adjDIM = maxHeight/$(this).find('DIV img').attr('height');
		var newH = $(this).find('DIV img').attr('height')*adjDIM;
		var newW = $(this).find('DIV img').attr('width')*adjDIM;
			
		if($(this).hasClass('current')) {
			$(this).find('DIV img').attr('height',newH);
			$(this).find('DIV img').attr('width',newW);
			lastWidth = newW;
			checkSize(this);
		} else {
			$(this).find('DIV img').attr('height',newH*.8);
			$(this).find('DIV img').attr('width',newW*.8);
			$(this).animate({
				opacity: 0.25
			}, 0);
			checkSize(this);
			if(i == current + 1) {
				nextWidth = newW;	
			}
			checkSize(this);
		}
		  i++;
	});
	
	var menuContainerHeight = $('.experience-wrapper').height() + $('.work-wrapper').height();
	var adjustedMenuHeight = (browserHeight - $('#guest_acc').height()) - menuContainerHeight;
	
	//$('#client_leftpanel .jScrollPaneContainer').css('height',adjustedMenuHeight - 105);
	//refreshFunctions();
	
	$('.images').attr('height',maxHeight);
	$('.leftNav').css('top',(browserHeight/2)-26);
	$('.rightNav').css('top',(browserHeight/2)-26);
	
	alignImages()
});
