Cufon.replace('h1, h2, h3, h4, h5, h6, #sidebar .header span, #nav a', {
	hover: true
});

var createHashQuery = function() {
	
	var hashQuery = {};
	var hashes = location.hash.substring(1).split("&");
	
	for(var i=0; i<hashes.length; i++) {
		
		var hash = hashes[i].split("=");
		hashQuery[hash[0]] = hash[1];
		
	}
	
	return hashQuery;
	
}

window.hashQuery = createHashQuery();

var trackPageView = function (url) {

    if (window._gaq) {
        window._gaq.push(['_trackPageview', url]);
    }

}

$(function() {
	
	$(window).bind("hashchange", function() {
		
		window.hashQuery = createHashQuery();
		
	});
	
	var sizeContent = function() {
	
		var $body = $("body");
		var $pageContainer = $("#page-container");
	
		$pageContainer.css("min-height", "");
		$pageContainer.css("min-height", $body.height());
		
	};
	
	sizeContent();
	
	$(window).resize(function() {
		
		sizeContent();
		
	});
	
	$("#nav").each(function() {
		
		var $container = $(this);
		var $menu = $(">ul", $container);
		
		var growMenu = function($li, callback) {
			
			var $subMenu = $(">.sub-menu", $li);
			
			if ($subMenu.length) {
				
				var newHeight = $li.outerHeight() + $subMenu.outerHeight() - parseInt($container.css("padding-bottom"));
			
				$container.stop(true).animate({
					
					height: newHeight
					
				}, {
					
					duration: 300,
					complete: function() {
					
						if (callback)
							callback();
						
					}
				
				});
				
			}
			
		};
		
		var shrinkMenu = function($li, callback) {
			
			if ($container[0]._shrink) {
			
				$container.stop(true).animate({
					
					height: $li.outerHeight()
					
				}, {
					
					duration: 300,
					complete: function() {
						
						if (callback)
							callback();
						
					}
					
				});
			
			}
			
		};
		
		var showSubMenu = function($li, callback) {

			var $subMenu = $(">.sub-menu", $li);
			
			if ($subMenu.length) {
			
				$subMenu.stop(true).animate({
					
					opacity: 1
					
				}, {
					
					duration: 200,
					complete: function() {
					
						$subMenu.css("opacity", "");
						if(callback)
							callback();
						
					}
				
				});
			
			} else {
				
				if (callback)
					callback();
				
			}
			
		};
		
		var hideSubMenu = function($li, callback) {
			
			var $subMenu = $(">.sub-menu", $li);
			
			if ($subMenu.length) {
			
				$subMenu.stop(true).animate({
					
					opacity: 0
					
				}, {
					
					duration: 200,
					complete: function() {
					
						$subMenu.hide();
						if(callback)
							callback();
						
					}
				
				});
			
			} else {
				
				if (callback)
					callback();
				
			}
			
		};
		
		$(">li", $menu).mouseenter(function() {
			
			var $liHover = $(">li.hover", $menu);
			var $li = $(this);
			var $subMenu = $(">.sub-menu", $li);
			
			$li.addClass("hover");
			
			if ($subMenu.length) {
				
				$container[0]._shrink = false;
				
				if (!$subMenu.is(":visible")) {
					
					$subMenu.css("opacity", 0).show();
					
					var width = 0;
					
					$(">li", $subMenu).each(function() {
						
						width += $(this).outerWidth(true);
						
					});
					
					//$subMenu.width(width);
					
				}

				growMenu($li, function() { showSubMenu($li); });
				
			}
			
		}).mouseleave(function() {
			
			var $li = $(this);
			var $subMenu = $(">.sub-menu", $li);
			
			$li.removeClass("hover");
			
			if ($subMenu.length) {
				
				$container[0]._shrink = true;
				hideSubMenu($li, function() { shrinkMenu($li); });
				
			}
			
		});

	});
	
	$(".hero-rotator").each(function() {
		
		var $container = $(this);
		var $inner = $(">.items", $container);
		var $items = $(">.item", $inner);
		
		var delay = 8000;
		
		$container.append("<div class='nav' />");
		var $nav = $(">.nav", $container);
		
		var timeoutId = -1;
		
		$items.each(function() {
			
			$nav.append("<a href='javascript:;'></a>");
			
		});
		
		var $navItems = $(">a", $nav);
		
		var startIdx = 0;
		
		$items.eq(startIdx).addClass("active").show();
		$navItems.eq(startIdx).addClass("active");
		
		$items.children("p").css("margin-right", $navItems.length * $navItems.outerWidth(true));
		
		$inner.height($items.eq(startIdx).height());
		
		var rotateToItem = function($activeItem, $nextItem) {
			
			if (!$nextItem.is(".active") && !$activeItem.is(":animated") && !$nextItem.is(":animated")) {
				
				$activeItem.stop(true).animate({
					
					opacity: 0
					
				}, {
					
					duration: 500,
					complete: function() {

						$activeItem.hide().removeClass("active");
						$nextItem.addClass("active");
						
						$navItems.eq($activeItem.index()).removeClass("active");
						$navItems.eq($nextItem.index()).addClass("active");

						$inner.stop(true).animate({
							
							height: $nextItem.height()
							
						}, {
							
							duration: Math.abs($inner.height() - $nextItem.height()) / .25,
							complete: function() {
							
								$nextItem.stop(true).css("opacity", 0).show().animate({
									
									opacity: 1
									
								}, {
									
									duration: 500,
									complete: function() {
										
										$nextItem.css("opacity", "");
										scheduleAutoRotate();
									
									}
									
								});
								
								
							}
						});
						
					}
					
				});
			
			}
			
		};
		
		var scheduleAutoRotate = function() {
		
			clearTimeout(timeoutId);
			
			timeoutId = setTimeout(function() {
				
				var $activeItem = $items.filter(".active");
				var $nextItem = $activeItem.next();
				
				if (!$nextItem.length)
					$nextItem = $activeItem.siblings().first();
				
				rotateToItem($activeItem, $nextItem);
				
			}, delay);
		
		};
		
		$navItems.click(function() {
			
			clearTimeout(timeoutId);
			
			rotateToItem($items.filter(".active"), $items.eq($(this).index()));
			
			return false;
			
		});
		
		scheduleAutoRotate();
		
		$inner.mouseenter(function() {
			
			clearTimeout(timeoutId);
			
		}).mouseleave(function() {
			
			scheduleAutoRotate();
			
		});
		
	});
	
	$(".latest-blogs").each(function() {
		
		var $container = $(this);
		var $inner = $(">.items", $container);
		var $items = $(">.item", $inner);
		
		var delay = 4500;
		
		$container.append("<a href='javascript:;' class='prev' title='Previous' /><a href='javascript:;' class='next' title='Next' />");
		
		var timeoutId = -1;
		
		var startIdx = 0;
		
		$items.eq(startIdx).addClass("active").show().css({
			
			marginTop: Math.round(($inner.height() - $items.eq(startIdx).height()) / 2)
			
		});
		
		var rotateToItem = function($activeItem, $nextItem) {
			
			if (!$nextItem.is(".active") && !$activeItem.is(":animated") && !$nextItem.is(":animated")) {
				
				$activeItem.stop(true).animate({
					
					opacity: 0
					
				}, {
					
					duration: 500,
					complete: function() {

						$activeItem.hide().removeClass("active");
						$nextItem.addClass("active");
							
						$nextItem.stop(true).show().css({
							
							opacity: 0,
							marginTop: Math.round(($inner.height() - $nextItem.height()) / 2)
							
						}).animate({
							
							opacity: 1
							
						}, {
							
							duration: 500,
							complete: function() {
								
								$nextItem.css("opacity", "");
								scheduleAutoRotate();
							
							}
							
						});
						
					}
					
				});
			
			}
			
		};
		
		$(".prev, .next", $container).click(function() {
			
			clearTimeout(timeoutId);
			
			var $link = $(this);
			var isPrev = $link.is(".prev");
			
			var $nextItem = isPrev ? $items.filter(".active").prev() : $items.filter(".active").next();
			
			if ($nextItem.length == 0)
				$nextItem = isPrev ? $items.last() : $items.first();
			
			rotateToItem($items.filter(".active"), $nextItem);
			
			return false;
			
		});
		
		var scheduleAutoRotate = function() {
		
			clearTimeout(timeoutId);
			
			timeoutId = setTimeout(function() {

				$(".next", $container).click();
				
			}, delay);
		
		};
		
		scheduleAutoRotate();
		
		$inner.mouseenter(function() {
			
			clearTimeout(timeoutId);
			
		}).mouseleave(function() {
			
			scheduleAutoRotate();
			
		});
		
	});
	
	$(".sub-page-expander").each(function() {
		
		var $container = $(this);
		var $items = $(">.item", $container);
		
		var startIdx = 0;

		if (window.hashQuery.tab) {

			var $startItem = $items.filter("#" + window.hashQuery.tab);
			
			if ($startItem.length) {
				
				$startItem.addClass("expanded").height($(".expander", $startItem).outerHeight() + $(".content", $startItem).outerHeight());
				$(".content", $startItem).show();
				$(".expander .help", $startItem).text("Collapse");
				
				$("body, html").stop(true).animate({
					
					scrollTop: $startItem.offset().top
					
				}, $startItem.offset().top / 1.6);
				
			}
			
		}
		
		var closeItem = function($item, callback) {
			
			var $link = $(">.expander>a", $item);
			var $content = $(">.content", $item);
			var $expander = $(".expander", $item);
			
			$item.removeClass("expanded");
			$(".help", $link).text("Expand");
			
			$content.stop(true).animate({
				
				opacity: 0
				
			}, {
				
				duration: 120,
				complete: function() {
				
					$content.hide();
					
					var diff = Math.abs($item.height() - $expander.outerHeight());

					$item.stop(true).animate({
						
						height: $expander.outerHeight()
						
					}, {
						
						duration: diff / 2,
						complete: function() {
						
							if (callback)
								callback();
						
						}
						
					});
				
				}
			
			});
			
		};
		
		var showItem = function($item, callback) {
			
			var $link = $(">.expander>a", $item);
			var $expander = $(".expander", $item);
			var $content = $(">.content", $item);
			
			$item.addClass("expanded");
			$(".help", $link).text("Collapse");
			
			var diff = Math.abs($item.height() - $expander.outerHeight() - $content.outerHeight());

			$item.stop(true).animate({
				
				height: $expander.outerHeight() + $content.outerHeight()
				
			}, {
				
				duration: diff / 1.75,
				complete: function() {
				
					if (!$content.is(":visible"))
						$content.css("opacity", 0).show();
					
					$content.stop(true).animate({
						
						opacity: 1
						
					}, {
						
						duration: 250,
						complete: function() {
						
							$content.css("opacity", "");
						
						}
						
					});
				
				}
				
			});
			
		};
		
		$(".expander>a", $items).click(function() {
			
			var $link = $(this);
			var $item = $link.closest(".item");
			
			if (!$item.is(".expanded")) {
			
				if ($items.filter(".expanded").length)
					closeItem($items.filter(".expanded"), function() { showItem($item); });
				else
					showItem($item);
			
			} else {
				
				if (window.hashQuery.tab == $item.attr("id"))
					location.hash = "_";
				
				closeItem($item);
				
				return false;
				
			}
			
		});
		
	});
	
	if ($(".google-map").length) {
		
		$.getScript("http://maps.googleapis.com/maps/api/js?sensor=false&callback=loadMaps");
		
		window.loadMaps = function() {
			
			$(".google-map").each(function() {
				
				var $map = $(this);
				var coords = $map.data("coords").split(",");
				coords = new google.maps.LatLng(coords[0], coords[1]);

				var map = new google.maps.Map($map[0], {
					
					zoom: 15,
					maxZoom: 17,
					minZoom: 10,
					center: coords,
					mapTypeId: google.maps.MapTypeId.ROADMAP
					
				});
				
				var marker = new google.maps.Marker({
					
					position: coords,
					map: map,
					title: $map.data("marker-title")
					
				});
				
			});
			
		}
		
	}
	
$(".team-member-list a").click(function() {
		
		var $link = $(this);
		var $details = $link.closest(".team-member");
		var $container = $link.closest(".team-member-list");
		var $ajaxWrap = $container.closest(".team-member-ajax-wrap");
		var $breadcrumbs = $("#breadcrumbs");
		var origBreadcrumbs = $breadcrumbs.html();
		var origTitle = document.title;
		
		if (!$ajaxWrap.length) {
			
			$container.wrap("<div class='team-member-ajax-wrap' />");
			$ajaxWrap = $container.closest(".team-member-ajax-wrap");
			
		}
		
		trackPageView($link.attr("href"));
		window.location.hash = "#!" + $link.attr("href").replace(window.location.protocol + "//" + window.location.host + window.location.pathname, "");
		
		var detailsCss = {
			
			position: "absolute",
			left: $details.position().left + parseInt($details.css("marginLeft")),
			top: $details.position().top
				
		};
		
		var $newDetails = $("<div class='team-member-single clearfix' />");
		
		$ajaxWrap.append($newDetails);
		
		$newDetails.append($details.clone()).css(detailsCss).find(".details img").unwrap();
		
		$details.css("visibility", "hidden");
		
		$(".details", $newDetails).append("<span class='loading' />");
		
		$container.stop(true).animate({
			
			opacity: 0
			
		}, {
			
			duration: 150,
			complete: function() {
			
				$container.hide();
				
				$newDetails.stop(true).animate({
					
					left: 0,
					top: 0
					
				}, {
					
					duration: detailsCss.left / .9,
					complete: function() {
					
						$newDetails.css({
							
							position: "",
							left: "",
							top: ""							
							
						});
						
						$(".team-member", $newDetails).append("<div class='content' />");
						
						$newDetails.append("<a href='javascript:;' class='close image-text' title='Close' />");
						
						var $close = $(".close", $newDetails);
						var $content = $(".content", $newDetails);
						
						$close.hide();
						
						$content.css("opacity", 0).load($link.attr("href") + " #" + $details.attr("id") + ">.content", function() {

							$content.stop(true).animate({
								
								opacity: 1
								
							}, {
								
								duration: 200,
								complete: function() { $content.css("opacity", ""); $close.show(); $(".details .loading", $newDetails).remove(); }
							
							});
							
						});
						
						$close.click(function() {
							
							$close.hide();
							$content.stop(true).animate({
								
								opacity: 0
								
							}, {
								
								duration: 150,
								complete: function() {
								
									$content.hide();
								
									$newDetails.stop(true).css("position", "absolute").addClass("small").animate({
										
										left: detailsCss.left,
										top: detailsCss.top
										
									}, {
										
										duration: detailsCss.left / 1.2,
										complete: function() {
										
											$container.stop(true).show().animate({
												
												opacity: 1
												
											}, {
												
												duration: 200,
												complete: function() {
												
													$container.css("opacity", "");
													
													$newDetails.remove();
													$details.css("visibility", "");
													
												}
											
											});
										
										}
										
									});
								
								}
							
							});
							
						});
					
					}
					
				});
				
			}
		
		});
		
		return false;
		
	});
	
});
