jQuery.noConflict();
jQuery(document).ready(function($) {
	
	//related projects functionality
	var relatedIsOpen = false;
	var categoryIsOpen = false;
	var projectIsOpen = false;
	var otherprojectsIsOpen = false;

	if($("#onProjectPage").length > 0){
		var relatedIsOpen = true;
	}


	$(".videothumbnail").bind("click", function(){
		$(".relateditems").slideUp("slow");
		$(".relatedcontrol").children(".arrow").attr("src",stylesheetdirectory + "/images/icon_openarrow.png");
		relatedIsOpen = false;
	});
	
	$("#academicbuildings").addClass("active");
	$("#academicbuildings").show();
	$("#academicprojects").addClass("active");
	$("#academicprojects").show();
	
	$(".relatedcontrol").bind("click", function(){
		if (relatedIsOpen) {
			$(".relateditems").slideUp("slow");
			$(".relatedcontrol").children(".arrow").attr("src",stylesheetdirectory + "/images/icon_openarrow.png");
			relatedIsOpen = false;
		}else{
			$(".relateditems").slideDown("slow");
			$(".relatedcontrol").children(".arrow").attr("src",stylesheetdirectory + "/images/icon_closearrow.png");
			relatedIsOpen = true;
		}
	});
	
	$(".closerelated").bind("click", function(){
		$(".relateditems").slideUp("slow");
		$(".relatedcontrol").children(".arrow").attr("src",stylesheetdirectory + "/images/icon_openarrow.png");
		relatedIsOpen = false;
	});
	$(document).click(function() {
		if (categoryIsOpen) {
			$(".category").hide();
			$(".subcategory").hide();
			$(".staticcategory").hide();
			$(".category:first").addClass("active");
			$(".category:first").show()
			categoryIsOpen = false;
		}
		if (projectIsOpen) {
			$(".project").hide();
			$(".projects").children(".project").removeClass("active");
			$(".project:first").addClass("active");
			$(".project:first").show();
		}
	});
	
	$(".category, .subcategory, .staticcategory").bind("click", function(e){
		e.stopPropagation();
		if (categoryIsOpen) {
			//close the dropdown
			$(".category").hide();
			$(".subcategory").hide();
			$(".staticcategory").hide();
			
			//unset all the other actives
			$(".categories").children(".category").removeClass("active");
			$(".categories").children(".subcategory").removeClass("active");
			$(".categories").children(".staticcategory").removeClass("active");
			
			//set this one to active
			$(this).addClass("active");
			$(this).show();
			
			if ($(this).attr("id") != '') {
				if ($(this).hasClass("catlist")) {
					window.location = homeurl + '/?page_id=19&categoryId=' + $(this).attr("id");
				}else if($(this).attr("id") == "6" || $(this).attr("id") == "9" || $(this).attr("id") == "17" || $(this).attr("id") == "11" || $(this).attr("id") == "12"){
					window.location = homeurl + '/main-discipline/?categoryId=' + $(this).attr("id");	
				}else if($(this).attr("id") == "fulllist"){
					window.location = homeurl + '/portfolio/';
				}else {
					window.location = homeurl + '/?page_id=19&categoryId=' + $(this).attr("id");
				}
			}
			categoryIsOpen = false;
			
		}else{
			//unset all the other actives
			$(".categories").children(".category").removeClass("active");
			$(".categories").children(".subcategory").removeClass("active");
			$(".categories").children(".staticcategory").removeClass("active");
			$(".category").show();
			$(".subcategory").show();
			$(".staticcategory").show();
			categoryIsOpen = true;
		}
		
		
	});
	
	
	$(".project").bind("click", function(e){					e.stopPropagation();
		if (projectIsOpen) {
			//close the dropdown
			$(".project").hide();
			
			//unset all the other actives
			$(".projects").children(".project").removeClass("active");
			
			//set this one to active
			$(this).addClass("active");
			$(this).show();
			projectIsOpen = false;
			
			if ($(this).attr("id") != '') {
				window.location = homeurl + '/?page_id=' + $(this).attr("id");
			}
			
		}else{
			//unset all the other actives
			$(".projects").children(".project").removeClass("active");
			
			//open the dropdown
			$(".project").show();
			projectIsOpen = true;
		}
		
		
	});
	
	$(".thumbnail").bind("click",function(){
		//reset all before images to show
		//$(".afterImage").hide();
		$(".image").children(".firstimage").show();
		
		//show active thumbnail
		$(".thumbnail").css({background: 'none'}); 
		$(this).css({backgroundImage: 'url(' + stylesheetdirectory + '/images/icon_arrow.gif)'}); 
		$(this).css({backgroundRepeat: 'no-repeat'}); 
		
		//show active big image
		var imageId = $(this).attr("id");
		$(".image").hide();

		$("#large_" + imageId).fadeIn(250);
		$("#large_" + imageId).children(".beforeImageLink").show();
		var imageCaption = $("#large_" + imageId).children(".imageCaption").text();
		$(".staticImageCaption").text(imageCaption);
		
		//if the image we just displayed has a before picture then wait a second and fade that in
		if($("#large_" + imageId).children(".afterImage").length > 0){
			var waitToDisplay = 500;
			window.setTimeout(function() {
				$("#large_" + imageId).children(".firstimage").fadeOut(2000);
			}, waitToDisplay);
		}

		
		return false;
	});
	
	$(".otherprojects").bind("click", function(){
		if (otherprojectsIsOpen) {
			$(this).children(".otherprojectsitems").hide();
			$(this).children(".otherprojectslabel").children(".arrow").attr("src",stylesheetdirectory + "/images/icon_openarrow.png");
			otherprojectsIsOpen = false;
		}else{
			$(this).children(".otherprojectsitems").show();
			$(this).children(".otherprojectslabel").children(".arrow").attr("src",stylesheetdirectory + "/images/icon_closearrow.png");
			otherprojectsIsOpen = true;
		}
	});
	
	$("a[href*=.pdf]").each(function(index) {
		$(this).attr({"target":"_blank"});
	});
	
	
	$(".image").children("a").each(function(index) {
		var rel = $(this).attr("rel");
		if (rel != "lightbox") {
			var galleryName = $(this).parent(".image").children(".galleryName").text();
			rel = galleryName;
			//$(this).attr("rel", rel);
		}
	});										
			
	$("a[href*=.pdf]").each(function(index) {
		$(this).attr({"target":"_blank"});
	});
	
	
});
