$(document).ready(function() {
	
	//Open external links in a new window
	$("a").each(
		function() {
			var destinationURL = $(this).attr("href");
			if(destinationURL.indexOf("http") > -1 && destinationURL.indexOf("metz") == -1) {
				$(this).bind("click", function() { //Open link in new window
					window.open(destinationURL);
					return false;
				});
			}
		}
	);
	
	var currentURL = document.URL;	
	if(currentURL.indexOf("projects") > -1 && currentURL.indexOf("bim") < 0) $("#sub-nav-projects").show();
	if(currentURL.indexOf("about-bim") > -1) $("#sub-nav-bim").show();
	if(currentURL.indexOf("about-rem") > -1) $("#sub-nav-rem").show();
	
	var currentCategory = $("span.cat-name").text();
	var checkCat = currentCategory.indexOf("Metz Architects");
	if(checkCat != -1) {
		currentCategory = currentCategory.substr(15);
	} else {
		var checkCat = currentCategory.indexOf("Metz BIM");
		if(checkCat != -1) {
			currentCategory = currentCategory.substr(8);
		} else {
			var checkCat = currentCategory.indexOf("Metz REM");
			if(checkCat != -1) {
				currentCategory = currentCategory.substr(8);
			}
		}
	}
	
	if(currentCategory == "Restoration, Heritage &amp; Culture") {
		currentCategory = "restoration";
	} else if(currentCategory == "Mixed Use") {
		currentCategory = "mixed";
	} else if(currentCategory == "How BIM will affect the construction industry") {
		currentCategory = "how-bim-affects";
	} else if(currentCategory == "How is a Building Information Model made?") {
		currentCategory = "how-is-a-bim-made";
	} else if(currentCategory == "Issues with BIM") {
		currentCategory = "issues-with-bim";
	} else if(currentCategory == "What MetzBIM do") {
		currentCategory = "what-metzbim-do";
	}
	
	if(currentCategory != "") {
		$("ul." + currentCategory.toLowerCase()).show();
		
		if(currentCategory == "restoration") {
			currentCategory = "Restoration, Heritage &amp; Culture";
		} else if(currentCategory == "mixed") {
			currentCategory = "Mixed Use";
		} else if(currentCategory == "how-bim-affects") {
			currentCategory = "How BIM will affect the construction industry";
		}  else if(currentCategory == "how-is-a-bim-made") {
			currentCategory = "How is a Building Information Model made?";
		} else if(currentCategory == "issues-with-bim") {
			currentCategory = "Issues with BIM";
		} else if(currentCategory == "what-metzbim-do") {
			currentCategory = "What MetzBIM do";
		}
		
		$("#project-cats a").removeClass("current");
		
		$("#project-cats a").each(
			function() {
				
				if($(this).text() == currentCategory) $(this).addClass("current");
				
			}
		);
	}

	//SUB NAVIGATION
	$("#nav-projects").toggle(
		function() {
			
			if($("#sub-nav-projects").is(":hidden")) {
				$(this).addClass("current");
				$("#sub-nav-projects").slideDown(500);	
			} else {$("#sub-nav-projects").slideUp(500);}
						
			return false;
			
		}, function() {
			
			if($("#sub-nav-projects").is(":visible")) {
				$(this).removeClass("current");
				$("#sub-nav-projects").slideUp(500);
			} else {$("#sub-nav-projects").slideDown(500);}
			
			return false;
			
		}
	);
	
	$("#nav-aboutbim").toggle(
		function() {
			
			if($("#sub-nav-bim").is(":hidden")) {
				$(this).addClass("current");
				$("#sub-nav-bim").slideDown(500);	
			} else {$("#sub-nav-bim").slideUp(500);}
						
			return false;
			
		}, function() {
			
			if($("#sub-nav-bim").is(":visible")) {
				$(this).removeClass("current");
				$("#sub-nav-bim").slideUp(500);
			} else {$("#sub-nav-bim").slideDown(500);}
			
			return false;
			
		}
	);
	
	$("#nav-aboutrem").toggle(
		function() {
			
			if($("#sub-nav-rem").is(":hidden")) {
				$(this).addClass("current");
				$("#sub-nav-rem").slideDown(500);	
			} else {$("#sub-nav-rem").slideUp(500);}
						
			return false;
			
		}, function() {
			
			if($("#sub-nav-rem").is(":visible")) {
				$(this).removeClass("current");
				$("#sub-nav-rem").slideUp(500);
			} else {$("#sub-nav-rem").slideDown(500);}
			
			return false;
			
		}
	);
	
	$("#project-cats a").click(
		function() {
			
			if(!$(this).hasClass("nonav")) {
				var selectedCat = $(this).text().toLowerCase();
				//alert(selectedCat);
				
				if(selectedCat == "restoration, heritage &amp; culture") {
					selectedCat = "restoration";
				} else if(selectedCat == "mixed use") {
					selectedCat = "mixed";
				} else if(selectedCat == "how bim will affect the construction industry") {
					selectedCat = "how-bim-affects";
				} else if(selectedCat == "how is a building information model made?") {
					selectedCat = "how-is-a-bim-made";
				} else if(selectedCat == "issues with bim") {
					selectedCat = "issues-with-bim";
				} else if(selectedCat == "what metzbim do") {
					selectedCat = "what-metzbim-do";
				}
				
				if(selectedCat == "philosophy") {				
					return true;
				} else {
					$("#project-cats a").removeClass("current");
					$(this).addClass("current");				
					$("ul.projects").hide();
					$("ul." + selectedCat).fadeIn(300);
				}
			
				return false;
			} else {
				
				return true;
				
			}
		}
	);
	
	//NEWS CATEGORIES
	var currentURL = document.URL;
	var currentCat = currentURL.substr(currentURL.indexOf("category/") + 9);
	currentCat = currentCat.substr(0, currentCat.indexOf("/"));
	if(currentCat == "articles") {
		
		$("#sub-nav-news li a").removeClass("current");
		$("#sub-nav-news li.cat-2 a").addClass("current");
		
	} else if(currentCat == "press") {
		
		$("#sub-nav-news li a").removeClass("current");
		$("#sub-nav-news li.cat-3 a").addClass("current");
		
	} else if(currentCat == "vacancies") {
		
		$("#sub-nav-news li a").removeClass("current");
		$("#sub-nav-news li.cat-5 a").addClass("current");
		
	} else {
		
		$("#sub-nav-news li a").removeClass("current");
		$("#sub-nav-news li.cat-1 a").addClass("current");
		
	}
	
	//PRACTICE SUB NAV
	
	
	//HERO SLIDESHOW
	var slideshow_count = $("#hero li").length;
	var slideshow_current = 1;
	
	$("#hero li:first").show();
	
	function slideshow_run() {
		
		if(slideshow_count > 1) {
			
			$("#hero-image-" + slideshow_current).fadeOut(1500);
			
			slideshow_current++;
			
			if(slideshow_current > slideshow_count) {
				slideshow_current = 1;
			}
			
			$("#hero-image-" + slideshow_current).fadeIn(1500);
			
		}
		
	}
	
	if(slideshow_count > 1) {
	    $("#hero").everyTime(4000, slideshow_run);
	}
	
	//NEWS TICKER
	var ticker_count = $("#news-ticker li").length;
	var ticker_count = 1;
	
	$("#news-ticker li:first").show();
	
	//NEWS TICKER NAVIGATION
	var ticker_current = "content-1";
	
	$("#news-ticker-nav li a").click(
		function() {
			var ticker_id = $(this).attr("id");
			
			if (ticker_current != ticker_id && ticker_id != "") {
				
				$("#news-ticker-nav li a").removeClass("current");
				$("#" + ticker_id).addClass("current");
				
				$("#slideshow-" + ticker_current).fadeOut(300, function() {
					
					$("#slideshow-" + ticker_id).fadeIn(300);
					ticker_current = ticker_id;
					
				});
				
				return false;
				
			}
			
		}
	);
	
	//PROJECT HERO SLIDESHOW
	if($("#completed li").length > 0) {
	
		$("#slideshow-c-content-1").show();
		var slideshow_group = "c-";
		
	} else if($("#in-progress li").length > 0) {
	
		$("#slideshow-i-content-1").show();
		var slideshow_group = "i-";
		
	} else if($("#pre-development li").length > 0) {
	
		$("#slideshow-p-content-1").show();
		var slideshow_group = "p-";
		
	} else if($("#maps li").length > 0) {
	
		$("#slideshow-m-content-1").show();
		var slideshow_group = "m-";
		
	} else {
	
		$("#completed").hide();
		$("#in-progress").hide();
		$("#pre-development").hide();
		
	}
	
	//SLIDESHOW NAVIGATION
	var current_content = slideshow_group + "content-1";
	var content_i = 1;
	$("#" + current_content).addClass("current");
	
	$(".slideshow-nav li a").click(
		function() {
			var content_id = $(this).attr("id");
			
			if (current_content != content_id && content_id != "") {
				
				$(".slideshow-nav li").each(
					function() {
						var this_content_id = $(this).children("a").attr("id");
						
						if (current_content == this_content_id) {
							$("#slideshow-" + current_content).fadeOut(300);
							$("#slideshow-" + content_id).animate({ marginLeft: 0 }, 300).fadeIn(300);
							$("#" + current_content).removeClass("current");
							current_content = content_id;
							$("#" + content_id).addClass("current");
							
							return false;
						}
					}
				);
				
				return false;
				
			}
			
		}
	);
	
});

//OVERLAYS
function overlay_privacy() {$("#overlay").load("http://metz.uk.com/overlays #overlay-privacy");	
	return false;
}

function close_overlay() {	
	$("#overlay").empty();	
	return false;
}
