$(document).ready(function() {

	/* SITEWIDE*/

	// make sidebar the same height as the main section
	window.onload = function(){
		equalizeHeights(true);
	};

	function equalizeHeights(first) {
		mainheight = $("section#main").first().height();
		sectionheight = $("section#sidebar").first().height();
		if (mainheight > sectionheight || !first) {
			$("section#sidebar").height(mainheight);
		} else {
			$("section#main").height(sectionheight);
		}
	}
	/* HOMEPAGE */
	
	/* HOMEPAGE CAROUSEL */
	$("body.home .scrollable ").scrollable({
		next: '.next, .scrollable .items img',
		circular: true,
		speed: 2000,
		onBeforeSeek: function(event, index){
			// to do before the scroll:
			// fade out text
			$(".captions div").fadeOut();
		},
		onSeek: function(event, index){
			// to do after the scroll
			// fade in text
			$(".caption"+index).fadeIn();
			$(".current").html(index+1);
		}
	}).autoscroll({
		autoplay: false,
		interval: 11000
	});

	/* LANDSCAPE CAROUSEL */
	$("body.landscapes:not(.gazetteer) .scrollable ").scrollable({
		next: '.next, .scrollable .items img',
		circular: true,
		speed: 2000,
		onBeforeSeek: function(event, index){
			$(".captions div").fadeOut();
		},
		onSeek: function(event, index){
			$(".caption"+index).fadeIn();
			$(".current").html(index+1);
		}
	});

	/* GAZETTEER CAROUSEL */
	$("body.gazetteer .scrollable").scrollable({
		next: '.next, .scrollable .items img',
		circular: true,
		speed: 2000,
		onBeforeSeek: function(event, index){
			//	// to do before the scroll:
			//	// fade out text
			var wrapper = this.getItemWrap();
			$(wrapper).parent(".scrollable").siblings(".captions").find("div").fadeOut();
		},
		onSeek: function(event, index){
			// to do after the scroll
			// fade in text
			var wrapper = this.getItemWrap();
			$(wrapper).parent(".scrollable").siblings(".captions").find(".caption"+index).fadeIn();
			$(wrapper).parent(".scrollable").siblings(".carousel-nav").find(".current").html(index+1);
		}
	});
	
	/* GAZETTEER PROJECTS */
	$(".projects > p").click(function() {
		$(this).next().slideToggle("fast", function() {
			equalizeHeights();
		});
	});
	
	$(".projects h2").click(function() {
		opening = $(this).next();
		$(this).parents('ul').find('div:visible').not(opening).slideUp("fast", function() {
			equalizeHeights();
		});
		opening.slideToggle('fast', function() {
			equalizeHeights();
		});
		return false;
	});

	/* IMAGE CAROUSEL POPUP */

	// popup carousel
	$("#popup .scrollable").scrollable({
		circular: false,
		speed: 2000,
		onSeek: function(event, index){
			count = this.getItems().length;
			$("div.counter").text(index+1+" of "+count);
		}
	});

	// Generic image popup code - should be consistent
	$("body:not(.contact) section#main > img").overlay({
		mask: {
			color: '#000000',
			loadSpeed: 200,
			opacity: 0.8
		},
		target: '#popup',
		top: 'center'
	 });
	
	/* CONTACT PAGE */
	if ($("body").hasClass("contact")) {
		$("body.contact #contactFade").cycle({
			timeout: 7000
		})
	};

	/* NEWS PAGE */
	$("body.news div#share");
	$("body.news #s2submit").click( function() {
		$("#s2form").submit();
	});

	
	/* FOOTER LINKS POPUP */
	
	$("#page_footer a").overlay({
		mask: {
			color: '#000000',
			loadSpeed: 200,
			opacity: 0.8
		},
		target: '#popup-text',
		onBeforeLoad: function() {
			// grab wrapper element inside content
			var wrap = this.getOverlay().find("#wrapper");

			// load the page specified in the trigger
			wrap.load(this.getTrigger().attr("href"));
		}
		
	})
	
});
