// JavaScript Document
$(document).ready(function(){
	var imgHeight;
	var divHeight = $('.team-gill-image').css('height').replace('px','');
	var divWidth = $('.team-gill-image').css('width').replace('px','');
	var colWidth = $('.team-gill-column').css('width').replace('px','');
	var numLow = 0;
	var numHigh = 20;
	var adjustedHigh = (parseFloat(numHigh) - parseFloat(numLow)) + 1;
	var numRand;
	var numRand2;
	var imClicked = 0;
	var newDivWidth;
	var newImageWidth;
	var newImageHeight;
	var newImageRatio;
	var newTopPos= 50;
	var teamGillLeft = 0;
	
	$('.team-gill-image').each(function() {
		numRand = Math.floor(Math.random()*adjustedHigh) + parseFloat(numLow);
		numRand2 = Math.floor(Math.random()*adjustedHigh) + parseFloat(numLow);
		imgHeight = $(this).children('a').children('img').css('height').replace('px','');
		$(this).children('a').children('img').css({
			'margin-top':((divHeight-imgHeight-6)/2)+'px'
	  	});
		$(this).css({
			'left':((colWidth-divWidth-6)/2)+numRand+'px',
			'top': numRand2+50 +'px',
			'z-index':numRand2
		});
	});
	
	$('#gill-race-team, #sponsorships').mouseenter(function(){
		if(imClicked!=1){
			numLow = -100;
			numHigh = 100;
			adjustedHigh = (parseFloat(numHigh) - parseFloat(numLow)) + 1;
			$('.team-gill-image',this).each(function() {
				numRand = Math.floor(Math.random()*adjustedHigh) + parseFloat(numLow);
				numRand2 = Math.floor(Math.random()*adjustedHigh) + parseFloat(numLow);
				$(this).stop().animate({
					'top': numRand+50 +'px',
					'left': numRand2 +'px'
				});
			});
		}
	}).mouseleave(function(){
		if(imClicked!=1){
			numLow = 0;
			numHigh = 20;
			adjustedHigh = (parseFloat(numHigh) - parseFloat(numLow)) + 1;
			$('.team-gill-image',this).each(function() {
				numRand = Math.floor(Math.random()*adjustedHigh) + parseFloat(numLow);
				numRand2 = Math.floor(Math.random()*adjustedHigh) + parseFloat(numLow);
				imgHeight = $(this).children('a').children('img').css('height').replace('px','');
				$(this).stop().animate({
					'left':((colWidth-divWidth-6)/2)+numRand+'px',
					'top': numRand2+50 +'px'
				});
			});
		}
	}).click(function(){
		if(imClicked==1){
		} else {
			$('a.team-gill-back').fadeIn();
			imClicked = 1;
			$(this).addClass('showThumbs');
			/*$('.team-gill-column:not(".showThumbs")').animate({'width':'0%'},50,function(){
					$('.team-gill-column:not(".showThumbs")').hide();
			});*/
			$(this).css({'height':'700px'});
			$('.team-gill-column:not(".showThumbs")').hide();
			if($('#sponsorships').hasClass('showThumbs')){
				$('#sponsorships').css({
					'position':'absolute',
					'top':'80px',
					'left':'50%'
				});
				$('#sponsorships').animate({
					'left':'0px',
					'width':'100%'
				});
			}
			if($('#gill-race-team').hasClass('showThumbs')){
				$('#gill-race-team').css({
					'position':'absolute',
					'top':'80px',
					'left':'0%'
				});
				$('#gill-race-team').animate({
					'left':'0px',
					'width':'100%'
				});
			}
			
			
			
			//$(this).css({'width':'100%'});
			newDivWidth = 0;
			newTopPos= 50;
			$('.team-gill-image',this).each(function() {
				$(this).animate({
					'width':'163px',
					'height':'163px',
					'left':newDivWidth+'px',
					'top':newTopPos+'px'
				});
				
				newImageWidth = $(this).children('a').children('img').css('width').replace('px','');
				newImageWidth = parseFloat(newImageWidth);
				newImageHeight = $(this).children('a').children('img').css('height').replace('px','');
				newImageHeight = parseFloat(newImageHeight);
				newImageRatio = newImageWidth / newImageHeight;
				
				if (newImageWidth>newImageHeight){
					newImageWidth = 157;
					newImageHeight = newImageWidth / newImageRatio;
				} else {
					newImageHeight = 157;
					newImageWidth = newImageHeight * newImageRatio;
				}
				
				//var imageHeight=$(this).children('a').children('img').css('height').replace('px','');
				$(this).children('a').children('img').animate({
					'width':newImageWidth+'px',
					'height':newImageHeight+'px',
					'marginTop':((163-newImageHeight-6)/2)+'px'
				}/*,function(){
					var imageHeight=$(this).css('height').replace('px','');
					$(this).css({
						'margin-top':((163-imageHeight-6)/2)+'px'
					});
				}*/);
				
				if(newDivWidth+newDivWidth>770) {
					newTopPos = newTopPos+188;
					newDivWidth = 0;
				} else {
					newDivWidth = newDivWidth+188;
				}
				$('#right-column').css({'height':newTopPos+(188*2)+'px'});
				$(this).css({'height':newTopPos+(188*2)+'px'});
				$('.team-gill-column-title').css({'text-align':'left'})
			})
			return false;
		}
	});
	
	$('a.team-gill-back').click(function(){
		$('a.team-gill-back').fadeOut();
		if($('#sponsorships').hasClass('showThumbs')){
				$('#sponsorships').animate({
					'top':'80px',
					'left':'50%',
					'width':'50%'
				},function(){
					$('.team-gill-column:not(".showThumbs")').fadeIn();
				});
				$('#gill-race-team').css({
				'position':'absolute',
					'top':'80px',
					'left':'0%',
					'height':'auto'
				})
			}
		if($('#gill-race-team').hasClass('showThumbs')){
			$('#gill-race-team').animate({
				'top':'80px',
				'left':'0%',
				'width':'50%'
			},function(){
				$('.team-gill-column:not(".showThumbs")').fadeIn();
			});
			$('#sponsorships').css({
				'position':'absolute',
				'top':'80px',
				'left':'50%',
				'width':'50%',
				'height':'auto'
			});
		}
		
		numLow = 0;
		numHigh = 20;
		adjustedHigh = (parseFloat(numHigh) - parseFloat(numLow)) + 1;
		$('.showThumbs .team-gill-image').each(function() {
				numRand = Math.floor(Math.random()*adjustedHigh) + parseFloat(numLow);
				numRand2 = Math.floor(Math.random()*adjustedHigh) + parseFloat(numLow);
				imgHeight = $(this).children('a').children('img').css('height').replace('px','');
				$(this).animate({
					'width':'311px',
					'height':'311px',
					'left':((colWidth-divWidth-6)/2)+numRand+'px',
					'top': numRand2+50 +'px'
				});
			
				newImageWidth = $(this).children('a').children('img').css('width').replace('px','');
				newImageWidth = parseFloat(newImageWidth);
				newImageHeight = $(this).children('a').children('img').css('height').replace('px','');
				newImageHeight = parseFloat(newImageHeight);
				newImageRatio = newImageWidth / newImageHeight;
				
				
				if (newImageWidth>newImageHeight && newImageRatio>=1){
						newImageWidth = 305;
						newImageHeight = newImageWidth / newImageRatio;
				} else if (newImageHeight>newImageWidth && newImageRatio<=1) {
					newImageHeight = 305;
					newImageWidth = newImageHeight * newImageRatio;
				} 
				
					//var imageHeight=$(this).children('a').children('img').css('height').replace('px','');
				$(this).children('a').children('img').animate({
					'width':newImageWidth+'px',
					'height':newImageHeight+'px',
					'marginTop':((311-newImageHeight-6)/2)+'px'
				});
					
			});
			$('.showThumbs').removeClass('showThumbs');
		
		$('.team-gill-column-title').css({'text-align':'center'})
		imClicked = 0;

		return false
	});
});
