$(function() {
	
	var $header = $("#header");
	var $footer = $("#footer");
	var $content = $("#content");
	var $contentMain = $("#content>.content-main");
	var $contentLeft = $("#content>.content-main>.content-left");
	var $contentRight = $("#content>.content-main>.content-right");
	var $headlines = $("#headlines>.inner");
	
	$content.css("padding-bottom", $footer.height());
	
	var bindWindowSizeContent = function() {
		
		$(window).resize(function() { sizeContent(false) });
		
	};
	
	var unbindWindowSizeContent = function() {
		
		$(window).unbind("resize");
	
	};
	
	var sizeContent = function(animate, animateCallback) {
		
		if (!$("body").is(".home")) {
			
			unbindWindowSizeContent();
			
			var origContentHeight = $contentMain.height();
			var origHeadlinesHeight = $headlines.length ? $headlines.height() : 0;
			
			$contentMain.css("height", "");
			$contentRight.css("height", "");
			$contentLeft.css("height", "");
			
			if ($headlines.length)
				$headlines.css("height", "");
			
			var contentHeight = $contentLeft.height() > $contentRight.height() ? $contentLeft.height() : $contentRight.height();
			
			if (contentHeight < $footer.offset().top - ($header.offset().top + $header.outerHeight(true)) - ($contentMain.outerHeight(true) - $contentMain.height())) {
				
				contentHeight = $footer.offset().top - ($header.offset().top + $header.outerHeight(true)) - ($contentMain.outerHeight(true) - $contentMain.height());
				
			}
			
			var headlinesHeight = $headlines.length ? contentHeight - $headlines.parent("#headlines").position().top - $headlines.position().top - ($headlines.outerHeight(true) - $headlines.height()) : 0;
			
			if (!animate) {
			
				$contentMain.height(contentHeight);
				$contentRight.height(contentHeight);
				$contentLeft.height(contentHeight);
				
				if ($headlines.length)
					$headlines.height(headlinesHeight);
				
				bindWindowSizeContent();
			
			} else {
				
				if (origContentHeight < contentHeight) {
				
					$contentLeft.height(contentHeight);
					
					if ($headlines.length)
						$headlines.height(headlinesHeight);
					
				} else {
					
					$contentLeft.height(origContentHeight);
					
					if ($headlines.length)
						$headlines.height(origHeadlinesHeight);
					
				}
				
				$contentMain.stop().height(origContentHeight).animate({
				
					height: contentHeight
					
				}, {
					
					duration: 500,
					complete: function() {
					
						if (origContentHeight > contentHeight) {
							
							$contentLeft.height(origContentHeight);
							
							if ($headlines.length)
								$headlines.height(origHeadlinesHeight);
							
						}
						
						if (animateCallback) animateCallback();
						
						bindWindowSizeContent();
					
					}
					
				});
				
			}
		
		}
		
	};
	
	$(".drop-down-list").dropDownList();
	
	$(".scroll-list-container ul li a").live("mouseenter", function() {
		
		var $link = $(this);
		var $li = $link.closest("li");
		
		$li.addClass("hover");
		
		if (!$li.is(".active")) {
			
			$li.stop().animate({
				
				paddingLeft: 20
				
			}, 200).find("a>.indent").stop().animate({
				
				width: 20
				
			}, 200);
		
		}
		
	}).live("mouseleave", function() {
		
		var $link = $(this);
		var $li = $link.closest("li");
		
		$li.removeClass("hover");
		
		if (!$li.is(".active")) {
			
			$li.stop().animate({
				
				paddingLeft: 0
				
			}, 200).find("a>.indent").stop().animate({
				
				width: 0
				
			}, 200);
		
		}
		
	});
	
	var setupScrollListContainer = function($elm, findHash, clickHash) {

		if (findHash) {
			
			var hash = location.hash.split("!");
			
			if (hash[1]) {
				
				var $startElm = $elm.find("#" + hash[1]);
				
				if ($startElm.length && !$startElm.is(".active")) {
				
					$elm.find("ul a.active").removeClass("active");
					
					if (clickHash)
						$elm.find("#" + hash[1]).click();
					else
						$elm.find("#" + hash[1]).addClass("active");
				
				} else if (!$startElm.length) {
					
					location.hash = "#!";
					
				}
				
			}
			
		}
		
		$elm.find("ul a").append("<span class='indent' />");
		
		$elm.children(".inner").hoverscroll({
			
			vertical: true,
			width: 328,
			height: 342,
			arrows: false
			
		});
		
	};
	
	var setupContentDefaults = function($container) {
		
		$(".gallery .item .video", $container).append("<span class='button button-play'>Play Video</span>");
		$("a.lightbox", $container).lightbox();
		
	};
	
	$("#scroll-list-people ul a, #scroll-list-companies ul a", $contentLeft).live("click", function() {
		
		var $link = $(this);
		var $li = $link.closest("li");
		
		if (!$li.is(".active") && !$contentRight.is(".loading")) {
			
			$li.closest(".scroll-list-container").find("li.active").css("padding-left", 20).removeClass("active hover").stop().animate({
				
				paddingLeft: 0
				
			}, 200).find("a>.indent").width(20).stop().animate({
				
				width: 0
				
			}, 200);
			
			$li.addClass("active");
		
			$li.addClass("loading");
			$contentRight.addClass("loading");
			
			var fadeOutContentRight = function() {
				
				$contentRight.stop().animate({
					
					opacity: 0
					
				}, {
					
					duration: 300,
					complete: function() {
					
						$contentRight.css("visibility", "hidden").load($link.attr("href") + " #content>.content-main>.content-right>*", function(data) {
							
							if (window._gaq)
								window._gaq.push(['_trackPageview', $link.attr("href")]);
							
							document.title = data.match(/<title>(.*?)<\/title>/)[1];
							
							$("#utility .breadcrumbs").html($("#utility .breadcrumbs", data).html());
							
							setupContentDefaults($contentRight);
							
							sizeContent(true, function() {
								
								var $images = $(".content-images", $contentRight);
								
								$images.css("opacity", 0);
								
								$contentRight.css("visibility", "");
								
								$contentRight.stop().animate({
									
									opacity: 1
									
								}, {
									
									duration: 300,
									complete: function() {
									
										$contentRight.removeClass("loading");
										$li.removeClass("loading");
									
										if ($(this).css("opacity") == 1)
											$(this).css("opacity", "");
										
										$images.stop().animate({
											
											opacity: 1
											
										}, {
											
											duration: 300,
											complete: function() {
											
												if ($(this).css("opacity") == 1)
													$(this).css("opacity", "");
												
											}
											
										})
									
									}
									
								});
								
							});
							
						});
					
					}
					
				});
				
			};
			
			var $images = $(".content-images", $contentRight);
			
			if ($images.length) {
			
				$images.stop().animate({
					
					opacity: 0
					
				}, {
					
					duration: 300,
					complete: function() {
					
						fadeOutContentRight();
					
					}
				
				});
			
			} else {
				
				fadeOutContentRight();
				
			}
			
			location.hash = "#!" + $link.attr("id");
			
		}

		return false;
		
	});
	
	$(".scroll-list-filter a").click(function() {
		
		var $link = $(this);
		var $container = $link.closest(".filter");
		var targetSelector = "#" + $container.data("rel");
		var $target = $(targetSelector);
		
		$target.load($link.attr("href") + " " + targetSelector + ">*", function() {
			
			if (window._gaq)
				window._gaq.push(['_trackPageview', $link.attr("href")]);
			
			setupScrollListContainer($target, true, false);
			sizeContent(true);
			
		});
		
		return false;
		
	});
	
	var hideHomeImage = function($item, callback) {
		
		if ($item.length) {
			
			var $container = $(".image-container", $item);
			var $image = $("img", $container);
			var $details = $(".details", $item);
			
			$details.animate({
				
				opacity: 0
				
			}, {
				
				duration: 500,
				complete: function() {
				
					$container.width($image.width()).height($image.height()).animate({
						
						width: 2
						
					}, {
						
						duration: 1500,
						complete: function(){
						
							$container.height(376).animate({
								
								height: 268
								
							}, {
								
								duration: 300,
								complete: function() {
								
									$item.hide();
									
									if (callback)
										callback();
									
								}
								
							});
							
						}
					
					});
					
				}
			
			});
		
		} else {
			
			if (callback) callback();
			
		}
			
	};
	
	var showHomeImage = function($item, callback) {
		
		var $container = $(".image-container", $item);
		var $image = $("img", $container);
		var $details = $(".details", $item);
		
		$details.css("opacity", 0);
		
		$item.show();
		
		$container.width(2).height(268).animate({
			
			height: 376
			
		}, {
			
			duration: 300,
			complete: function(){
			
				$container.height($image.height()).animate({
					
					width: $image.width()
					
				}, {
					
					duration: 1500,
					complete: function() {
					
						$details.animate({
							
							opacity: 1
							
						}, {
							
							duration: 500,
							complete: function() {
								
								$details.css("opacity", "");
								
							}
							
						});
						
						if (callback)
							callback();
						
					}
					
				});
				
			}
		
		});
		
	};
	
	var rotateHomeImage = function($nav) {
		
		if ($(">a.active", $nav).next().length)
			$(">a.active", $nav).next().click();
		else
			$(">a.active", $nav).siblings(":first").click();
		
	};
	
	$(".content-home .images").each(function() {
		
		var $container = $(this);
		var $images = $(">.item", $container);
		var rotateTimeout = -1;
		
		$container.append("<div class='nav' />");
		
		var $nav = $(">.nav", $container);
		
		for(var i=1; i<=$images.length; i++) {
			
			$nav.append("<a href='javascript:;'>" + i + "</a>");
			
		}
		
		var $navs = $(">.nav>a", $container);
		
		var startIdx = Math.round(Math.random() * ($images.length - 1));
		
		$images.hide();
		//$images.eq(startIdx).addClass("active").siblings(".item").hide();
		//$navs.eq(startIdx).addClass("active");
		
		$navs.click(function() {
			
			var $link = $(this);
			
			clearTimeout(rotateTimeout);
			
			$link.addClass("active").siblings(".active").removeClass("active");
			
			var $activeImage = $images.filter(".active");
			var $nextImage = $images.eq($link.index());
			
			$activeImage.removeClass("active");
			$nextImage.addClass("active");
			
			hideHomeImage($activeImage, function() {
				
				showHomeImage($nextImage, function() {
					
					rotateTimeout = setTimeout(function() { rotateHomeImage($nav); }, 5000);
					
				});
				
			});
			
		});
		
		$(".details a.button-play").click(function() {
			
			clearTimeout(rotateTimeout);
			
		});
		
		$navs.eq(startIdx).click();
		
		//rotateTimeout = setTimeout(function() { rotateHomeImage($nav); }, 5000);
		
	});
	
	setupContentDefaults($("body"));
	
	setupScrollListContainer($(".scroll-list-container"), true, true);
	sizeContent(false);
	
	bindWindowSizeContent();
	
});
