var sup;

jQuery(function($){
		sup = $.supersized({
	
		//Functionality
		slideshow               :   1,		//Slideshow on/off
		autoplay				:	1,		//Slideshow starts playing automatically
		start_slide             :   1,		//Start slide (0 is random)
		random					: 	1,		//Randomize slide order (Ignores start slide)
		slide_interval          :   7000,	//Length between transitions
		transition              :   1, 		//0-None, 1-Fade, 2-Slide Top, 3-Slide Right, 4-Slide Bottom, 5-Slide Left, 6-Carousel Right, 7-Carousel Left
		transition_speed		:	1000,	//Speed of transition
		new_window				:	1,		//Image links open in new window/tab
		pause_hover             :   0,		//Pause slideshow on hover
		keyboard_nav            :   0,		//Keyboard navigation on/off
		performance				:	3,		//0-Normal, 1-Hybrid speed/quality, 2-Optimizes image quality, 3-Optimizes transition speed // (Only works for Firefox/IE, not Webkit)
		image_protect			:	1,		//Disables image dragging and right click with Javascript
		image_path				:	'img/', //Default image path

		//Size & Position
		min_width		        :   0,		//Min width allowed (in pixels)
		min_height		        :   0,		//Min height allowed (in pixels)
		vertical_center         :   1,		//Vertically center background
		horizontal_center       :   1,		//Horizontally center background
		fit_portrait         	:   0,		//Portrait images will not exceed browser height
		fit_landscape			:   0,		//Landscape images will not exceed browser width
		
		//Components
		navigation              :   1,		//Slideshow controls on/off
		thumbnail_navigation    :   0,		//Thumbnail navigation
		slide_counter           :   1,		//Display slide numbers
		slide_captions          :   1,		//Slide caption (Pull from "title" in slides array)
		slides 					: 	[
										{image : ''},  
										{image : ''}
		]
								
	}); 
   });

var initSplash;
var state = "splash";
var contSplash = true;
var slideshowActive = false;
var currentProjectImage;
var darkOverlayVisible = false;
var currentSlideshow;
var sortCol;
var sortOrder;
var galleryInterval;
var smallGalleryInterval;
var textInterval;
var galleryIdle;
$.idleTimer();
navAnimating = false;

//preload image loader
pic1= new Image(); 
pic1.src="../images/ajax-loader-big.gif"; 

