function stretchSidebar()
{
	if(!$('#big-footer').hasClass('open')){
		$("#sidebar").height($(document).height());
	}else{
		var bigfooterheight = $('#big-footer.open').outerHeight();
		var newh = $(document).height() - bigfooterheight;
		$("#sidebar").height(newh);
	}
}

$(document).ready(function() {
	/*init*/
	var bigfooterheight = $('#big-footer').outerHeight();
	$('#big-footer').css({'height':0});
	stretchSidebar();
	
	//masonry goodness
	$('#teamfaces').masonry({ columnWidth: 48 });

	
	/*typekit*/
	try{
		Typekit.load({
			active: function() {
				//stretch that sidebar down *after* typekit has loaded
				stretchSidebar();
				
				//work1 caption also needs to happen after typekit
				//as it has a dynamic height, dependent upon type of course...
				var work1height = $('.work1caption').outerHeight()-50;
				work1height + "px";
				$('.work1caption').css({bottom: -work1height});
				$('.has-caption').hover(function(){
					$(".work1caption", this).stop().animate({bottom: "0px"}, {duration: 500, queue: false});
				}, function() {
					$(".work1caption", this).stop().animate({bottom: -work1height}, {duration: 500, queue: false});
				});
			},
			inactive: function() {
				//fallbacks
				stretchSidebar();
				
				var work1height = $('.work1caption').outerHeight();
				work1height + "px";
				$('.work1caption').css({bottom: -work1height});
				$('.has-caption').hover(function(){
					$(".work1caption", this).stop().animate({bottom: "0px"}, {duration: 500, queue: false});
				}, function() {
					$(".work1caption", this).stop().animate({bottom: -work1height}, {duration: 500, queue: false});
				});
			}
		});
	} catch(e) {}
	
	
	
	//news hover
	var w = $('.news-image img').outerWidth();
	var nw = w-8;
	$('.news').hover(function(){
		$(this).animate({}, {duration: 400, queue: false});
		$('.news-image-mask-t', this).stop().animate({'top' : '0'}, {duration: 400, queue: false});
		$('.news-image-mask-r', this).stop().animate({'right' : '0'}, {duration: 400, queue: false});
		$('.news-image-mask-b', this).stop().animate({'bottom' : '0'}, {duration: 400, queue: false});
		$('.news-image-mask-l', this).stop().animate({'left' : '0'}, {duration: 400, queue: false});
	}, function(){
		$(this).animate({}, {duration: 400, queue: false});
		$('.news-image-mask-t', this).stop().animate({'top' : '-10px'}, {duration: 400, queue: false});
		$('.news-image-mask-r', this).stop().animate({'right' : '-10px'}, {duration: 400, queue: false});
		$('.news-image-mask-b', this).stop().animate({'bottom' : '-10px'}, {duration: 400, queue: false});
		$('.news-image-mask-l', this).stop().animate({'left' : '-10px'}, {duration: 400, queue: false});
	});
	
	
	
	//work2 hover
	var w = $('.work2-image img').outerWidth();
	var nw = w-8;
	$('.work').hover(function(){
		$(this).animate({}, {duration: 400, queue: false});
		$('.work2-image-mask-t', this).stop().animate({'top' : '0'}, {duration: 400, queue: false});
		$('.work2-image-mask-r', this).stop().animate({'right' : '0'}, {duration: 400, queue: false});
		$('.work2-image-mask-b', this).stop().animate({'bottom' : '0'}, {duration: 400, queue: false});
		$('.work2-image-mask-l', this).stop().animate({'left' : '0'}, {duration: 400, queue: false});
	}, function(){
		$(this).animate({}, {duration: 400, queue: false});
		$('.work2-image-mask-t', this).stop().animate({'top' : '-10px'}, {duration: 400, queue: false});
		$('.work2-image-mask-r', this).stop().animate({'right' : '-10px'}, {duration: 400, queue: false});
		$('.work2-image-mask-b', this).stop().animate({'bottom' : '-10px'}, {duration: 400, queue: false});
		$('.work2-image-mask-l', this).stop().animate({'left' : '-10px'}, {duration: 400, queue: false});
	});
	
	
	//team hover functions
	$('.team').hoverIntent(function(){
		$('.team-block', this).fadeIn(300);
	}, function(){
		$('.team-block', this).fadeOut(300);
	});
	$('.team-slide-2').hoverIntent(function(){
		$('.team-block', this).fadeIn(300, function(){
			$(this).animate({left: "0"}, function(){
				$('div', this).fadeIn(300);
			});
		});
	}, function(){
		$('.team-block div', this).fadeOut(300, function(){
			$('.team-slide-2 .team-block').animate({left: "96px"}, function(){
				$(this).fadeOut(300);
			});
		});
	});
	$('.team-slide-4').hoverIntent(function(){
		$('.team-block', this).fadeIn(300, function(){
			$(this).animate({left: "0"}, function(){
				$('div', this).fadeIn(300);
			});
		});
	}, function(){
		$('.team-block div', this).fadeOut(300, function(){
			$('.team-slide-4 .team-block').animate({left: "-96px"}, function(){
				$(this).fadeOut(300);
			});
		});
	});
	$('.team-slide-5').hoverIntent(function(){
		$('.team-block', this).fadeIn(300, function(){
			$(this).animate({top: "0"}, function(){
				$('div', this).fadeIn(300);
			});
		});
	}, function(){
		$('.team-block div', this).fadeOut(300, function(){
			$('.team-slide-5 .team-block').animate({top: "96px"}, function(){
				$(this).fadeOut(300);
			});
		});
	});
	$('.team-slide-9').hoverIntent(function(){
		$('.team-block', this).fadeIn(300, function(){
			$(this).animate({left: "0"}, function(){
				$(this).animate({height: "96px"}, function(){
					$('div', this).fadeIn(300);
				});
			});
		});
	}, function(){
		$('.team-block div', this).fadeOut(300, function(){
			$('.team-slide-9 .team-block').animate({height: "48px"}, function(){
				$(this).animate({left: "96px"}, function(){
					$(this).fadeOut(300);
				});
			});
		});
	});
	$('.team-slide-11').hoverIntent(function(){
		$('.team-block', this).fadeIn(300, function(){
			$(this).animate({right: "0"}, function(){
				$('div', this).fadeIn(300);
			});
		});
	}, function(){
		$('.team-block div', this).fadeOut(300, function(){
			$('.team-slide-11 .team-block').animate({right: "48px"}, function(){
				$(this).fadeOut(300);
			});
		});
	});
	$('.team-slide-12').hoverIntent(function(){
		$('.team-block', this).fadeIn(300, function(){
			$(this).animate({left: "0"}, function(){
				$('div', this).fadeIn(300);
			});
		});
	}, function(){
		$('.team-block div', this).fadeOut(300, function(){
			$('.team-slide-12 .team-block').animate({left: "48px"}, function(){
				$(this).fadeOut(300);
			});
		});
	});
	$('.team-slide-13').hoverIntent(function(){
		$('.team-block', this).fadeIn(300, function(){
			$(this).animate({right: "0"}, function(){
				$('div', this).fadeIn(300);
			});
		});
	}, function(){
		$('.team-block div', this).fadeOut(300, function(){
			$('.team-slide-13 .team-block').animate({right: "96px"}, function(){
				$(this).fadeOut(300);
			});
		});
	});
	
	
	
	//captions hover and slide
	$('.has-caption').hover(function(){
		$(".caption", this).css({bottom: -$(".caption", this).outerHeight()}).show();
		$(".caption", this).stop().animate({bottom: "0px"}, {duration: 500, queue: false});		
	}, function() {
		$(".caption", this).stop().animate({bottom: -$(".caption", this).outerHeight()}, {duration: 500, queue: false});
	});

	
	
	
	//hover overlay
	$('.hover-overlay').stop().hover(function(){
		$(this).css("opacity", "0.8");
	}, function(){
		$(this).css("opacity", "1");
	});
	
	//hover overlay 90
	$('.hover-overlay-90').stop().hover(function(){
		$(this).css("opacity", "0.9");
	}, function(){
		$(this).css("opacity", "1");
	});
	
	
	
	/***********/
	/* GALLERY */
	/***********/
	
	/*** gallery-sidebar ***/
	//vars
	var $gal			= $('.gallery-sidebar');
	var $gal_ul			= $('.gallery-sidebar ul');
	var $gal_li			= $('.gallery-sidebar ul li');
	var $gal_next		= $gal.find('.next');
	var $gal_prev		= $gal.find('.prev');
	
	//get number of images, if less than 4 hide all nav's
	var numItems = $gal_ul.children('li').size();
	if(numItems <= 3){
		$gal_next.hide();
		$gal_prev.hide();
	}else{
		$gal_prev.hide();
	}
	
	var gal_width		= $gal.outerWidth();
	var li_width		= $gal_li.outerWidth();
	var leftval			= 0; //or -(li_width/2)
		
	
	//get computed value of all li's
	//set the ul to be that value
	//set scrollmax for later use in scrolling
	var ul_width		= 0;
	$gal_li.each(function(index) {
		ul_width += $(this).outerWidth();
	});
	$gal_ul.width(ul_width);
	var scrollmax		= -(ul_width-gal_width);
	

	
	//next
	$gal_next.click(function(e){
		e.preventDefault();
		$gal_prev.fadeIn();
		leftval -= li_width;
		$gal_ul.stop().animate({left: leftval}, {duration: 500, queue: false});
		if(leftval <= scrollmax){$(this).hide();}
	});
	
	//prev
	$gal_prev.click(function(e){
		e.preventDefault();
		$gal_next.fadeIn();
		leftval += li_width;
		$gal_ul.stop().animate({left: leftval}, {duration: 500, queue: false});
		if(leftval >= 0){$(this).hide();}
	});
	
	// maincontent search hover/focus
	$('input#keywords').live('focus', function () {
		if (this.value == "Search") {
			this.value = '';
		}
	}).live('blur', function () {
		if (this.value === '') {
			this.value = "Search";
		}
	});


	
	
	
	/************************/
	/* Footer expandy thing */
	/************************/
	$('#footer-button').click(function(e){
		e.preventDefault();
		var doch =  $(document).height();
		if($('#big-footer').hasClass('open')){
			$('#big-footer').animate({'height' : 0}, {duration : 1000, queue : false}).removeClass('open');
			$('#footer-button').removeClass('footer-button-open').addClass('footer-button-closed');
		}else{
			$('#big-footer').animate({'height':bigfooterheight+'px'}, {duration : 1000, queue : false}).addClass('open');
			$('html, body').animate({scrollTop : doch},1000);
			$('#footer-button').addClass('footer-button-open').removeClass('footer-button-closed');
		}
	});
});

