$(document).ready(function($) { 

	var windowWidth = $(window).width();
	function openNavText(navText) {
		
		
		if($(window).width() >= 750) {
			$(".nav").css("height", "auto");
			$(".navText").css("position", "static");
		}
		else {
			$(".navText").css("position", "absolute");
			$(".nav").css("height", "28px");
		}
		
		$(".navText").css("display", "none");
		
		navText.css("display", "block");
		var headerHeight = navText.parent().parent().css("height");
	
		var navTextHeight = navText.css("height");
		
		headerHeight = parseInt(headerHeight.replace("px", ""));
		navTextHeight = parseInt(navTextHeight.replace("px", ""));
		navTextPaddingTop = parseInt(navText.css("padding-top").replace("px", ""));
		navTextPaddingBottom = parseInt(navText.css("padding-bottom").replace("px", ""));
		
		var newHeaderHeight =  headerHeight + navTextHeight + navTextPaddingTop + navTextPaddingBottom;
		newHeaderHeight = newHeaderHeight + 'px';
		
		if($(window).width() < 750)
			navText.parent().parent().css("height", newHeaderHeight);
			
			
		
	}
	
	if($("#map").length) {
		if($(window).width() >= 750) {
	
			$("#map").attr("src", "img/map-980.gif");
		}
		else if($(window).width() > 400) {
			$("#map").attr("src", "img/map-750.gif");
		}
		else if($(window).width() <= 400) {
			$("#map").attr("src", "img/map-400.gif");
		}
	}
	
	$(window).resize(function() {
		
		if($(window).width() != windowWidth) {
			windowWidth = $(window).width();
			
			// map resizing depending on the width of the window
			if($("#map").length) {
				if($(window).width() >= 750) {
			
					$("#map").attr("src", "img/map-980.gif");
				}
				else if($(window).width() > 400) {
					$("#map").attr("src", "img/map-750.gif");
				}
				else if($(window).width() <= 400) {
					$("#map").attr("src", "img/map-400.gif");
				}
			}
			
			if($(window).width() >= 750) {
				$(".nav").css("height", "auto");
				$(".navText").css("position", "static");
			}
			else {
				$(".navText").css("position", "absolute");
				if($(".openNav").html()) {
					openNavText($(".openNav").parent().parent().next());	
				}
			}
		}
		
	});

	var projLi = $("#projectsList li.projectBlock");
	
	$('.nav a').click(function() {
		// changing the arrow for the nav tab that is already open
		
		var curentNavLink = $(this);
		var navText = $(this).parent().parent().next();
		
		if(! navText.html()) {
			return
		}
		
		$(".openNav").parent().css("background", "url(./img/arrow_u.gif) center right no-repeat");
		$(".openNav").removeClass("openNav");

		// changing the arrow for the nav that clicked to be open
		$(this).parent().css("background", "url(./img/arrow_d.gif) center right no-repeat");
		
		if(navText.css("display") == "none") {
			$(this).addClass("openNav");
			openNavText(navText);
			
			$("#projectsList li.projectBlock").remove();
			var linkclass = $.trim(curentNavLink.attr("rel"));
			$("#projectsList").css('display', 'none');
			$.each(projLi, function(index, value) { 
		
				if($(this).hasClass(linkclass)) {
					$("#projectsList").append($(this));
				}
			});
			
			$("#projectsList").fadeIn(1000);
		}
		else {
			if($(window).width() >= 750) {
				$(".nav").css("height", "auto");
			}
			else {
				$(".nav").css("height", "28px");
			}
			
			$(this).removeClass("openNav");
			$(this).parent().css("background", "url(./img/arrow_u.gif) center right no-repeat");
			navText.css("display", "none");
			
			$("#projectsList li.projectBlock").remove();
			$("#projectsList").css('display', 'none');
			$.each(projLi, function(index, value) { 
		
				$("#projectsList").append($(this));
			});
			
			$("#projectsList").fadeIn(1000);

		}
		
		return false;
	});
   	
});
