function gc_gallery_carousel_start(config){
	var thumbLightbox = config[0];

	jQuery(document).ready(function(){
		// make thumbs and bind events
		gc_gallery_carousel_thumbs(config);

		// create carousel
		jQuery('.gc_gallery_carouselview .gc_gallery').css('visibility', 'visible');
		jQuery('.gc_gallery_carouselview .gc_gallery').jcarousel({
			scroll: 2,
			initCallback: carousel_initCallback
		});

		if(thumbLightbox == true){
			/* set as default in lightbox.js (this doesnt work in ie6)
			jQuery.Lightbox.construct({
				show_linkback:	false,
				show_extended_info: false,
				download_link: false,
				baseurl: '',
				text: {
					// For translating
					image:		'',
					of:			'/',
					close:		'X',
					closeInfo:	'',
					download:	'',
					help: {
						close:		'',
						interact:	''
					},
					about: {
						text: 	'',
						title:	'',
						link:	''
					}
				},
				files: {
					images: {
						prev: 'typo3/ext/gc_gallery/res/css/lightbox-prev.gif',
						next: 'typo3/ext/gc_gallery/res/css/lightbox-next.gif',
						blank: 'typo3/ext/gc_gallery/res/css/lightbox-blank.gif',
						loading: 'typo3/ext/gc_gallery/res/css/lightbox-loading.gif'
					}
				}
			});
			*/

			// create lightbox
			jQuery('.gc_gallery_carouselview .gc_gallery img').lightbox();
		}
	});
}

function gc_gallery_carousel_thumbs(config){
	var thumbLightbox = config[0];
	var thumbCapt = config[1];
	var thumbCaptPad = config[2];
	var thumbOpacity = config[3];

	var _cont = jQuery('.gc_gallery_carouselview .gc_gallery');

	jQuery.each(_cont.children('li'), function() {
		var _li = jQuery(this);
		//var _a = _li.children('a');
		var _img = _li.children('img');

		var _title = _img.attr('title');
		var _link = _img.attr('link');

		if(_title.length == 0){
			_img.attr('title', ' ');
		}

		// thumbnail caption
		if(thumbCapt == true){
			var h = parseInt(_li.height()) + parseInt(thumbCaptPad);
			if(!isNaN(h)){
				// set li height
				_li.css('height',h + 'px');
				_li.parent().parent().css('height',h + 'px');
			}

			if(_link.length > 0){
				var _h3 = '<h3 class="align-center"><a href="'+_link+'">'+_title+'</a></h3>';
			} else {
				var _h3 = '<h3 class="align-center">'+_title+'</h3>';
			}
			_li.append('<div class="carouselview_caption">'+_h3+'</div>');
			//_a.attr('title', '');
		}

		// hover
		_img.fadeTo(500, thumbOpacity);
		_li.hover(
			function() { _img.fadeTo('fast', 1); },
			function() { _img.fadeTo('fast', thumbOpacity); }
		)
	});
}

// callback when clicking carousel items
function carousel_initCallback(carousel) {
	// calculate middle position in jcarousel (alot of magic here)
	var contWidth = jQuery('.gc_gallery_carouselview .jcarousel-clip-horizontal').css("width");
	contWidth = contWidth.substr(0, contWidth.length - 2);
	var itemWidth = jQuery('.gc_gallery_carouselview .jcarousel-item').css("width");
	itemWidth = itemWidth.substr(0, itemWidth.length - 2);
	var mid = Math.round(contWidth / itemWidth / 2) - 1;

	// bind click event
	 jQuery('.gc_gallery_carouselview .jcarousel-item').click(function() {
	// jump if link is set
	var link = jQuery(this).children('img').attr('link');
	if(link.length > 0){
		window.location.href = link;
	// else scroll carousel
	} else {
		var num = parseInt((jQuery(this).attr('jcarouselindex'))) - mid;
	    	carousel.scroll(num, true);
	    }

	    return false;
	  });
}