var scroll_position = 0;
var photosPerPage = 3;
var pages; 

function initjQueryGallery(photosCount) {
	jQuery(document).ready(function() {
		pages = Math.ceil(photosCount/photosPerPage)-1; 
		$("#carousel-content").show();
	    $("#mycarousel").jcarousel({
	        scroll: 1,
	        initCallback: mycarousel_initCallback	       
	    });
	    
	    $.fn.fancyzoom.defaultsOptions.imgDir='/js/jqueryfancyzoom/ressources/';
		$('#mycarousel ul li a').fancyzoom();
	});		
}    

function uncolorPageButtons() {
	var i=0;
	for (i=0; i<pages+1; i++) {
		$('#page-button-'+(i+1)).removeClass('selected');		
	}
}
	
function colorPageButton(number) {
	$('#page-button-'+(number+1)).addClass('selected');	
}

function mycarousel_initCallback(carousel) {
    jQuery('.jcarousel-control a').bind('click', function() {
        carousel.scroll(jQuery.jcarousel.intval((jQuery(this).text()-1)*photosPerPage)+1);
        uncolorPageButtons();
        scroll_position = jQuery.jcarousel.intval((jQuery(this).text()-1));
        colorPageButton(scroll_position);
        return false;
    });
    
    carousel.options.scroll = jQuery.jcarousel.intval(photosPerPage);
    

    jQuery('#mycarousel-next').bind('click', function() {
    	uncolorPageButtons();
    	if (scroll_position<pages) scroll_position++;
    	colorPageButton(scroll_position);
        carousel.next();
        
        return false;
    });

    jQuery('#mycarousel-prev').bind('click', function() {
    	uncolorPageButtons();
    	if (scroll_position>0) scroll_position--;
        colorPageButton(scroll_position);
        carousel.prev();  
              
        return false;
    });
};