function mycarousel_itemVisibleInCallback(carousel, item, i, state, evt) {
	var idx = carousel.index(i, itemList.length);
	carousel.add(i, mycarousel_getItemHTML(itemList[idx - 1])); 
}

function mycarousel_itemVisibleOutCallback(carousel, item, i, state, evt) {
	carousel.remove(i);
}

function mycarousel_itemFirstInCallback(carousel, item, idx, state) {
	firstInIdx = parseInt(jQuery(item.innerHTML).attr("id"));
	firstInItem = item;
}

function mycarousel_itemLastInCallback(carousel, item, idx, state) {
	lastInIdx = parseInt(jQuery(item.innerHTML).attr("id"));
	lastInItem = item;
	
	item.firstChild.removeAttribute("onmouseover");
	item.firstChild.removeAttribute("onmouseout");
	
	
	manageTransiction(item);
}


/**
 * Item html creation helper.
 */
function mycarousel_getItemHTML(item) {
	return '<img id="' + item.itemid + '" src="' + item.thumb + '" width="100" height="52" border="0" alt="" title="" ' + 						'onclick="slideImage(this);" onmouseover="tooltipIn(this);" onmouseout="tooltipOut(this);" />';
};


function initializeControls(carousel){
	// global variables
	carouselLength = itemList.length;
	myCarousel = carousel;
	
	
	carousel.clip.mouseover(function() {
		carousel.stopAuto();
		jQuery(this).css({
			cursor: "pointer"
		});
	});
	carousel.clip.mouseout(function() {
		carousel.startAuto();
		jQuery(this).css({
			cursor: "normal"
		});
	});
	
}


function slideImage(image){
	var clickedIdx = parseInt(image.id);
	var slideForNext;
	var slideForPrev;
	
	if (scrollDirection == 0){
		slideForNext = (lastInIdx + carouselLength - clickedIdx) % carouselLength;
		slideForPrev = (clickedIdx + carouselLength - lastInIdx) % carouselLength;
	}
	else {
		slideForNext = (clickedIdx + carouselLength - firstInIdx) % carouselLength;
		slideForPrev = (firstInIdx + carouselLength - clickedIdx) % carouselLength;
	}
	
	//slidePrev(image, slideForPrev, itemToScroll);	
	slideNext(image, slideForNext, itemToScroll);
}

function manageTransiction(item){
	var imageId = parseInt(item.firstChild.id);
	var jMainFoto = jQuery("#mainFoto");
	
	jMainFoto.css({
		display: "none"
	});
	
	jMainFoto.attr("src", itemList[imageId].url);
	jMainFoto.parent().attr("href", itemList[imageId].link);
	jMainFoto.parent().attr("target", itemList[imageId].target);
	
	jQuery('#titolum_grande_visore').html(itemList[imageId].title);
	jQuery('#verbo_grande_visore').html(itemList[imageId].description);
	jQuery('#containerLink').attr("href", itemList[imageId].link);
	jQuery('#containerLink').attr("target", itemList[imageId].target);
	
	jQuery("#mainFoto").fadeIn(fadeInTime);
	
	jQuery(item.firstChild).wrap("<a href=\"" + itemList[imageId].link + "\" target=\"" + itemList[imageId].target + "\"></a>");
	
	Reflection.remove(item.firstChild.firstChild);
	Reflection.add(item.firstChild.firstChild, { height: 1/5, opacity: 2/3 });
}


function slideNext(image, slideFor, originalSlideWindow){
	myCarousel.options.scroll = slideFor;
	myCarousel.next();
	myCarousel.options.scroll = originalSlideWindow;
}

function slidePrev(image, slideFor, originalSlideWindow){				
	myCarousel.options.scroll = slideFor;
	myCarousel.prev();
	myCarousel.options.scroll = originalSlideWindow;
}


function getCarouselIdx(image){
	return parseInt(jQuery(image).parent().attr("jcarouselindex"));
}


function getImageRelativeIdxFromTom(image){
	var curImgCarouselIdx = parseInt(jQuery(image).parent().attr("jcarouselindex"));
	var topFirstItemCarouselIdx = parseInt(jQuery(firstInItem).attr("jcarouselindex"));
	return curImgCarouselIdx - topFirstItemCarouselIdx;
}


function tooltipIn(image){
	var top = -731;
	var liHeight = 62; 

	var relPos = getImageRelativeIdxFromTom(image);
	var newTop = top + (liHeight * relPos);
	
	jQuery(".tooltip_visore").css({
		top: newTop + 'px'
	});
	
	var imageId = parseInt(image.id);
	jQuery(".titolum_piccolo_visore").html(itemList[imageId].title);
	
	jQuery(".tooltip_visore").css({
		display: "block"
	});
}

function tooltipOut(image){
	jQuery(".tooltip_visore").css({
		display: "none"
	});
}



// function to call to start carousel
function initCarousel(){
	jQuery('#mycarousel').jcarousel({
		wrap: 'circular',
		vertical: scrollVertical,
		direction: scrollDirection, //--> if 0 invert standard scrolling
		scroll: itemToScroll, //-> numero di item da scrollare alla volta
		animation: scrollTimeAnimation, //-> number of milliseconds of animation
		auto: timeout, //-> numero di secondi per l'autoscroll (0 significa senza autoscroll)
		initCallback: initializeControls,
		itemFirstInCallback:  mycarousel_itemFirstInCallback,
		itemLastInCallback:  mycarousel_itemLastInCallback,
		itemVisibleInCallback: {onBeforeAnimation: mycarousel_itemVisibleInCallback},
		itemVisibleOutCallback: {onAfterAnimation: mycarousel_itemVisibleOutCallback},
		buttonNextHTML: null,
		buttonPrevHTML: null
	});
	
	// fix png in internet explorer <= 6
	jQuery('.tooltip_visore').pngFix();
	
}