$( document ).ready( function () {

	var projects = 6;	
	var archives = 6;
	var sections = ["recent", "archive"]; //, "awards"
	var doubles = ["contact"]; //, "about""awards", 
	var double_pages = ["contact-jobs"]; //, "awards-publications","awards",  "awards-other", , "about", "about-people"
	var pages = ["home", "contact"]; //, "awards","about"

	function hide_all_projects() {   
	$('#projects').hide();
	};
	
	
	function hide_all_archives() {
	for (i=1; i <= archives; i++) {  	
	    	$('#archive'+i).hide();
		$('#archive'+i+'-description').hide();
	};
	};

	function hide_all_pages() {
	for (i=0; i < pages.length; i++) {  	
    	$('#page-'+pages[i]).hide();
	};
	};
	function hide_all_double_pages() {
	for (i=0; i < double_pages.length; i++) {  	
    	$('#page-'+double_pages[i]).hide();
	};
	};
	
	
	function hide_all() {
	hide_all_projects();
	hide_all_archives();
	hide_all_pages();
	hide_all_double_pages();
	}
	
	// Set up page links
	$.each(pages, function(i,val){ 
	$('a#link-'+pages[i]).click(function(){
		hide_all();
		$('#archive-items:visible').slideToggle();
		$('#recent-items:visible').slideToggle();
		//$('#awards-items:visible').slideToggle();
		$('#contact-items:visible').slideToggle();
		//$('#about-items:visible').slideToggle();
		$("#page-"+pages[i]).fadeIn();
	});
	});
	
	// Set up double page links
	$.each(double_pages, function(i){  //,val
	$('a.'+double_pages[i]+'-link').click(function(){
		hide_all();
		$("#page-"+double_pages[i]).fadeIn();
	});
	
	});
	
	function setup_link(which) {		
		$("a.link"+which).click(function(j) {
			hide_all();
			$('#project'+which).fadeIn();
			$('#project'+which+'-description').fadeIn();
		});
	};
	
	function setup_links() {
		for (j=1; j <= projects; j++) {
			setup_link(j);
		}
	} 
	
	setup_links();
	
	hide_all();
	
	
	if ($(".homepage").length > 0) {
		
	$("#page-home div:first").hide();
	$("#page-home div.landing").hide();
	
	$('#page-home').fadeIn(function(){ start_intro() });
		
	}
	else {
		
		$('#page-home').fadeIn();
		
	}


	jQuery.each(sections, function() { 
		$('#'+this+'-items').hide();
		setup_accs(this);
	});
	
	
	function setup_accs(wha) {
	$('a#link-'+wha).click(function() {
		$.each(sections, function() { $('#'+this+'-items').hide(); });
		$.each(doubles, function() { $('#'+this+'-items:visible').hide(); });
		$('#'+wha+'-items').slideToggle('normal');
		});
	}


	jQuery.each(doubles, function() { 
		$('#'+this+'-items').hide();
		setup_doubles(this);
	});
	
	
	
	function setup_doubles(hmm) {
	$('a.link-'+hmm).click(function() {
		hide_all();
		$.each(sections, function() { $('#'+this+'-items:visible').hide(); });
		$('#'+hmm+'-items:hidden').slideToggle('normal');
		if ($('#page-'+hmm).length > 0) {$('#page-'+hmm).fadeIn();}
		});
	}

	$('.ajax').click(function() { 
		hide_all();
		$("#projects").hide();
		$("#projects .project").hide();
		$("#loading").fadeIn();
		$("#projects").empty().load($(this).attr('href')+" .project", function() {
			$("#projects .project").hide();
			$("#projects .project .csw").codaSlider(); 
			$("#loading").fadeOut(); 
			$("#projects").show(); 
			$("#projects .project").fadeIn(); 
		}); 
		return false; // Don't follow URL
	});
	
	$('.ajax-reverse-order').click(function() { 
		hide_all();
		$("#projects").hide();
		$("#projects .project").hide();
		$("#loading").fadeIn();
		$("#projects").empty().load($(this).attr('href')+" .project", function() {
			$("#projects .project").hide();
			$("#projects .project .csw .panelContainer .panel").reverseOrder();
			total_panels = $("#projects .project .new-description div");
			myvar = total_panels.length;
			total_panels.each(function(counter){
				this.id = "slide"+(myvar-counter);
			});
			$("#projects .project .csw").codaSlider(); 
			$("#loading").fadeOut(); 
			$("#projects").show(); 
			$("#projects .project").fadeIn(); 
		}); 
		return false; // Don't follow URL
	});
	
	
	$('#menu').css("position", "relative");
	$('#menu').scrollFollow({ container: 'page' });
	
	
	
	
	
	
	function start_intro() {

	$("#page-home div:first").animate({opacity: 1.0}, 1000).fadeIn().animate({opacity: 1.0}, 5000).fadeOut(function(){ show_last_one() });
	
	}
	
	function show_last_one() {
		$("#page-home div.landing").animate({opacity: 1.0}, 1000).fadeIn();
	}
	
	
			
});
