jQuery(function(){
    jQuery('#photos li').each(function(idx) {
        jQuery(this).data('index', (++idx));
    });

    jQuery('#photos').jcarousel({
        scroll: 7,
        initCallback: initCallbackFunction
    })
    
    function initCallbackFunction(carousel) {
        jQuery('#big').bind('image-loaded',function() {
            var idx =  jQuery('#photos li.active').data('index') - 2;
            
            carousel.scroll(idx);
            return false;
        });       
    };

    // load and fade-in thumbnails
    jQuery('#photos li img').css('opacity', 0).each(function() {    
        if (this.complete || this.readyState == 'complete') { jQuery(this).animate({'opacity': 1}, 500) } 
        else { jQuery(this).load(function() { jQuery(this).animate({'opacity': 1}, 500) }); }
    });

    
    jQuery('#photos').galleria({
        // #img is the empty div which holds full size images
        insert: '#big',
        clickNext : true, // helper for making the image clickable
        
        // enable history plugin
        history: false,
        
        // function fired when the image is displayed
        onImage: function(image, caption, thumb) {    
			 // fade in the image 
            image.hide().fadeIn(500);
            
            // animate active thumbnail's opacity to 1, other list elements to 0.6
            thumb.parent().fadeTo(200, 1).siblings().fadeTo(200, 0.6);
            
            jQuery('#big').trigger('image-loaded');                
                
        },
        
        // function similar to onImage, but fired when thumbnail is displayed
        onThumb: function(thumb) {
            var $li = thumb.parent(),
                opacity = $li.is('.active') ? 1 : 0.6;
            
            // hover effects for list elements
            $li.hover(
                function() { $li.fadeTo(200, 1); },
                function() { $li.not('.active').fadeTo(200, opacity); }
            )
        }        
    }).find('li:first-child').addClass('active'); // display first image when Galleria is loaded
    
    
});
