function carousel_init(id,visible,speed,menuId)
	{
		 // id:string = carousel id (without #)
		 // visible:int = max n of visible items 
		 // speed:int = animation speed

		if ( $("#"+id+" .carousel_item").length <= visible && $("#"+id).hasClass('alwaysShowControls') == false )
		{
			// if there are less items than max visible, do nothing.

		}
		else
		{
			// there are more items than max visible: show controls and add carousel functionality.
			
			// save parameters in the markup
			$("#"+id).attr("max",visible.toString());
			$("#"+id).attr("speed",speed.toString());
			$("#"+id).attr("menuId",menuId.toString());
			
			// add html for the controls
			var menuIdHtml = "disabled"; if (menuId) { menuIdHtml = "";};
			var controlsHtml = '<a href="javascript:;" class="carousel_prev '+menuIdHtml+'"  carouselID="'+id.toString()+'" ><span></span></a><a href="javascript:;" class="carousel_next" carouselID="'+id.toString()+'"><span></span></a>';
			$("#"+id+" .carousel_wrapper").after(controlsHtml);
			if ( $("#"+id+" .carousel_item").length <= visible) { $("#"+id+" .carousel_next").addClass("disabled"); };
			
			
			// NEXT handler
			$("#"+id+" .carousel_next").click(function(event)
			{
				event.preventDefault();
				carousel_scroll($(this).attr("carouselID"),"next");  // scorre il carosello avanti
			});
			
			// PREV handler
			$("#"+id+" .carousel_prev").click(function(event)
			{
				event.preventDefault();
				carousel_scroll($(this).attr("carouselID"),"prev");  // scorre il carosello indietro
			});
		
		} 	// end else

			
		// select the paired carousel
		if ( $("#"+menuId).length > 0)
		{
			$("#"+menuId+" .carousel_item").eq(0).addClass("active");
		}			
		
	} // end carousel_init()
	
	function carousel_jumpTo(carouselId,itemId)
	{
		// get parameters
		var item_width = $("#"+carouselId+" .carousel_item:first").width();
		var nth = $("#"+carouselId+" .carousel_item:visible").length; 
		var visible =  $("#"+carouselId).attr("max"); 				// set in carousel_init(), integer
		var menuId =  $("#"+carouselId).attr("menuId"); 	// set in carousel_init(), string
		var maxOffset =  item_width*(nth-visible);
		
		var i = 0;		
		$("#"+carouselId+" .carousel_item").each(function()
		{	
			if ($(this).attr("id") == itemId)
			{
				
				$("#"+carouselId+" .carousel_items").css("marginLeft",-item_width*i);	

				if ( $("#"+menuId).length == 1)
				{
					$("#"+menuId+" .carousel_item").removeClass("active");
					$("#"+menuId+" .carousel_item").eq(i).addClass("active");
				}
				
				// set the buttons enlabed/disabled if reached on of the edges
				if ( i == nth-1 )
				{
					$("#"+carouselId+" .carousel_next").addClass("disabled");
				}
				else
				{
					$("#"+carouselId+" .carousel_next").removeClass("disabled");
				}
				
				if ( i == 0 )
				{
					$("#"+carouselId+" .carousel_prev").addClass("disabled");
				}
				else
				{
					$("#"+carouselId+" .carousel_prev").removeClass("disabled");
				}		
				return false;
			}
			i++;			
		})
	}

	function carousel_scroll(id,direction)
	{
	
		// move carousel 
		// id:string:string = carousel id (without #)
		// direction:string = "next", "prev" [...]
	
		if ( $("#"+id+" .carousel_items").is(":animated") ) {return false; } // if is already animating, quit 

		// get parameters
		var item_width = $("#"+id+" .carousel_item:first").width();
		var nth = $("#"+id+" .carousel_item:visible").length; 
		var visible =  $("#"+id).attr("max"); 				// set in carousel_init(), integer
		var speed =  $("#"+id).attr("speed"); 				// set in carousel_init(), in milliseconds
		var menuId =  $("#"+id).attr("menuId");	// set in carousel_init(), string
		var maxOffset =  item_width*(nth-visible);
		var currentOffset = 0;
		var limitOffset =  $("#"+id+" .carousel_items").width(); // after the container width (should be 9999px) the contents switch to a new line
		var posActive = 0; // position of the active element of the paired carousel
		var offsetMenu = 0;	// offset of the paired carousel
		
		
		// which direction?
		switch (direction)
		{
			case "prev":
				currentOffset = $("#"+id+" .carousel_items").css("marginLeft").replace("px","");			
				if (currentOffset < 0)
				{
					if (currentOffset == -item_width) 
					{
						$("#"+id+" .carousel_prev").addClass("disabled");
					}
					else
					{
						$("#"+id+" .carousel_prev").removeClass("disabled");
					}					

					if (currentOffset != -maxOffset+item_width) 
					{
						$("#"+id+" .carousel_next").removeClass("disabled");					
					}					

					// select the paired carousel
					if ( $("#"+menuId).length == 1)
					{
						var selectedNo = (Math.abs(parseFloat($("#"+id+" .carousel_items").css("marginLeft").replace("px","")))/item_width)-1;
						if ($("#"+menuId+" .carousel_item").eq(selectedNo).is(":visible"))
						{;
							$("#"+menuId+" .carousel_item").removeClass("active");
							$("#"+menuId+" .carousel_item").eq(selectedNo).addClass("active");
						}						
						posActive = $("#"+menuId+" .carousel_item.active").position();
						offsetMenu = $("#"+menuId+" .carousel_items").css("marginLeft").replace("px","");
						
						if ( posActive.left <= - parseFloat(offsetMenu) )
						{
							carousel_scroll(menuId,"prev");
						}

					}
					
					$("#"+id+" .carousel_items").animate({"marginLeft":"+="+item_width+"px"}, speed);		

								
				}
			break; // end case prev
			
			case "next":
			default:
				currentOffset = $("#"+id+" .carousel_items").css("marginLeft").replace("px","");					
				if (currentOffset > -maxOffset)
				{
					if (currentOffset == -maxOffset+item_width) 
					{
						$("#"+id+" .carousel_next").addClass("disabled");				
					}
					else
					{
						$("#"+id+" .carousel_next").removeClass("disabled");					
					}			

					if (currentOffset != -item_width) 
					{
						$("#"+id+" .carousel_prev").removeClass("disabled");
					}
					
					// select the paired carousel
					if ( $("#"+menuId).length == 1)
					{
						var selectedNo = (Math.abs(parseFloat($("#"+id+" .carousel_items").css("marginLeft").replace("px","")))/item_width)+1;
						if ($("#"+menuId+" .carousel_item").eq(selectedNo).is(":visible"))
						{
							$("#"+menuId+" .carousel_item").removeClass("active");
							$("#"+menuId+" .carousel_item").eq(selectedNo).addClass("active");
							posActive = $("#"+menuId+" .carousel_item.active").position();
							offsetMenu = $("#"+menuId+" .carousel_items").css("marginLeft").replace("px","");
							
							if ( posActive.left >= ( $("#"+menuId+" .carousel_item:first").width()*$("#"+menuId).attr("max") - parseFloat(offsetMenu) ))
							{
								carousel_scroll(menuId,"next");
							}
							// alert ( "posActive: " + posActive.left +" > ( width: " + $("#"+menuId+" .carousel_item:first").width() + " * max: " + $("#"+menuId).attr("max") + " = "+ $("#"+menuId+" .carousel_item:first").width()*$("#"+menuId).attr("max") +" + offsetMenu: " + offsetMenu + " )");							
						}
					}					
					
					$("#"+id+" .carousel_items").animate({"marginLeft":"-="+item_width+"px"}, speed);						
				}
			
			break; // end case next, default
		} // end switch
				
		
			
				
	}	// end carousel_scroll()
