$(document).ready( function() {
	// number each image (scroller uses this as a reference)
    $('.eventGalleryDiv li').each(function(idx) {
        $(this).data('index', (++idx));
    });
							
	$('.eventGalleryDiv').addClass('eventGallery'); // adds new class name to
													// maintain degradability
		$('ul.eventGallery').galleria( {
			history :true, 			// activates the history object for bookmarking, back-button etc.
			clickNext :true, 		// helper for making the image clickable
			insert :'#main_image',	// the containing selector for our main image
		onImage : function(image, caption, thumb) {

			// fade in the image & caption
			image.css('display', 'none').fadeIn(250);
			caption.css('display', 'none').fadeIn(250);

			// fetch the thumbnail container
			var _li = thumb.parents('li');

			// fade out inactive thumbnail
			_li.siblings().children('img.selected').fadeTo(250, 0.5);

			// fade in active thumbnail
			thumb.fadeTo('fast', 1).addClass('selected');
			
			$('#main_image').trigger('img_change');

			// add a title for the clickable image
			// image.attr('title','Next image >>');
		},
		onThumb : function(thumb) { // thumbnail effects goes here
				// fetch the thumbnail container
				var _li = thumb.parents('li');

				// if thumbnail is active, fade all the way.
				var _fadeTo = _li.is('.active') ? '1' : '0.5';

				// fade in the thumbnail when finnished loading
				thumb.css( {
					display :'none',
					opacity :_fadeTo
				}).fadeIn(250);

				// hover effects
				thumb.hover( 
					function() { thumb.fadeTo('fast', 1); }, 
					function() { _li.not('.active').children('img').fadeTo('fast', 0.5); } // don't fade out if the parent is active
				)
			}
		});
		
		$('.eventGalleryDiv').jcarousel({
        	scroll: 3,
	        initCallback: myJcarouselInitCallback
		});
	});

	function myJcarouselInitCallback(carousel) {
		jQuery('#main_image').bind('img_change',function() {
			var idx =  $('.eventGalleryDiv li.active').data('index') - 4;
			//alert(idx);
			carousel.scroll(idx);
			return false;
		});
	};	

