$( document ).ready( function () {

	var projects = 6;	
	var archives = 6;
	var sections = ["recent", "archive"];
	var doubles = ["contact"]; 
	var double_pages = ["contact-jobs"]; 
	var pages = ["home", "contact"]; 

	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 #slideshow > *").hide();
		$("#page-home #slideshow :first-child").show();
	
		$('#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 slide_switch() {
		
		clearInterval(window.sliding);		
		
		var $active = $("#page-home #slideshow > .active");
		
		var $next = $active.next();
			
		set_slideshow_interval();
				
		if ($next.attr("class") == "landing") {

			$('#slideshow').unbind('click');
			
			clearInterval(window.sliding);
			
		}

		$active.hide();
		$active.removeClass('active');
		
		$next.css({opacity: 0.0})
			.show()
			.addClass('active')
			.animate({opacity: 1.0}, 1000, function() {
				//$active.removeClass('active')
			});	

	}
	
	
	function set_slideshow_interval() {
		window.sliding = setInterval(function() {
			slide_switch();
			}, 5000);
	}
	
	
	

	
	
	
	function start_intro() {
		
		$("#page-home #slideshow img:first").addClass('active');
		
		$("#slideshow").click(slide_switch);
		
		set_slideshow_interval();
		
	}
	
	
	
			
});
