var first_image = true;

var carousel_1;
function carousel_1_initCallback(carousel)
{
	
	carousel.selected = 1;
	
    carousel.buttonNext.bind('click', function()
    {
        carousel.startAuto();
        return false;
    });

    carousel.buttonPrev.bind('click', function()
    {
        carousel.startAuto();
        return false;
    });
   
    // Pause autoscrolling if the user moves with the cursor over the clip.
    carousel.clip.hover(function() {
        carousel.stopAuto();
    }, function() {
        carousel.startAuto();
    });

    
    
	// Autoscrolling-Zeit nach dem ersten Autoscrollen ändern
	setTimeout(function() 
			{
					carousel.next();
					carousel.stopAuto();
					carousel.startAuto(8);
			}, 6000);
	
    
    
};

function carousel_1_beforeAnimation(carousel,element,i,status)
{
	
	$('.is_visible').each(function()
			{
				hideTextEl(this);
			});
	
    var idx = carousel.index(i, carousel.options.size);
	carousel.add(i, carousel.get(idx).html());
	
	
	addMouseHandlers(carousel.get(i));
	//$("a", carousel.get(i)).lightbox();
	

}
        	
function carousel_1_afterAnimation(carousel,element,index,status)
{
	var idx = carousel.index(index, carousel.options.size);
	carousel.selected = idx;
	
}

function carousel_1_itemVisibleOutCallback(carousel, item, i, state, evt)
{

   	if (i>carousel.options.size || i<0)
   	{
    	carousel.remove(i);

    	// Safari-Fix: Breite erhöhen, um unnötigen Wrap zu verhindern. Nicht schön, funktioniert aber.
    	$(".tx-bildergalerie-pi2-list", carousel.element).animate({
	    		width: "+=1px"
	    	}, 1);
   	}
  	carousel.startAuto();

};

function addMouseHandlers(el)
{

	if (el)
	{
		$("a", el).lightbox();
		
		var textel = $(".galerie-text", el); 
		if (textel)
		{
			var imgel = $(".galerie-img img", el); 
			var infoel = $('.galerie-info', el);
						
			imgel.click(function()
					{
						if (!textel.hasClass("is_visible"))
						{
							showTextEl(textel, null);
						}
					});
			
			imgel.mouseover(function()
					{
						//infoel.addClass('galerie-info-hover');
					});

			
			infoel.click(function()
					{
						showTextEl(textel, function()
								{
									//infoel.removeClass('galerie-info-hover');
								});
					});
			
			infoel.mouseenter(function()
					{
						infoel.addClass('galerie-info-hover');
					});
			infoel.mouseleave(function()
					{
						infoel.removeClass('galerie-info-hover');
					});
			
			textel.click(function()
					{
						hideTextEl(textel);
					});
		
		}

	}

}

function hideTextEl(el)
{

	el = $(el);
	el.removeClass("is_visible");
	el.css("right", "-236px");
	el.animate({
	    right: '-457px'
	  	}, 1000);	

}

function showTextEl(el, callback)
{

	el = $(el);
	el.addClass("is_visible");
	el.css("right", "-457px");
	el.animate({
	    right: '-236px'
	}, 700, callback);

}


$(document).ready(function()
{
	var galerie = $(".tx-bildergalerie-pi1-list");
	if (galerie)
	{
		//alert("Galerie gefunden");
		$(galerie).jcarousel({
				scroll:	1,
				auto:	6,
				wrap:	'circular',
		        initCallback: carousel_1_initCallback,
		        itemVisibleInCallback: {
		  			onBeforeAnimation: carousel_1_beforeAnimation,
		  			onAfterAnimation: carousel_1_afterAnimation
				},
				itemVisibleOutCallback:carousel_1_itemVisibleOutCallback
		});
		//alert("Carousel erstellt");		
	}

	var galerie2 = $('.tx-bildergalerie-pi2-list');
	if (galerie2)
	{
		$(galerie2).jcarousel({
				scroll: 3,
				wrap:	'circular',
		        initCallback: carousel_1_initCallback,
		        itemVisibleInCallback: {
		  			onBeforeAnimation: carousel_1_beforeAnimation,
		  			onAfterAnimation: carousel_1_afterAnimation
				},
				itemVisibleOutCallback:carousel_1_itemVisibleOutCallback
			}
		);		
	}
	
	


});

