jQuery.easing['InOutCubic'] = function (p, t, b, c, d) {
	if ((t/=d/2) < 1) return c/2*t*t*t + b;
	return c/2*((t-=2)*t*t + 2) + b;
};		

function extcarousel_initCallback(carousel){
	//set height after load?
	jQuery('#b2bcarousel').css({height:jQuery('#jcarousel a img').attr('height')});
	jQuery('#b2bcarousel').append('<div id="jcarousel_buttons"><ul><li id="w1"><a href="#"><img id="hpb1" width="26" height="25" alt="" src="images/1_white_on.png"/><' + '/a><' + '/li><li id="w2"><a href="#"><img id="hpb2" width="26" height="25" alt="" src="images/2_white_off.png"/><' + '/a><' + '/li><li id="w3"><a href="#"><img id="hpb3" width="26" height="25" alt=""src="images/3_white_off.png"/><' + '/a><' + '/li><' + '/ul><' + '/div>');
	
	jQuery('#jcarousel_buttons li#w1 a img').attr('alt',jQuery('.jcarousel-item-1').attr('data-spot'));
	jQuery('#jcarousel_buttons li#w1 a').attr('title',jQuery('.jcarousel-item-1').attr('data-spot'));
	jQuery('#jcarousel_buttons li#w1 a').bind('click', function() {
		carousel.startAuto(0);
		carousel.scroll(jQuery.jcarousel.intval(1));
		return false;
	});
	jQuery('#jcarousel_buttons li#w2 a img').attr('alt',jQuery('.jcarousel-item-2').attr('data-spot'));
	jQuery('#jcarousel_buttons li#w2 a').attr('title',jQuery('.jcarousel-item-2').attr('data-spot'));
	jQuery('#jcarousel_buttons li#w2 a').bind('click', function() {
		carousel.startAuto(0);
		carousel.scroll(jQuery.jcarousel.intval(2));
		return false;
	});
	jQuery('#jcarousel_buttons li#w3 a img').attr('alt',jQuery('.jcarousel-item-3').attr('data-spot'));
	jQuery('#jcarousel_buttons li#w3 a').attr('title',jQuery('.jcarousel-item-3').attr('data-spot'));
	jQuery('#jcarousel_buttons li#w3 a').bind('click', function() {
		carousel.startAuto(0);
		carousel.scroll(jQuery.jcarousel.intval(3));
		return false;
	});			
};

//Hide the buttons before animating heros
function extcarousel_itemVisibleInCallbackBeforeAnimation(carousel, item, idx, state) {
		jQuery('#jcarousel_buttons').fadeOut('fast', function(){
				jQuery('#jcarousel_buttons li#w1 a').hide();
				jQuery('#jcarousel_buttons li#w2 a').hide();
				jQuery('#jcarousel_buttons li#w3 a').hide();
				});
		
};

//Return the buttons after animating heros
function extcarousel_itemVisibleInCallbackAfterAnimation(carousel, item, idx, state) {			
		var spotcolor = new Array();
		if (spotcolor.length < 3) {
			spotcolor[1] = jQuery('.jcarousel-item-1').attr('data-color');
			spotcolor[2] = jQuery('.jcarousel-item-2').attr('data-color');
			spotcolor[3] = jQuery('.jcarousel-item-3').attr('data-color');
		}
		jQuery('#jcarousel_buttons li#w1 a img').attr("src",'images/1_'+spotcolor[idx]+'_off.png');
		jQuery('#jcarousel_buttons li#w2 a img').attr("src",'images/2_'+spotcolor[idx]+'_off.png');
		jQuery('#jcarousel_buttons li#w3 a img').attr("src",'images/3_'+spotcolor[idx]+'_off.png');
		//replace "off" button with "on" for current item
		jQuery('#jcarousel_buttons li#w'+idx+' a img').attr("src",'images/'+idx+'_'+spotcolor[idx]+'_on.png');
		
		jQuery('#jcarousel_buttons').show();
		jQuery('#jcarousel_buttons li#w1 a').fadeIn('fast', function(){
				jQuery('#jcarousel_buttons li#w2 a').fadeIn('fast', function(){
						jQuery('#jcarousel_buttons li#w3 a').fadeIn('fast');
				});
		});
};

jQuery(document).ready(function() {
	// init carousel					
	jQuery('#jcarousel').jcarousel({
				auto: 3,
				wrap: 'both',
				scroll: 1,
				easing: 'InOutCubic',
				animation: 1000,
				size: 3,
				start: 1,  
				buttonNextHTML: null,
				buttonPrevHTML: null,
				initCallback: extcarousel_initCallback,
				itemVisibleInCallback: {onBeforeAnimation: extcarousel_itemVisibleInCallbackBeforeAnimation,
										onAfterAnimation:  extcarousel_itemVisibleInCallbackAfterAnimation}
	});
	
	//Disable text & element selection
	//document.onselectstart = function() {return false;} // ie
	//document.onmousedown = function() {return false;} // mozilla - disabled: problematic		
});
