/**
 * Flax Facts JS functions
 *
 * Handles the fade in/out of lage slides at the top,
 * as well as the sliding of "buckets" in the bottom "tray."
 *
 * Roger Glenn 2010-01-12
 * roger@mach1media.com
 */

// custom :random function
// used in selecting a random "did you know" blurb
jQuery.jQueryRandom = 0;
jQuery.extend(jQuery.expr[":"],
{
	random: function(a, i, m, r) {
		if (i == 0) {
			jQuery.jQueryRandom = Math.floor(Math.random() * r.length);
		};
		return i == jQuery.jQueryRandom;
	}
});

// init
function initSlideshow() {
	// bind click listener to slider arrows
	$("#slide-left").bind("click", function(e){
		slideRight();
		return false;
	});
	$("#slide-right").bind("click", function(e){
		slideLeft();
		return false;
	});
	
	// did you know
	$("#trigger_didyouknow").bind("click", function(e){
		toggleDidYouKnow();
		return false;
	});
}

// toggleDidYouKnow
function toggleDidYouKnow() {
	$(".dyk:visible").fadeOut(500, function(){
		// fade in the new slide
		$(".dyk:random").fadeIn(500);
	});
}

// slide right
function slideRight() {
	$("#slider").animate({
			"left" : "+=940px"
		},
		"slow",
		"easeInOutCirc"
	);
}

// slide left
function slideLeft() {
	$("#slider").animate({
			"left" : "-=940px"
		},
		"slow",
		"easeInOutCirc"
	);
}

// show/hide slide
function showSlide(slide_id, slideBuckets) {
	// fade out the currently visible slide
	$(".slide:visible").fadeOut(1000, function(){
		// fade in the new slide
		$("#"+slide_id).fadeIn(1000);
		// reload sIFR fonts for previously hidden h1's
		// copied from sifr-config.js
		sIFR.replace(exuberanceBold, {
			selector: 'h1, h2',
			wmode: 'transparent',
			css: {'.sIFR-root': { 'color':'#6A4542','text-decoration' : 'none'},
				'a': {'color':'#62776B','text-decoration' : 'none'},
				'a:hover': {'color':'#62776B','text-decoration' : 'underline'}}
		});
	});
	
	// remove background-color from all buckets
	$(".bucket").removeClass("highlighted");
	
	// highlight bucket for this slide
	var string = new String(slide_id);
	id = string.slice(6);	
	$("#bucket_"+id).addClass("highlighted");
	
	// slide the buckets if necessary
	if (id <= 4 && slideBuckets) {
		slideRight();
	} else if (id > 4 && slideBuckets) {
		slideLeft();
	}
	
	// return
	return false;
}


$(document).ready(function(){
	// init
	initSlideshow();
});