$(document).ready(function() {
	$.easing.def = "linear";
	
	//feature rotation
	if($("#bg img").length) {
		$("#bg").supersize();
		if($("#bg img.active").hasClass("gradient")) $("body").addClass("gradient");
		$("h1#tagline").html($("#bg img.active").attr("alt"));
		$("h1#tagline").css("top",467-$("h1#tagline").height()+"px");
		$("h1#tagline").fadeIn(500);
		if($("#bg img").length > 1) setTimeout('featureRotation()',8000);
	}
	
	//open login overlay
	$("a.login").click(function() {
		$("#mask").fadeIn();
		$("#login").show(function() {
			$(this).animate({
				width: 572
			}, 500, 'easeOutQuad', function() {
				$("#login .inner").animate({
					marginTop: 0
				}, 500, 'easeOutQuad');
			});
		});
		return false;
	});
	//close login overlay
	$("a.close, #mask").click(function() {
		$("#login .inner").animate({
			marginTop: 453
		}, 500, 'easeInQuad', function() {
			$("#login").animate({
				width: 0
			}, 500, 'easeInQuad', function() {
				$("#mask").fadeOut();
				$(this).hide();
			});
		});
		return false;
	});
	
	//content div width
	$("#content").width($("#container").width()-186);
	var content_width = $("#content").width()-120;
	if($("#leadership_wrapper").length) {
		var count = Math.floor(content_width/318);
		$("#leadership_wrapper").width(content_width);
		$("#leadership_wrapper .photo").css("width","100%");
		$("#leadership_wrapper .leadership:nth-child("+count+"n)").find(".photo").css("width","auto");
		$("#leadership_wrapper .photo").last().css("width","auto");
	}
	if($("#portfolio_wrapper").length) {
		$("#portfolio_wrapper").width(content_width);
		
		//grid layout
		$('#portfolio_wrapper').masonry({
			itemSelector: '.tile',
			isResizeable: true
		});
		
		//filter on load
		var incoming = location.hash.split("#/");
		if(incoming.length>1) {
			var filter = incoming[1];
			$("#filters a.current").removeClass("current");
			$("#filters a[href='#/"+filter+"']").addClass("current");
			if(filter=="all") $("#portfolio_wrapper a").not(".tile").show().addClass("tile");
			else {
				$("#portfolio_wrapper a").not("."+filter).hide().removeClass("tile");
				$("#portfolio_wrapper a."+filter).show().addClass("tile");
				$("#filters a.all").show();
			}
			//reset masonry
			$('#portfolio_wrapper').masonry('reload');
		}
		
		//filters
		$("#filters a").click(function(e) {
			$("#filters a.current").removeClass("current");
			$(this).addClass("current");
			var filter = $(this).attr("href");
			filter = filter.replace(/#\//,'');
			if(filter=="all") {
				$("#portfolio_wrapper a").not(".tile").show().addClass("tile");
				$("#filters a.all").hide();
			} else {
				$("#portfolio_wrapper a").not("."+filter).hide().removeClass("tile");
				$("#portfolio_wrapper a."+filter).show().addClass("tile");
				$("#filters a.all").show();
			}
			$("html, body").animate({scrollTop:0},"slow");
			//reset masonry
			$('#portfolio_wrapper').masonry('reload');
		});
		
		//set browing category
		$("a.tile").click(function() {
			if($("#filters a.current").attr("href")!="#/all") {
				var id = $(this).attr("href");
				id = id.split("/");
				var category = $("#filters a.current").attr("href");
				category = category.split("#/");
				if(id.length>1 && category.length>0) window.location = "/portfolio/"+category[1]+"/"+id[2];
				else return true;
			} else return true;
			return false;
		});
	}
	if($("#project_wrapper").length) {
		$("#project_wrapper").width(content_width);
		
		//grid layout
		$('#project_wrapper').masonry({
			itemSelector: '.image',
  			columnWidth: 437,
			isResizeable: true
		});
		
		//project detail expand
		$(".box .expand").click(function() {
			$(this).hide();
			$(this).next().slideDown();
			return false;
		});
		$(".box .close").click(function() {
			$(this).parent().slideUp(function() {
				$(".box .expand").show();
			});
			return false;
		});
	}
	if($("#culture_wrapper").length) {
		$("#culture_wrapper").width(content_width);
		
		//grid layout
		$('#culture_wrapper').masonry({
			itemSelector: '.tile',
  			columnWidth: 437,
			isResizeable: true
		});
	}
	
	//video overlay
	$("#video_overlay").overlay({mask: { color: '#000', opacity: 0.4 }, top: 'center' });
	$("#video_overlay .close").click(function() {
		$("#video_overlay .video").empty();
	});
	$(".play").click(function() {
		$("#video_overlay .video").empty();
		$("#video_overlay .video").append('<iframe width="640" height="390" src="http://www.youtube.com/embed/'+$(this).attr("href")+'?rel=0&amp;hd=1&amp;autoplay=1" frameborder="0" allowfullscreen></iframe>');
		$("#video_overlay").overlay().load();
		return false;
	});
	if($(window).height()>718) $("body").addClass("large");
});

$(window).resize(function() {
	//content div width
	$("#content").width($("#container").width()-186);
	var content_width = $("#content").width()-120;
	if($("#leadership_wrapper").length) {
		var count = Math.floor(content_width/318);
		$("#leadership_wrapper").width(content_width);
		$("#leadership_wrapper .photo").css("width","100%");
		$("#leadership_wrapper .leadership:nth-child("+count+"n)").find(".photo").css("width","auto");
		$("#leadership_wrapper .photo").last().css("width","auto");
	}
	if($("#portfolio_wrapper").length) $("#portfolio_wrapper").width(content_width);
	if($("#project_wrapper").length) $("#project_wrapper").width(content_width);
	if($("#culture_wrapper").length) $("#culture_wrapper").width(content_width);
	if($(window).height()>718) $("body").addClass("large");
	else  $("body").removeClass("large");
});

//resize image on ready or resize
$.fn.supersize = function() {
	$(document).ready(function() {
		$('#bg').resizenow(); 
	});
	$(window).bind("resize", function() {
		$('#bg').resizenow(); 
	});
};
$.fn.resizenow = function() {
	var startwidth = 1600;  
	var startheight = 1060;
	//image ratio
	var ratio = startheight/startwidth;
	//browser dimensions
	var browserwidth = $(window).width();
	var browserheight = $(window).height();
	//resize image to proper ratio
	if ((browserheight/browserwidth) > ratio) {
		$(this).height(browserheight);
		$(this).width(browserheight / ratio);
		$(this).children().height(browserheight);
		$(this).children().width(browserheight / ratio);
	} else {
		$(this).width(browserwidth);
		$(this).height(browserwidth * ratio);
		$(this).children().width(browserwidth);
		$(this).children().height(browserwidth * ratio);
	}
	//position image so that it is in full view
	$(this).children().css('left', (browserwidth - $(this).width()));
	$(this).children().css('top', (browserheight - $(this).height()));
};

//rotation homepage features
function featureRotation() {
	var current = $("#bg img.active");
	$("#bg img.active").removeClass("active");
	if(current.next().length) current.next().addClass("active");
	else $("#bg img").first().addClass("active");
	
	$("h1#tagline").fadeOut(500, function() {
		if($("#bg img.active").hasClass("gradient")) $("body").addClass("gradient");
		else $("body").removeClass("gradient");
		$(this).html($("#bg img.active").attr("alt"));
		$(this).css("top",467-$(this).height()+"px");
		$(this).fadeIn(500);
	});
	setTimeout('featureRotation()',8000);
}
