jQuery.fn.center = function () {
	this.css("position","absolute");
	this.css("top",  ( jQuery(window).height() - this.height() ) / 2+jQuery(window).scrollTop() + "px");
	this.css("left", ( jQuery(window).width() - this.width() ) / 2+jQuery(window).scrollLeft() + "px");
	return this;
}

var slideshow2 = function()
{
	jQuery('#carousel').jcarousel(
	{
		scroll: 1,
		initCallback: initSlideshow2,
		itemVisibleInCallback: {onAfterAnimation: updateSlideshowControls2},
		buttonNextHTML: null,
		buttonPrevHTML: null,
		auto: 4
	});

	if (scrollTo != null)
	{
		var carousel = jQuery('#carousel').data('jcarousel');
		carousel.scroll(scrollTo-3);
	}
};

var initSlideshow2 = function(obj)
{
	var nb_items = jQuery("#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');

	$('.jcarousel-control a').bind('click', function()
	{
		var index = jQuery(this).attr("rel");
		obj.scroll(jQuery.jcarousel.intval(index));
		updateSlideshowControls(obj, '', index, '');
        jQuery(this).blur();
		return false;
    });
	
	$("#carousel img").click(function(e)
	{
		var html	= "<img src='"+jQuery(this).parent().attr("href")+"' alt='"+jQuery(this).attr("alt")+"' />";
		var rel		= jQuery(this).attr("rel");
		$("#popupimg").width(640);
		$("#popupimg").height(480);
		$("#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)
{
	index = (index >3) ? index-3 : index;
	var items = jQuery('.jcarousel-control li');
	items.removeClass('selected');
	//console.log(index);
	items.filter(':nth-child('+index+')').addClass('selected');
	jQuery("#slideshowCurrentItem").html(index);
};

