jQuery.fn.center = function () {
	this.css("position","absolute");
	this.css("top", ( $(window).height() - this.height() ) / 2+$(window).scrollTop() + "px");
	this.css("left", ( $(window).width() - this.width() ) / 2+$(window).scrollLeft() + "px");
	return this;
}

var slideshow2 = function()
{
	jQuery('#carousel').jcarousel(
	{
		scroll: 1,
		initCallback: initSlideshow2,
		itemVisibleInCallback: {onAfterAnimation: updateSlideshowControls2},
		buttonNextHTML: null,
		buttonPrevHTML: null,
		auto: 4
	});
};

var initSlideshow2 = function(obj)
{
	var nb_items = $("#carousel>li").size();
	var html = '<div class="jcarousel-control"><ul>';
	for(i=1; i<=nb_items;i++) { html += '<li><a href="#" rel="' +i+ '">' +i+ '</a></li>'; }
	html += '</ul></div>';
	$(html).appendTo("#slideshow");
	$(html).find('li:first').addClass('selected');

	// "more infos" button
	$('.jcarousel-control a').bind('click', function()
	{
		var index = $(this).attr("rel");
		obj.scroll($.jcarousel.intval(index));
		updateSlideshowControls(obj, '', index, '');
        $(this).blur();
		return false;
    });
	
	// popup images
	// rel=landscape or portrait
	
	$("#carousel img").click(function(e)
	{
		var html	= "<img src='"+$(this).parent().attr("href")+"' alt='"+$(this).attr("alt")+"' />";
		var rel		= $(this).attr("rel");
		var width	= (rel=="portrait") ? 480 : 640;
		var height	= (rel=="portrait") ? 640 : 480; 
		$("#popupimg").width(width);
		$("#popupimg").height(height);
		$("#popupimg").html(html).center().fadeIn("slow");	
		$("#popupbg").css({"opacity" : "0.6"}).fadeIn("slow");
		var carousel = jQuery('#carousel').data('jcarousel');
		carousel.stopAuto();	
		return false;
	});
	$("#popupbg").click(function()
	{
		$("#popupbg").fadeOut("fast");
		$("#popupimg").fadeOut("fast");
		var carousel = jQuery('#carousel').data('jcarousel');
		carousel.startAuto();
	});
    $("#popupimg").click(function()
	{
		$("#popupbg").fadeOut("fast"); 
		$("#popupimg").fadeOut("fast");
		var carousel = jQuery('#carousel').data('jcarousel');
		carousel.startAuto();
	});

};

var updateSlideshowControls2 = function(obj, li, index, state)
{
	var items = $('.jcarousel-control li');
	items.removeClass('selected');
	//console.log(index);
	items.filter(':nth-child('+index+')').addClass('selected');
	jQuery("#slideshowCurrentItem").html(index);
};

