/*
Name: World Council of Churches 2009 Homepage Feature
Author: Scott Lenger
Author URI: http://www.scottlenger.com/
Last Updated: June, 2009
 */

function featureToggle(sectionheading) {
	// remove active cloass
	$("ul#feature >li").removeClass("active");
	// hide all sections
	$(".section-wrap").hide();
	// add active class to parent div of selected heading
	$(sectionheading).parent().addClass("active");
	// show content of selected heading
	$(sectionheading).parent().find(".section-wrap").show();
}

// Toggle feature content
var divheight;
var sliderTimer;
var fullheight = $("ul#feature").height() + "px";

function openSlider(fullheight) {

	clearInterval(sliderTimer);
	if ($("ul#feature").hasClass("open")) {
		// if open, find total height
		divheight = $("ul#feature").height() + "px";
		// hide feature content
		$("ul#feature").animate( {
			"height" : "270px"
		}, {
			duration : "slow"
		});
		// remove classes
		$("ul#feature").removeClass("open");
		$("#details").removeClass("true");
	} else {
		// show feature content
		if (divheight == null) {
			divheight = fullheight;
		}
		// animaiton requires a fixed px height, this quickly resets it to 100%
		// so it stretches to accomodate different section heights
		$("ul#feature").animate( {
			"height" : divheight
		}, {
			duration : "slow"
		}).animate( {
			"height" : "100%"
		}, "fast");
		// add classes
		$("ul#feature").addClass("open");
		$("#details").addClass("true");
	}
}

// Slideshow
function slideSwitch() {
	// find active slide
	var active = $('ul#feature li.active');
	// see if active slide is last
	if ($(active).length == 0)
		$(active) = $('ul#feature li:last');
	// get the next slide
	var next = $(active).next().length ? $(active).next()
			: $('ul#feature li:first');
	// see notes in featureToggle()
	$("ul#feature >li").removeClass("active");
	$(".section-wrap").hide();
	$(next).addClass("active");
	$(next).find(".section-wrap").fadeIn();
}

// start slideshow
function startshow() {
	timer = setInterval(slideSwitch, 5000);
	// $("#stop_slideshow").show();
	// $("#start_slideshow").hide();
}

// stop slideshow
function stopshow() {
	// clear slide advance timer
	clearInterval(timer);
	// hide stop button
	// $("#stop_slideshow").hide();
	// dislay start button
	// $("#start_slideshow").show();
}

function feature() {
	// feature toggle
	var sliderHeight = "270px";
	fullheight = $("ul#feature").height() + "px";
	$("ul#feature").css("height", sliderHeight);

	// attach click behavior to details button
	$("#details").click(function() {
		stopshow();
		clearInterval(sliderTimer);
		openSlider(fullheight);
	});

	// Randomly select active feature panel
	// count the nodes in the feature
	var countsections = $("ul#feature > li").size();
	// old school javascript
	var getrandom = Math.floor(countsections * Math.random());
	// get random section element
	var rsection = $('ul#feature > li').eq(getrandom);
	// set active section
	$(rsection).addClass("active");
	// get section heading
	var sectionheading = $(rsection).children("h2");
	featureToggle(sectionheading);

	startshow();

	// make section headings clickable
	$('ul#feature > li > h2').hover(function() {
		// ignore active sectino (don't refresh
			clearInterval(sliderTimer);
			if ($(this).parent().hasClass("active")) {
				stopshow();
				// return;
			} else {
				// activate selected section
				featureToggle(this);
				stopshow();
			}
			if (!$("ul#feature").hasClass("open")) {
				// If nothing happens after a while, the slider opens
				sliderTimer = setInterval('openSlider(fullheight);', 4000);
			}
		});

	// The slider also opens when tab is clicked
	$('ul#feature > li > h2').click(function() {
		openSlider(fullheight);
	});
	// Cancel the auto unfold timer if mouse out of feature pannel
	$('ul#feature').hover(
			function () {void('');}, // hover in
			function () {clearInterval(sliderTimer);} // hover out
	);

}
