<!-- features slideshow js -->

var controlsLocked = false;
var currentItem = 1;

jQuery(document).ready(function(){

	if(jQuery(".editmode").text() != "editmode"){
		showComponentBackground();
		buildNumberNav();
		
		jQuery('#features_slideshow').jcarousel({
			initCallback: slideshow_initCallback,
			itemLoadCallback:{
				onBeforeAnimation: lockControls,
				onAfterAnimation: unlockControls
			},
			itemVisibleInCallback:{
				onBeforeAnimation: highlightNumber
			},
			scroll:1,
			visible:1,
			animation:700
		});
	}
	
	function showComponentBackground(){
		jQuery('#features_slideshow_container').addClass('background');
	}
		
	function buildNumberNav(){
		jQuery("#features_slideshow li").each(function(i){
			var classes = "slideshow_nr";
			if(i==0){
				classes = "slideshow_nr selected";			
			}
			jQuery('#number_nav').append('<a href="" class="'+ classes +'">'+ (i+1) +'</a>');	
		});
	}
	
	function slideshow_initCallback(carousel){		
		jQuery('#number_nav a').bind('click', function() {
			var newItem = jQuery.jcarousel.intval(jQuery(this).text());
			if(!controlsLocked && currentItem != newItem){							
				carousel.scroll(newItem);
				currentItem = newItem;
				jQuery(this).addClass('selected').siblings().removeClass('selected');
			}
			return false;
		});
	}	
	
	function lockControls(carousel, state){
		controlsLocked = true;
	}
		
	function unlockControls(carousel, state){
		controlsLocked = false;
	}
	
	function highlightNumber(carousel, item, idx, state){
		jQuery('#number_nav a:eq('+ (parseInt(idx)-1) +')').addClass('selected').siblings().removeClass('selected');
		currentItem = parseInt(idx);
	}	
});		