$(document).ready( function () {
	$('#flash-fact-1').flash({
		swf: '../swf/fact1.swf',
		play: true,
		wmode: 'transparent',
		height: 250,
		width: 626
	});
		
	$('#flash-fact-2').flash({
		swf: '../swf/fact2.swf',
		play: false,
		wmode: 'transparent',
		height: 250,
		width: 626
	});
	
	$('#flash-fact-3').flash({
		swf: '../swf/fact3.swf',
		play: false,
		wmode: 'transparent',
		height: 250,
		width: 626
	});
		
		
	$("#factscarousel").jcarousel({
		scroll: 1,
		wrap: 'both',
		initCallback: factscarousel_initCallback,
		// This tells jCarousel NOT to autobuild prev/next buttons
		buttonNextHTML: null,
		buttonPrevHTML: null,
		animation: "slow",
		itemVisibleInCallback: {
		   onBeforeAnimation: factscarousel_itemVisibleInCallbackBeforeAnimation,
		   onAfterAnimation:  factscarousel_itemVisibleInCallbackAfterAnimation
		},
		itemVisibleOutCallback: {
		   onBeforeAnimation: factscarousel_itemVisibleOutCallbackBeforeAnimation//,
		   //onAfterAnimation:  factscarousel_itemVisibleOutCallbackAfterAnimation
		}
	});
});


/**
 * We use the initCallback callback
 * to assign functionality to the controls
 */
function factscarousel_initCallback(carousel) {
    jQuery('#factscarousel .jcarousel-control a').bind('click', function() {
        carousel.scroll(jQuery.jcarousel.intval(jQuery(this).text()));
		return false;
    });

    jQuery('#factscarousel-next').bind('click', function() {
        carousel.next();
        return false;
    });

    jQuery('#factscarousel-prev').bind('click', function() {
        carousel.prev();
        return false;
    });
    
    //$('#flash-fact-1').flash(function() {this.Play();});
    
    if (!window.XMLHttpRequest) {DD_belatedPNG.fix('.square, .fact-graph')};
    
};


/**
 * This is the callback function which receives notification
 * when an item becomes the first one in the visible range.
 * Triggered before animation.
 */
function factscarousel_itemVisibleInCallbackBeforeAnimation(carousel, item, idx, state) {
    $('#nav-fact-'+idx).addClass('current');
    if (state == 'init'){return;} // Don't execute this next line on first load - otherwise it gives errors in IE
    $('#flash-fact-'+idx).flash( function() {this.GotoFrame(0);});
};

function factscarousel_itemVisibleInCallbackAfterAnimation(carousel, item, idx, state) {
	
    if (state == 'init'){
    	// Do nothing - setting it to autoplay further up in this file.
   		//setTimeout("tryToPlayFirst()",2000); 
    } else {
    	$('#flash-fact-'+idx).flash( function() {this.Play();});
    }
    
};


function tryToPlayFirst() { // No longer needed - setting it to autoplay further up in this file.
	$('#flash-fact-1').flash( function() {this.Play();});
};

/**
 * This is the callback function which receives notification
 * when an item is no longer the first one in the visible range.
 * Triggered before animation.
 */
function factscarousel_itemVisibleOutCallbackBeforeAnimation(carousel, item, idx, state) {
    $('#nav-fact-'+idx).removeClass('current');
};










