$(document).ready(function() {	
	cron_pictureslider_start();
});

function cron_pictureslider_next() {
	var current = ($('#cron-pictureslider .cron-pictureslider-canvas a.show') ?  $('#cron-pictureslider .cron-pictureslider-canvas a.show') : $('#cron-pictureslider .cron-pictureslider-canvas a:first'));
	var next = ((current.next().length) ? ((current.next().hasClass('cron-pictureslider-caption')) ? $('#cron-pictureslider .cron-pictureslider-canvas a:first') : current.next()) : $('#cron-pictureslider .cron-pictureslider-canvas a:first'));	

	//alert(current.find('img').attr('title') + " und " + next.find('img').attr('title'));
	cron_pictureslider_show(current, next);
}

function cron_pictureslider_prev() {
	var canvas = $('#cron-pictureslider .cron-pictureslider-canvas');
	var current = (canvas.find('a.show') ? canvas.find('a.show') : canvas.find('a:first'));
	var prev = ((current.prev().length) ?  current.prev() : canvas.find('a:last'));	
	
	//alert(current.find('img').attr('title') + " und " + prev.find('img').attr('title'));
	cron_pictureslider_show(current, prev);
}

function cron_pictureslider_start() {
	
	// Set the caption background to invisible
	$('#cron-pictureslider .cron-pictureslider-caption').css({opacity: 0.0});
	
	// Set all pictures to invisible but the first one
	$('#cron-pictureslider .cron-pictureslider-canvas a').css({opacity: 0.0});
	$('#cron-pictureslider .cron-pictureslider-canvas a:first').css({opacity: 1.0});

	if (enableCaption) {
		// Start fadein's of caption box and text
		$('#cron-pictureslider .cron-pictureslider-caption-box').animate({opacity: captionOpacity}, 400);
		$('#cron-pictureslider .cron-pictureslider-caption-text').animate({opacity: 1.0}, 400);
		
		// Set title of image
		$('#cron-pictureslider .cron-pictureslider-caption-text .cron-pictureslider-content').html($('#cron-pictureslider .cron-pictureslider-canvas a.show').find('img').attr('title'));
		
		// caching the height of the caption-div in order to be able to resize it after being shinked down to 1px.
		captionHeight = $('#cron-pictureslider .cron-pictureslider-canvas .cron-pictureslider-caption').css('height');
	}
	
	if (enableDesc) {
		// Set description of image
		$('#cron-pictureslider .cron-pictureslider-desc .cron-pictureslider-content').html($('#cron-pictureslider .cron-pictureslider-canvas a.show').find('img').attr('alt'));
	}
	
	// if interval for slideshow is set -> start it.
	if (slideshowInterval) {
		setInterval('cron_pictureslider_next()', slideshowInterval);
	}
}

function cron_pictureslider_show(current, next) {
	
	// Show the next image
	next.css({opacity: 0.0})
	.addClass('show')              
	.animate({opacity: 1.0}, pictureFadeinDuration); 

	// Hides the current image
	current.animate({opacity: 0.0}, pictureFadeoutDuration)  	// Sicht runterfahren auf 0 innerhalb von 1000ms
	.removeClass('show');					// Nicht Zeigen.
	
	if (enableCaption) {
		var caption = next.find('img').attr('title');	
		var captionText = $('#cron-pictureslider .cron-pictureslider-caption-text');
		var captionBox = $('#cron-pictureslider .cron-pictureslider-caption-box');	
		
		// Shrinks down the caption
		captionText
		.animate({opacity: 0.0}, {queue: false, duration: 0})
		.animate({height: '1px'}, captionShrinkdownDuration, '', function callback() { captionText.find('div').html(caption); });	
		captionBox
		.animate({opacity: 0.0}, {queue: false, duration: 0})  
		.animate({height: '1px'}, {duration:captionShrinkdownDuration});	
		
		// Builds up the caption by setting its opacity to 70% and its height to its default
		// Crashs in IE if captionHeight is set to auto.
		// alert(captionHeight  + ", " + captionBuildupDuration);
		captionText
		.animate({opacity: 1.0}, { queue:false, duration:0 })
		.animate({height: captionHeight}, captionBuildupDuration);  
		captionBox
		.animate({opacity: captionOpacity}, { queue:false, duration:0 })
		.animate({height: captionHeight}, captionBuildupDuration); 
	}
	
	if (enableDesc) {
		var desc = next.find('img').attr('alt');
		var descText = $('#cron-pictureslider .cron-pictureslider-desc');
	
		// Sets the description
		descText.animate({opacity: 0.0}, descFadeoutDuration, '', function callback() { descText.find('div').html(desc); });
		descText.animate({opacity: 1.0}, descFadeinDuration);
	}
}

function cron_pictureslider_hover(element) {	$(element).addClass('hover');  }
function cron_pictureslider_unhover(element) {	$(element).removeClass('hover');  }
