function mycarousel_itemLoadCallback(carousel, state)
{
	// Pause autoscrolling if the user moves with the cursor over the clip.
    carousel.clip.hover(function() {
        carousel.stopAuto();
    }, function() {
        carousel.startAuto();
    });
	 
	 //Control
	 jQuery('#jcarousel-control a').bind('click', function() {
		 
		 value = jQuery(this).attr("id");
       carousel.scroll(jQuery.jcarousel.intval(value)); 
       return false;
    });
	 jQuery('#jcarousel-control a').bind('mouseover', function() {
		swapPointToSelected(jQuery(this));
    });
	 jQuery('#jcarousel-control a').bind('mouseout', function() {
		 swapPointToDeselected(jQuery(this));
    });
	
	//Selection balls
	selectPoint(jQuery('#' + carousel.index(carousel.first).toString()));
	
	var list = this[carousel.options.id + '_itemList'];
	//for (var i = carousel.first; i <= carousel.last; i++) {
	for (i = 1; i <= list.length; i++) {
	 
	if (carousel.has(i)) {
		continue;
	}
	if (i > list.length) {
		break;
	}		
		var item = list[carousel.index(i, list.length)-1];			
		carousel.add(i, mycarousel_getItemHTML(item,carousel.options.id));	
	}	 
	 setDefaultRolloverSwap();
};


function swapPointToBrown(object) {
	object.find('img').attr( 'src', 'images/pallino.png' );
}
function swapPointToBeige(object) {
	object.find('img').attr( 'src', 'images/pallino-sel.png' );
}

function swapPointToSelected(object) {	
	swapPointToBeige(object);
}
function swapPointToDeselected(object, force) {
	if(object.data('selected') && !force)
		return;	
	swapPointToBrown(object);
}


function deSelectPoints(exept) {
	var objects = [jQuery("#1"), jQuery("#2"), jQuery("#3")];
	var object;	
	//console.log(jQuery("#1"));
	for(var i = 0; i < objects.length; i++) {
		object = objects[i];
		if(object === exept){
			continue;}
		object.data('selected', false);
		swapPointToDeselected(object);
	}
}

function selectPoint(object) {
	deSelectPoints(object);
	object.data('selected', true);	
	swapPointToSelected(object);
}

function onAnimationStepCallback () {
	selectPoint();
}


function mycarousel_getItemHTML(item,idCarousel)
{
	var html = '';
		
	if(idCarousel == "manuale"){					
		html += '<img src="' + item.url + '.png" width="603" height="380" alt="' + item.title + '" />';
	} else {
		html += '<a href="' + item.linkTo + '"><img src="' + item.url + '.png" width="150" height="154" alt="' + item.title + '" /></a>';
	}
	return html;
};



//manuale
function buildCarouselManuale() {
	jQuery(document).ready(function() {
		 jQuery('#manuale').jcarousel({		
				visible: 1,
				//auto: 5,
				//wrap: 'circular',
				size: manuale_itemList.length,
				scroll: 1,
				id: 'manuale',
				itemLoadCallback: {onBeforeAnimation: mycarousel_itemLoadCallback}
		 });		 
	});
}

//gallery home page
function buildCarouselGalleryHP() {
	jQuery(document).ready(function() {
		 jQuery('#galleryHP').jcarousel({		
				visible: 1,
				auto: 5,
				wrap: 'circular',
				size: galleryHP_itemList.length,
				scroll: 1,
				id: 'galleryHP',
				itemLoadCallback: {onBeforeAnimation: mycarousel_itemLoadCallback}
		 });		 
	});
}


