
var current_index = 1;
var onMouseOutOpacity = 0.6;

function hideArrows() {$(".AAjcarousel-prev").addClass("jcarousel-prev-disabled-horizontal");
	$(".jcarousel-next").addClass("jcarousel-next-disabled-horizontal");}

function moveToSlide(carousel, index) {
	current_index = index;
    if (current_index > $('#slides li').size()) {
    	current_index = $('#slides li').size();
    }
    if (current_index < 1) {
    	current_index = 1;
    }
	
	hideArrows();
	$('.userpics li').removeClass("selected").css('opacity', onMouseOutOpacity);
    $('.userpics li:eq(' + (current_index - 1) + ')').addClass("selected").css('opacity', 1.0);
    
	carousel.scroll(current_index);
}

function init(carousel) {
	$('.userpics li').each(function(i) {
		$(this).click(function(e) {
			moveToSlide(carousel, i+1);
			return false;
		});
	});
	
	$('#slides li .userpic').each(function(e) {
		$(this).click(function(e) {
			var id = parseInt($(this).attr("id").replace("rel_user_", ""), 10);
			var index = parseInt($("#user_" + id).attr("index"), 10);
			moveToSlide(carousel, index);
		});
	});
	
	$('#next').click(function() {
		moveToSlide(carousel, current_index+1);
        return false;
    });
    
    $('#prev').click(function() {
		moveToSlide(carousel, current_index-1);
    	return false;
    });	
}

$(document).ready(function() {
//$(window).load(function() {

	$(".thumbs li").css('opacity', onMouseOutOpacity).hover(
		function () {
			if (!$(this).hasClass('selected')) {
				$(this).fadeTo('fast', 1.0);
			}
		},
		function () {
			if (!$(this).hasClass('selected')) {
				$(this).fadeTo('fast', onMouseOutOpacity);
			}
		}
	);
	$(".thumbs li.selected").css('opacity', 1.0);
	
	$("#slides").jcarousel({
		initCallback: init,
		scroll: 1,
		buttonNextHTML: null,
		buttonPrevHTML: null
	});
});