$(document).ready(function(){

		$('.navigation a').click(function(){
			if(($(this).hasClass('services') ||$(this).hasClass('news')||$(this).hasClass('ethos')||$(this).hasClass('team')||$(this).hasClass('careers')) && $(this).hasClass('active')){
				clearInterval(textInterval);
				textNext();
				textInterval = setInterval(textNext,8000);
			}
		});
		
		//Hide spinner when window load completes
		$(window).load(function(){
			$("#ajax-loader").css("background-image","none");
			setTimeout(function(){
				$("#ajax-loader").fadeOut(1000, function(){
					$.idleTimer('destroy');
					var temp = location.href.split('/');
					if((temp[temp.length-2]=='projects') || temp[temp.length-1]=='contact' || temp[temp.length-1]=='copyright' || temp[temp.length-2]=='principals'){}
					else {initSlideshowTimer();}
				});
				
			},250);
			if($.address.value() == '/'){
				initSplash();
				state="splash";
			}
		});

		navFilter = function(el) {
	        return $(el).attr('href').replace(/^#/, '');
	    };

		$("#footer a[href!='#'][target!='_blank']").address(function(){
			return navFilter(this); 
		});
		
		$("#footer a[href!='#'][target!='_blank']").each(function(){
			tmp = $(this).attr('href').substring(1).replace('.html','');
			$(this).attr('href','#'+tmp);
		});
		


		$.address.change(function(event) {
			subpath = event.value;
			param = event.value.substring(1).split('/');
			paramLast = param[param.length-1];
			(param.length-2 >-1)? param2ndLast = param[param.length-2]: param2ndLast = null;
			(param.length-3 >-1)? param3rdLast =  param[param.length-3]: param3rdLast =  null;
			paramOrig = null;
			
			if (event.value=="/"){
				findSlideshow('home');
				changeState("home");
			}
			
			else {
				$(document).mousemove(function(){
					$("#overlay").fadeOut(2000);
				});
				exitSlideshow();
				if ((param2ndLast == 'projects') && (paramLast != 'project-list') && (paramLast.indexOf('?') == -1) && ($('.nav1 a.projects').hasClass('active'))){ }
				
				else {
					
				/*	if ((param2ndLast == 'projects') && (paramLast != 'project-list') && (paramLast.indexOf('?') == -1)) {
					//make ".nav1 a.projects" active if view projectpage from external	
						paramOrig = param;
						param = ['projects'];

					} */

					//handle navigation menu
					(paramOrig)?param=paramOrig:"";
					diffCol = null;
					
					for (i=0; i<(param.length); i++){

						thisCol = $('.nav'+(i+1));
						nextCol = $('.nav'+(i+2));
						var selection = param[i];

						if (thisCol.find('a.'+selection).hasClass('active') && ((param.length - i)!=1)){
						}
						else {
							(thisCol.find('a.'+selection).hasClass('active'))?lastCol=true:lastCol=false;
							diffCol = thisCol;
							moreCols = ((param.length-1)-i);
							break;
						}
					}
					
					if (diffCol){
						if (lastCol){
							nextCol.nextAll().children().removeClass('active').fadeOut(500);
							thisCol.nextAll().find('a.active').removeClass('active');
						}
						else {
							navAnimating = true;
							var anim = diffCol.nextAll().children().fadeOut(500).removeClass('active');
						}
						
						diffCol.find('a.active').removeClass('active');
						diffCol.find('a.'+selection).addClass('active');
						nextCol.find('a.active').removeClass('active');
						if (anim){
							$.when(anim).done(function(){
								navAnimating = false;
								navAnim = nextCol.children('.'+selection).addClass('active').fadeIn(500);	
							});
						}
						
						if (moreCols >0){
							for(i=moreCols;i>0;i--){
								diffCol.next().find('a.'+param[param.length-i]).addClass('active');
								diffCol.next().next().children('.'+param[param.length-i]).addClass('active').fadeIn(500);
								diffCol = diffCol.next();
							}
						}
					}
					
					
					//end handle navigation
				}
				
				$("#splash, #overlay").hide();
				
				var test = param.join("");
				(test.indexOf('careers') >-1)?$('.sidebox a.careers').addClass('active'):$('.sidebox a.careers').removeClass('active');
				(test.indexOf('contact') >-1)?$('.sidebox a.contact').addClass('active'):$('.sidebox a.contact').removeClass('active');
				if (test.indexOf('?') > -1){ // interrupt pageHandler if "?" in path
					if ((param[0] == 'projects') && param.length >2){ //projects filter
						filterProjects(paramLast);
					}
					if (test.indexOf('projects')>-1){
						$("#content-sneak #text-holder-normal").empty();
					}
					$('#text-holder-normal .overview').hasClass('dark-overlay')?"":$('#dark-overlay').fadeOut(500);
					
					
				}
				else {
					//$.when(navAnim).done(function(){pageHandler()});
					pageHandler();
				}
				if (subpath.indexOf('careers')>-1){
					$("#contact-text").fadeIn(500);
				}
				else {
					$("#contact-text").fadeOut(500);
				}
					findSlideshow(paramLast);
				
				
			}
			
		}); 


		$('a').live("click", function(event) { 
		($(this).attr('target') == '_blank' || $(this).attr('rel')=='external')?'':event.preventDefault();
			$(document).trigger("click");
		});

		$('.projects-filter a').click(function(){
			if ($(this).hasClass("active")){}
			else {
				$(this).closest('ul').find('a.active').removeClass('active');
				$(this).addClass('active');
				if($(this).attr('href')=='#'){
					var firstClass = $(this).attr('class').split(' ')[0];
					thisProj = $('.nav5 .'+firstClass).addClass('active');
					var anim = $('.nav5').children().removeClass('active').fadeOut(500);
					$.when(anim).done(function(){
						var arg = $('.nav3').find('a.active').attr('class').split(' ')[0];
						thisProj.find('a').each(function(){
							($(this).hasClass(arg))? $(this).parent().show():$(this).parent().hide();
						});
						thisProj.children('a.active').removeClass('active');
						thisProj.fadeIn(500);
						(thisProj.hasClass('nav-scrollbar'))?thisProj.tinyscrollbar_update():"";
					});
				}
				else{
					$('.nav5').children().fadeOut(500).removeClass('active');
				}
			}
		});
		$('.sub-filter a').click(function(){
			if ($(this).hasClass("active")){}
			else {
				$(this).closest('ul').find('a.active').removeClass('active');
				$(this).addClass('active');
			}
		});
		$('.sidebox a.careers').click(function(){
			//$('.projects-filter').fadeOut(500);
		});
		
		initSplash = function(){
			//Initial splash logic
			$.idleTimer('destroy');
			setTimeout(function(){ //show splash content
				if (contSplash == true) {
					$("#splash .content").fadeIn(2000);
					setTimeout(function(){ //reveal home page
						$("#splash .content").fadeOut(2000);
						setTimeout(function(){
							$("#splash").fadeOut(2000, function(){
								initSlideshowTimer();
								setTimeout(function(){
									$(document).mousemove(function(){
										$("#overlay").fadeOut(2000);
									});
								}, 2000);
							});
							state = "home";							
						},1500);					
					},5000);
				}
			}, 3000);
		}

	$(document).click(function(e){
		if(state == "splash") {
			$("#splash").fadeOut(3000);
			state = "home";
			initSlideshowTimer();
			setTimeout(function(){
				$(document).mousemove(function(){
					$("#overlay").fadeOut(2000);
				});
			}, 2000);
			contSplash = false;
		}
	});
	
	
/*	$('.nav5 .sub-filter').each(function(){
		if ($(this).children('ul').height() > 135){
			var legacy = $(this).children('ul');
			legacy.wrap('<div class="nav-scrollbar" />')
				.wrap('<div class="viewport" />')
				.parent().before('<div class="scrollbar"><div class="track"><div class="thumb"><div class="end"></div></div></div></div>');
			legacy.addClass("overview");
		}
	});
*/	
	
	//scrollbar
	$(".nav-col ul").each(function(){
		if (($(this).height() > 135) && $(this).parent().hasClass('nav-col')){
			var classAttr = $(this).attr("class");
			$(this)
				.removeClass()
				.wrap('<div class="'+classAttr+' nav-scrollbar" />')
				.wrap('<div class="viewport"/>')
				.parent().before('<div class="scrollbar"><div class="track"><div class="thumb"><div class="end"></div></div></div></div>');
			$(this).addClass("overview");
		}
	});
	$(".nav-scrollbar").tinyscrollbar({sizethumb: 20, wheel: 57});
	$(".text").tinyscrollbar({sizethumb: 50, wheel: 57});
	
	//hide submenus at the start
	$(".nav1").nextAll().children().hide();

	//show more - project
	$("#project-holder .left-col a.more").live("click",function(){
		clearInterval(galleryInterval);
		clearTimeout(galleryIdle);
		currHeight = $('#project-holder .overview .description').height();
		$("#project-holder .description").css("height","auto");
		$("#project-holder .description").css("overflow","visible");
		$(this).hide();
		$("#project-holder .text-fade").fadeOut(500);
		$(this).siblings("a.less").css("display","block");
		$("#project-holder .description a.less").css("display","block");
		$("#project-holder .text").tinyscrollbar_update();
	});
	
	//show less - project
	$("#project-holder").delegate("a.less","click",function(){
		//$("#project-holder .viewport").css("height",currHeight2 + 'px');
		$("#project-holder .description").css("height",currHeight+'px');
		$("#project-holder .description").css("overflow","hidden");
		$("#project-holder .text").tinyscrollbar_update();
		$(this).hide();
		$("#project-holder a.more").show();
		$("#project-holder .text-fade").fadeIn(500);
	});
	
	//show less - gallery
	$("#project-gallery a.less").click(function(){
		exitGallery();
	});
	
	$(document).keyup(function(e) {
	  if (e.keyCode == 27) { exitGallery(); }   // esc
	});
	
	//exit Gallery
	function exitGallery(){
		if ($("#project-gallery").is(':visible')) {
			$("#project-gallery .image-holder img").die('click',imgClick);
			clearInterval(galleryInterval);
			clearTimeout(galleryIdle);
			$("#project-gallery").fadeOut(1000);
			$('#project-holder .image-holder img:visible').hide();
			$('#project-holder .image-holder img:nth-child('+(currentProjectImage+1)+')').fadeIn(500);
			(imageCount > 1)?smallGalleryInterval = setInterval(smallGalleryNext,8000):"";
		}
	}
	
	//click slideshow pagination
	$("#project-gallery ul").delegate("a","click",function(){
		clearInterval(galleryInterval);
		clearTimeout(galleryIdle);
		if($(this).parent().hasClass("active")){
		}
		else {
			$(this).parents("ul").children("li.active").removeClass("active");
			$(this).parent().addClass("active");
			currentProjectImage = $(this).parent().index();
			$("#project-gallery .image-holder").html('<img src="'+$('#project-holder .image-holder img:nth-child('+(currentProjectImage+1)+')').attr('src')+'" />');
			$("#project-gallery img").imgscale({
				center: true,
				scale:"fit",
				fade:500
			});
		}
		galleryIdle = setTimeout(function(){
			galleryInterval= setInterval(galleryNext, 8000);
		},8000);

		return false;
	});
	
	//show more - text-holder
	$("#text-holder-normal .expand a.more").live('click',function(){
		clearInterval(textInterval);
		$("#text-holder-normal .expand .text-item:visible .hidden").fadeIn(500);
		$(this).hide();
		var getheight = $(this).parent('.overview').height();
		if (getheight <474){
			$(this).closest('.viewport').css('height',getheight+'px').css('margin-bottom','-10px');
		}
		else {
			if ($(this).closest('left-col').hasClass('special')){
				$(this).closest('.viewport').css('height','475px');
			}
			else {
				$(this).closest('.viewport').css('height','445px');
			}
		}
		$(this).parents('.viewport').siblings("a.less").fadeIn(500);

		$("#text-holder-normal .text").tinyscrollbar_update();
		$("#dark-overlay").fadeIn(500);
	});
	
	//show less - text-holder
	$("#text-holder-normal a.less").live('click',function(){
		$("#text-holder-normal .expand .hidden").fadeOut(500);
		$(this).fadeOut(500, function(){
			$(this).siblings('.viewport').css('height','508px');
			$(this).siblings(".viewport").find('a.more').fadeIn(500);
			$("#text-holder-normal .text").tinyscrollbar_update();
		});
		$("#content-sneak .overview").hasClass('dark-overlay')?"":$("#dark-overlay").fadeOut(500);
		($('#text-holder-normal .overview').hasClass('rotate'))?textInterval = setInterval(textNext,8000):"";
	});
	
	//show more - copyright
	$("#copyright-holder a.less").hide();
	$("#copyright-holder a.more").live('click',function(){
		$("#copyright-holder .expand .text-item:visible .hidden").fadeIn(500);
		$(this).hide();
		var getheight = $(this).parent('.overview').height();
		if (getheight <474){
			$(this).closest('.viewport').css('height',getheight+'px').css('margin-bottom','-10px');
		}
		else {
			if ($(this).closest('left-col').hasClass('special')){
				$(this).closest('.viewport').css('height','475px');
			}
			else {
				$(this).closest('.viewport').css('height','445px');
			}
		}
		$(this).parents('.viewport').siblings("a.less").fadeIn(500);

		$("#copyright-holder .text").tinyscrollbar_update();
	});
	
	//show less - copyright
	$("#copyright-holder a.less").live('click',function(){
		$("#copyright-holder .expand .hidden").fadeOut(500);
		$(this).fadeOut(500, function(){
			$(this).siblings('.viewport').css('height','508px');
			$(this).siblings(".viewport").find('a.more').fadeIn(500);
			$("#copyright-holder .text").tinyscrollbar_update();
		});
	});
	
	
	//project-list table sorter	
	$("#project-list-holder .header span").live('click',function(){
		switch($(this).attr("class")){
			case "name":
				(sortCol== 0) ? sortReverse() : sortColOrder(0,0);
				break;
			case "category":
				(sortCol== 1) ? sortReverse() : sortColOrder(1,0);
				break;
			case "location":
				(sortCol== 2) ? sortReverse() : sortColOrder(2,0);
				break;
			case "scope":
				(sortCol== 3) ? sortReverse() : sortColOrder(3,0);
				break;
			case "range":
				(sortCol== 4) ? sortReverse() : sortColOrder(4,0);
				break;
		}
		$("#project-list").trigger("sorton",[[[sortCol,sortOrder]]]);
		return false;
		
		function sortReverse(){
			(sortOrder == 0) ? sortOrder = 1 : sortOrder = 0;
		}
		function sortColOrder(col, order) {
			sortCol = col;
			sortOrder = order;
		}
	});
	
	//handle contact map
	$('a.wow-location').live('click',function(){
		$(this).animate({
			opacity:0
		},500);
		if ($(this).hasClass('world-map')){
			$('#contact-holder img.world-map').fadeOut(500,function(){
				$('#contact-holder img.local-map').fadeIn(500);
				$('a.wow-location').removeClass('world-map').addClass('local-map').animate({opacity:1},500);
			});
		}
		
		else if ($(this).hasClass('local-map')) {
			$('#contact-holder img.local-map').fadeOut(500,function(){
				$('#contact-holder img.world-map').fadeIn(500);
				$('a.wow-location').removeClass('local-map').addClass('world-map').animate({opacity:1},500);
			});
		}
		
	});
	
	//handle fullscreen
	$('#footer-container a.fullscreen').live('click',function(){
		window.open($(location).attr('href'),'','type=fullWindow,fullscreen=yes,scrollbars=yes,width='+screen.availWidth+',height='+screen.availHeight);
	});
	
//	disable right click
	$('#project-gallery .image-holder img, #project-holder .image-holder img, #supersized img').live("contextmenu",function(){
		return false;
	});
	$('#project-gallery .image-holder img, #project-holder .image-holder img, #supersized img').live("mousedown",function(){
		return false;
	});
	
}); //end document ready

function filterProjects(arg){
	arg = arg.substring(0,arg.length-1); //remove "?"
	if ($(".projects-filter").is(":visible")){
		$(".projects-filter").fadeOut(500, function(){
			filter();
		});
	}
	else {
		filter();
	}
	
	function filter(){

		$(".projects-filter li a").each(function(){
			($(this).hasClass(arg))? $(this).parent().show():$(this).parent().hide();
		});
		$(".projects-filter").fadeIn(500);
		$(".projects-filter").tinyscrollbar_update();

	}
}

function initSlideshowTimer(){	
		$.idleTimer('destroy');
		$.idleTimer(20000);
		$(document).bind("idle.idleTimer", function(){
			($("#dark-overlay").is(":visible"))?darkOverlayVisible = true:darkOverlayVisible = false;
			$("#main, #footer-container, #dark-overlay").fadeOut(2000);
			$("#slidecaption-holder, .copyrighttext-holder").fadeIn(2000);
		//	state = "slideshow";
			slideshowActive = true;
		});
		$(document).bind("active.idleTimer", exitSlideshow);
}

function exitSlideshow(){
	$("#footer-container").stop().fadeTo(2000,1);
	$("#slidecaption-holder").stop().fadeOut(2000);
	setTimeout(function(){
		$(".copyrighttext-holder").stop().fadeOut(2000);
	},2000);
	$("#main, #footer-container").stop().fadeTo(2000,1);
	(darkOverlayVisible)?$("#dark-overlay").stop().fadeTo(2000,1):"";
	slideshowActive = false;
}

function loadAjax(path, target, callback) {
//	alert(path);
	$(target).load(path + ' #ajax-content', function(){
		var ajaxContent = $(this).find('#ajax-content').html();
		$(this).html(ajaxContent);
		
		if(callback) {
			callback();
		}
	});
	// Sets the page title
/*   $('#title').load(path+' #title', function(){
		$.address.title($(document).find('#title').text());
	});*/
}

function findSlideshow(set){
//change background images if new ones exist

	if (slideshowLinks[set]) {
			sup.stopSlideshow();
		if (param[param.length-2]=='principals'){
			$('#white-overlay').fadeIn(500, function(){
				loadSlideshow(set,'principal');
			});
		}
		//changeState("content-sneak");
		else {
			loadSlideshow(set);
		}
	}
	else{
		if (set == 'services' || (param[param.length-2] == 'services')|| set == 'principals' || set == 'team' || set == 'news' || (param[param.length-2] == 'news') || (param[param.length-3] == 'news') ){
			(currentSlideshow != 'practice')?loadSlideshow('practice'):"";
		}
		else if (param[param.length-2] == 'ethos'){
			(currentSlideshow != 'ethos')?loadSlideshow('ethos'):"";
		}
		else if (param.join("").indexOf('careers') >-1){
			(currentSlideshow != 'careers')?loadSlideshow('careers'):"";
		}
		else if (param[0]=='projects'){
			(currentSlideshow != 'projects')?loadSlideshow('projects'):"";
		}
		else if (set=='contact'){
			(currentSlideshow != 'home')?loadSlideshow('home'):"";
		}
		else if (set=='principals?'){
			(currentSlideshow != 'team')?loadSlideshow('team'):"";
		}

	}

}

function loadSlideshow(set, arg){
	if (arg=='principal'){
		$("#supersized a:nth-child(2)").html('<img src="'+slideshowLinks[set][0].image+'" />');
		$("#supersized a:nth-child(2) img").load(function(){
			sup.resizenow();
			$('#white-overlay').delay(500).fadeOut(1000);
		});
	}
	
	else {
		var randomNum = Math.floor(Math.random()*(slideshowLinks[set].length)); // Pick random number from array
		//	var randomNum =0;
		$("#supersized a:nth-child(3)").html('<img src="'+slideshowLinks[set][randomNum].image+'" />');	
/*		if (arg=='principal'){
			sup.setSlides(slideshowLinks[set],arg);
		}
		else{*/
			sup.setSlides(slideshowLinks[set]);
//		}

		(set!='home')?$("#overlay").fadeOut(2000):"";
	}
	

	currentSlideshow = set;
//	sup.resizenow;
}

function pageHandler(){
	// if /projects/XXX or /contact
	if((param[0]=='projects' && param[1]) || param[0]=='contact' || param[0]=='copyright') { //these are content-item pages
		
		if (state == "content-item") { // state is already content item
			
			$("#content-item").children().fadeOut(500);
		
			setTimeout(function(){
				(param[0]=='contact' || param[0]=='copyright') ? contentItemHandler(param[0]):contentItemHandler(param[1]);
			},500);
		}
		else { //state is not content item
			
			$("#content-item").children().hide();
			(param[0]=='contact' || param[0]=='copyright') ? contentItemHandler(param[0]):contentItemHandler(param[1]);
			changeState("content-item");
		}

	} //end content item pages
	
	else { // these are content-sneak pages

		var lastOne = param[param.length-1];

	
		if (state == "content-sneak") { //state is already content sneak
			darkOverlayVisible = false;
/*			$('#content-sneak').children().each(function(){
				($(this).is(':visible'))?$(this).fadeOut(500):$(this).hide();
			});*/
			if ($("#content-sneak").children(':visible').length > 0){
				var anim = $("#content-sneak").children(":visible").fadeOut(500);
				$.when(anim).done(function(){contentSneakHandler(lastOne)});
			}
			else {
				contentSneakHandler(lastOne);
			}
		}
		
		else { //state is not content-sneak
			$("#content-sneak").children().hide();
			contentSneakHandler(lastOne);
			changeState("content-sneak");
		}
		
		
	}

	function contentItemHandler(type){
//			$("#ajax-loader2").show();
		//handle contact
		if (type == 'contact') {
			loadAjax((subpath.substring(1) + ".html"), "#contact-holder", function(){
				$("#contact-holder").fadeIn(2000);
			});
		}
		
		else if (type=='copyright'){
			$.idleTimer('destroy');
			$("#project-gallery").fadeOut(500, function(){
				$("#copyright-holder").fadeIn(500);
			});
		}
		
		// else handle project-list
		else if (type == 'project-list'){
			loadAjax((subpath.substring(1) + ".html"), "#project-list-holder", projectListLoaded);
		}
		
		// else handle project-item
		else {
			loadAjax((subpath.substring(1) + ".html"), "#project-holder", projectLoaded);
		}
	
	}
	
	function projectLoaded(){
	/*	$('#ajax-loader2').fadeOut(500, function(){
			$("#project-holder").fadeIn(500);
			$("#project-holder .text").tinyscrollbar({sizethumb: 50, wheel: 57});
		});
		*/
//		$("#project-holder .right-col").prepend(imgLoader);
		
		
		var diffH2 = 0;
		$("#project-holder").fadeIn(500);
		if (($("#project-holder h2.name").height())>31){
			diffH2 = $("#project-holder h2.name").height()-31;
			var curr = $("#project-holder .viewport").height();
			var curr2 = $("#project-holder .overview .description").height();
			$("#project-holder .viewport").css('height', (curr-diffH2)+'px');
			$("#project-holder .overview .description").css('height', (curr2-diffH2)+'px');
		}
		if (($("#project-holder .overview .info").height())>108){
			diffInfo = $("#project-holder .overview .info").height()-108;
			var curr = $("#project-holder .overview .description").height();
			$("#project-holder .overview .description").css('height', (curr-diffInfo)+'px');
		}
		
		$("#project-holder .text").tinyscrollbar({sizethumb: 50, wheel: 57});

		function heightOfChildren(element){
		    var $heights = 0 ;
	        $(element).children().each(function(){
	            $heights = $heights + parseInt( $(this).height() ); // gets height of content
	            $heights = $heights + parseInt( $(this).css("margin-top") ); // adds height of box model
	            $heights = $heights + parseInt( $(this).css("margin-bottom") );
	            $heights = $heights + parseInt( $(this).css("border-top-width") );
	            $heights = $heights + parseInt( $(this).css("border-bottom-width") );
	            $heights = $heights + parseInt( $(this).css("padding-top") );
	            $heights = $heights + parseInt( $(this).css("padding-bottom") );
	        });
	        return $heights;
		}
		
		if( (heightOfChildren($("#project-holder .description")) + $("#project-holder .overview .info").height()) < ($("#project-holder .viewport").height())){
			$("#project-holder .left-col a.more, #project-holder .overview .text-fade").hide();
		}
	
		//handle project images
		
		$("#project-holder .image-holder img").each(function(){
			src = $(this).attr('src').substring(3);
			$(this).attr('src',src);
		});
		$("#project-holder .image-holder img").each(function(){
			$(this).
				imgscale({
						center: true,
						fade:500
				});

		});
		$("#project-holder .image-holder img:first").nextAll().hide();
/*		$("#project-holder .image-holder img:last").load(function(){
			setInterval(function(){
				$("#project-holder .right-col .image-loader").remove();
			},5000);
		});
*/		
		//handle image pagination
		imageCount = $("#project-holder .image-holder img").length;
		$("#project-holder .right-col a.more").show();
		currentProjectImage = 0;
		clearInterval(smallGalleryInterval);
		(imageCount >1)?(smallGalleryInterval = setInterval(smallGalleryNext,8000)):"";

		//show more - slideshow
		$("#project-holder .right-col a.more").live("click",function(){
			clearInterval(galleryInterval);
			clearInterval(smallGalleryInterval);
			clearTimeout(galleryIdle);
			$("#project-gallery").fadeIn(1000);
			$("#project-gallery .image-holder").html('<img src="'+$('#project-holder .image-holder img:nth-child('+(currentProjectImage+1)+')').attr('src')+'" />');
			$("#project-gallery .image-holder img").imgscale({
				center: true,
				scale:"fit"
			});
			$("#project-gallery ul").empty();
			$("#project-gallery .image-holder img").die('click',imgClick);
			for (i=0; i< imageCount; i++) {
				$("#project-gallery ul").append('<li><a href="#">'+(i+1)+'</a></li>');
			}
			$("#project-gallery ul li:nth-child("+(currentProjectImage+1)+")").addClass("active");
			if (imageCount>1){
				galleryInterval = setInterval(galleryNext, 8000);
				$("#project-gallery .image-holder img").css('cursor','pointer');
				$("#project-gallery .image-holder img").live("click",imgClick);
			}
				
			
		});
	}
	
	function projectListLoaded(){
		$("#project-list .name a[href!='#']").address(function(){
			return navFilter(this);  
		});

		$("#project-list .name a[href!='#']").each(function(){
			tmp = $(this).attr('href').substring(1).replace('.html','');
			$(this).attr('href','#'+tmp);

		});
		
		$("#project-list").tablesorter({
			sortList:[[0,0]]
		});
		
		/*$('#ajax-loader2').fadeOut(500, function(){
			$("#project-list-holder").fadeIn(500, function(){	
				$("#project-list-holder .text").tinyscrollbar({sizethumb: 50, wheel: 57});
			});
		});*/
		$("#project-list-holder").fadeIn(500, function(){	
			$("#project-list-holder .text").tinyscrollbar({sizethumb: 50, wheel: 57});
		});
	}
	
	function contentSneakHandler(type){
	//	$("#ajax-loader2").show();
		$.idleTimer('destroy');
		//handle no content

		//handle principals
		if (param[param.length-2] == 'principals') {
			loadAjax((subpath.substring(1) + ".html"), "#text-holder-principals", function(){
				$.idleTimer('destroy');
			/*	$('#ajax-loader2').fadeOut(500, function(){
					$("#text-holder-principals").delay(500).fadeIn(500, function(){
						$("#text-holder-principals .text").tinyscrollbar({sizethumb: 50, wheel: 57});
					});
					
				}); */
				$("#dark-overlay").fadeOut(500);
				$("#text-holder-principals").delay(500).fadeIn(500, function(){
					$("#text-holder-principals .text").tinyscrollbar({sizethumb: 50, wheel: 57});
				});
			});
		}
		
		//handle normal text
		else {
			loadAjax((subpath.substring(1) + ".html"), "#text-holder-normal",textLoaded);
		}

		if ((param[param.length-2] == 'publications') ||(param[param.length-2] == 'principals') || (param[param.length-1]=='awards')){
		}
		else{
			initSlideshowTimer();
		}
		
	}
	
	
	function textLoaded(){
		clearInterval(textInterval);
		($('#content-sneak .overview').hasClass('dark-overlay'))?$("#dark-overlay").fadeIn(500):$("#dark-overlay").fadeOut(500);
		$('#text-holder-normal .left-col a.less').hide();
		if ($('#text-holder-normal .overview > div').length >1){
			$('#text-holder-normal .overview > div').hide();
			var randomNum = Math.floor(Math.random()*($('#text-holder-normal .overview > div').length)); // Pick random number from array
			$('#text-holder-normal .overview > div:nth-child('+(randomNum+1)+')').show();
			($('#text-holder-normal .overview').hasClass('rotate'))?textInterval = setInterval(textNext,8000):"";
		}
		/*$('#ajax-loader2').fadeOut(500, function(){
			$("#text-holder-normal").fadeIn(500);
			$("#text-holder-normal .text").tinyscrollbar({sizethumb: 50, wheel: 57});
		})*/
		sneakAnim = $("#text-holder-normal").fadeIn(500);
		$("#text-holder-normal .text").tinyscrollbar({sizethumb: 50, wheel: 57});
		
		
		
/*		if ((param[param.length-2] == 'ethos') || (param[param.length-2] == 'ethos')) {
			$('#dark-overlay').fadeIn(500);
		} */
		if ((param[param.length-2] == 'publications') || (param[param.length-1]=='awards')){
			$.idleTimer('destroy');
		}
	}

}

function imgClick(){
	clearInterval(galleryInterval);
	clearTimeout(galleryIdle);
	galleryNext();
	galleryIdle = setTimeout(function(){
		galleryInterval= setInterval(galleryNext, 8000);
	},8000);
}

function galleryNext(){
	
	$("#project-gallery .image-holder").fadeOut(500, function(){
		if(currentProjectImage == (imageCount-1)){
			$("#project-gallery ul li.active").removeClass("active");
			$("#project-gallery ul li:first").addClass("active");
			currentProjectImage = 0;
		}
		else {
			$("#project-gallery ul li.active").removeClass("active").next().addClass("active");
			currentProjectImage = $("#project-gallery ul li.active").index();
		}
		
		$("#project-gallery .image-holder img").attr('src',$('#project-holder .image-holder img:nth-child('+(currentProjectImage+1)+')').attr('src'));
		$("#project-gallery img").imgscale({
			center: true,
			scale:"fit"
		});
		$("#project-gallery .image-holder").fadeIn(500);
	});
	
}

function smallGalleryNext(){
	var activeImage = $('#project-holder .image-holder img:visible');
	activeImage.fadeOut(500,function(){
		if (activeImage.index() == imageCount-1){
			$('#project-holder .image-holder img:first').fadeIn(500);
			currentProjectImage = 0;
		}
		else {
			activeImage.next().fadeIn(500);
			currentProjectImage = activeImage.next().index();
		}
	});
}

function textNext(){
	if ($('#text-holder-normal .overview > div:last').is(':visible')){
		$('#text-holder-normal .overview a.more').fadeOut(500);
		$('#text-holder-normal .overview > div:last').fadeOut(500, function(){
			$('#text-holder-normal .overview > div:first, #text-holder-normal .overview a.more').fadeIn(500);
		});
	}
	else {
		var nextone = $('#text-holder-normal .overview > div:visible').next();
		$('#text-holder-normal .overview a.more').fadeOut(500);
		$('#text-holder-normal .overview > div:visible').fadeOut(500, function(){
			nextone.fadeIn(500);
			$('#text-holder-normal .overview a.more').fadeIn(500);
		});
	}
}

function changeState(newState) {

	state = newState;
	switch(newState) {
		case "home":
			$(".nav1 li a").removeClass("active");
			$(".nav2, .nav3, .nav4, .nav5").children().fadeOut(500, function(){
				$(this).find(".active").removeClass("active");
			});
			$("#project-gallery, #slideshow-overlay .copyright, #slidecaption-holder, #content-item, #contact-text, #dark-overlay, #content-sneak, #footer-bg").fadeOut(2000);
			$("#overlay, #footer-container").fadeIn(2000);
			initSlideshowTimer();
			break;
		
		case "content-sneak":
			//$("#supersized, #main-content").show();
			//$("#project-gallery, #slideshow-overlay").fadeOut(2000);
			$("#content-item, #footer-bg").fadeOut(2000);
			$("#footer-container").fadeIn(2000);
			$("#content-sneak").fadeIn(2000,function(){
				$("#overlay").fadeOut(2000);
			});
			break;
			
		case "content-item":
			$.idleTimer('destroy');

//			$("#main-content").show();
			$("#content-sneak").fadeOut(2000);
			$("#content-item, #footer-bg").fadeIn(2000, function (){
				$("#overlay").hide();
			});
			break;
	}

}

