$(document).ready(function() {
	bindCarousel($("ul.gallery"));
});

function bindCarousel(el) {
	el.jcarousel({
        scroll: 		1,
        initCallback: 	setCarouselControl,
        buttonNextHTML: null,
        buttonPrevHTML: null,
        scroll:			5
    });
	
	el.find('li:first img:first').click();
}

function setCarouselControl(carousel) {
    $('ul.gallery li').bind('click', function() {
    	el = $(this).find('img:first');
        carousel.scroll($.jcarousel.intval(el.attr('iterator')) - 1);
        popup = $('div#gallery');

        popup.find('img.full:first').attr({
        	'alt'	: el.attr('alt'),
        	'title'	: el.attr('title'),
        	'src'	: el.attr('src').replace('thumb_', '')
        });
        
        popup.find('div.note:first').html(el.attr('alt'));
        
        return false;
    });
};
