/**
 * We use the initCallback callback
 * to assign functionality to the controls
 */
var playButton = 'PLAY';
var pauseButton = 'PAUSA';
function mycarousel_itemVisibleInCallback(carousel,item,index,state) {
	var indice=index;
	if(index>carousel.size())
	{
		indice=index%carousel.size();
		if(indice==0) indice=carousel.size();
	}
	jQuery('#button-'+indice).attr('class','active');
	
	if(index!=1)
	{
		var imgPath = $(item).find('img').attr('image-path');
		var images=$(item).find('img');
		
		$(images[0]).attr('src', imgPath);
	}
	
	if(index==1)
	{
		$(item).find('.editoriale').fadeIn(900);
	}
	else
	{
		/*while($(images[0]).width()!=935)
		{
			window.setTimeout(fake(),500);
			//wait(60);
		}*/
		$(item).find('.editoriale').fadeIn(900);
	}
};

function mycarousel_itemVisibleOutCallback(carousel,item,index,state) {
	var indice=index;
	if(index>carousel.size())
	{
		indice=index%carousel.size();
		if(indice==0) indice=carousel.size();
	}
	//alert(indice);
	jQuery('#button-'+indice).attr('class','');
};
 
function mycarousel_initCallback(carousel) {
	jQuery('.jcarousel-control a.index').bind('click', function() {
		carousel.scroll(jQuery.jcarousel.intval(jQuery(this).text()));
		return false;
	});

	jQuery('.jcarousel-scroll select').bind('change', function() {
		carousel.options.scroll = jQuery.jcarousel.intval(this.options[this.selectedIndex].value);
		return false;
	});

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

	jQuery('#mycarousel-prev').bind('click', function() {
		carousel.prev();
		return false;
	});*/
	
	jQuery('#controlli').bind('click', function() {
		//Determino lo stato del controllo
		var stato=jQuery('#controlli').attr('class');
		if(stato=="play")
		{
			jQuery('#controlli').attr('class','pause');
			jQuery('#controlli').html(pauseButton);
			carousel.next();
			carousel.startAuto();
			return false;
		}
		else
		{
			jQuery('#controlli').attr('class','play');
			jQuery('#controlli').html(playButton);
			carousel.stopAuto();
			return false;
		}
	});
	
	
	/* Pause autoscrolling if the user moves with the cursor over the clip.*/
    /*carousel.clip.hover(function() {
        jQuery('.controlli').fadeIn();
    }, function() {
        jQuery('.controlli').fadeOut();
    });*/
};

// Ride the carousel...
jQuery(document).ready(function() {
	jQuery("#mycarousel").jcarousel({
		
		auto: 8,
        wrap: 'circular',
        scroll: 1,
        //easing: 'easeOutElastic',
        
		initCallback: mycarousel_initCallback,
		itemVisibleInCallback: mycarousel_itemVisibleInCallback,
		itemVisibleOutCallback: mycarousel_itemVisibleOutCallback,
		// This tells jCarousel NOT to autobuild prev/next buttons
		buttonNextHTML: null,
		buttonPrevHTML: null
	});
	
	
	jQuery('#visual').bind('mouseenter', function() {
		//Determino lo stato del controllo
		//alert("leave");
		jQuery('.controlli').fadeIn(1000,function(){$(this).css('filter','');});
	});
	
	jQuery('#visual').bind('mouseleave', function() {
		//Determino lo stato del controllo
		//alert("leave");
		jQuery('.controlli').fadeOut();
	});

});
