function homeCarousel_initCallback(carousel) {
    $('.jcarousel-control a').bind('click', function() {
        carousel.scroll( $.jcarousel.intval( $(this).attr("custIndex") ) );
        return false;
    });
};

function homeCarousel_itemVisibleInCallback( carousel, li, index, state ){
	$('.jcarousel-control a:eq('+(index-1)+')').addClass("selected")
}

function homeCarousel_itemVisibleOutCallback( carousel, li, index, state ){
	$('.jcarousel-control a:eq('+(index-1)+')').removeClass("selected")
}

$(document).ready(function() {

	var count = $('#homeCarousel ul li').length;

	//add required navigators
	for ( var i=0; i<count; i++ ){
		$("#homeCarousel .jcarousel-control").append('<a href="#" custIndex="'+(i+1)+'">'+(i+1)+'</a>');	
	}
	
	$('#homeCarousel').jcarousel({
		scroll: 1,
		auto: 4,
		visible: 1,
		animation: 700,
		wrap: 'both',
		initCallback: homeCarousel_initCallback,
		itemVisibleInCallback: {onBeforeAnimation: homeCarousel_itemVisibleInCallback},
		itemVisibleOutCallback: {onBeforeAnimation: homeCarousel_itemVisibleOutCallback}
	});
	
	//locate right button depending on items length
	var controlWidth = $("#homeCarousel .jcarousel-control").width();
	var controlLeft = $("#homeCarousel .jcarousel-control").css("left");
	$("#homeCarousel .jcarousel-next-horizontal").css("left", controlWidth+parseInt(controlLeft) );
	
	//set read more button click handler
	$(".oaugCarousel .readButton").click(function(){
		var carousel = $(this).parents(".jcarousel-container:first");
		var index = carousel.find(".jcarousel-control > a.selected").attr("custindex");
		var linkUrl = carousel.find(".jcarousel-clip .jcarousel-item[jcarouselindex="+index+"] > a").attr("href");
		window.location.href = linkUrl;
	});
	
});

