function mycarousel_itemVisibleInCallback(postId, carousel, item, i, state, evt)
	{
		// The index() method calculates the index from a
		// given index who is out of the actual item range.
		var idx = carousel.index(i, mycarousel_itemList['post-'+postId].length);
		
		// crappy IE6 crappety crap
		//var isMSIE = /*@cc_on!@*/false;
		//if (isMSIE && idx == mycarousel_itemList_".$post->ID.".length) {
		//	idx = 1;
		//	i = 1;
		//}
		
		carousel.add(i, mycarousel_getItemHTML(mycarousel_itemList['post-'+postId][idx - 1]));
	};

	
	function mycarousel_itemVisibleOutCallback(postId, carousel, item, i, state, evt)
	{
		carousel.remove(i);
	};
	
	/**
	 * Item html creation helper.
	 */
	function mycarousel_getItemHTML(item)
	{
		return "<h2 class='cgj_title'>" + item.title + "</h2><div class='cgj_image'><a href='#' rel='nofollow'><img src='"+item.url+"' alt='"+item.title+"' /></a></div><p class='caption'>" + item.caption.replace(/{br}/g, '<br>') +"</p><p class='description'>" + item.description.replace(/{br}/g, '<br>') +"</p>";

	};
	
	
	
	/**
	 * We use the initCallback callback
	 * to assign functionality to the controls
	 */
	function mycarousel_initCallback(postId, carousel) {

		jQuery('.jcarousel-control_'+postId+' a').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_'+postId+'-next').bind('click', function() {
			carousel.next();
			return false;
		});
	
		jQuery('#mycarousel_'+postId+'-prev').bind('click', function() {
			carousel.prev();
			return false;
		});

		jQuery('#mycarousel_'+postId+'-forward').bind('click', function() {
			carousel.next();
			return false;
		});

	};


	/**
	 * This is the callback function which receives notification
	 * when an item becomes the first one in the visible range.
	 */

	function mycarousel_itemFirstInCallback(postId, carousel, item, idx, state) {
		
		nextItem = idx % mycarousel_itemList['post-'+postId].length;
		nextItem = (nextItem == 0) ? mycarousel_itemList['post-'+postId].length : nextItem;
		
		jQuery('.jcarousel-control_'+postId+' a').removeClass('active');
		jQuery('.item'+nextItem+'_'+postId).addClass('active');
	
	
		
		// add forward btns on each image in its own right

		jQuery('#mycarousel_'+postId+' a').bind('click', function() {
			carousel.next();

			return false;
		});
	};
	
	

	
	/**
	* Initialize
	*/

	function mycarousel_setup(postId, animationSpeed){
		jQuery(document).ready(function() {
			jQuery('#mycarousel_'+postId).jcarousel({
				wrap: 'circular',
				itemVisibleInCallback: {onBeforeAnimation: function(carousel, item, i, state, evt){ mycarousel_itemVisibleInCallback(postId, carousel, item, i, state, evt) } }
				,itemVisibleOutCallback: {onAfterAnimation: function(carousel, item, i, state, evt){ mycarousel_itemVisibleOutCallback(postId, carousel, item, i, state, evt) } }
				,scroll: 1
				,animation: animationSpeed
				,initCallback: function (carousel){ mycarousel_initCallback(postId, carousel); }
				,buttonNextHTML: null
				,buttonPrevHTML: null
				,itemFirstInCallback: function(carousel, item, idx, state){ mycarousel_itemFirstInCallback(postId, carousel, item, idx, state); }
			});
		});
	}

