/*
 * jQuery Livion Slideshow
 * http://www.livion.fi
 */
(function($) {
	$.fn.livionSlideshow = function(arg) {
		var slideshow = $(this);
		
		if ( arg == 'next' ) {
			var imgs = slideshow.children();
			var speed = slideshow.data('ssSpeed');
			var horTrans = slideshow.data('ssHorTrans');
			var curIndex = slideshow.data('ssIndex');
			var nextIndex = (curIndex + 1) % imgs.length;
			
			var curImage = $(imgs[curIndex]);
			curImage.animate({
				left: -horTrans,
				opacity: 0.0
			}, speed);
			
			var nextImage = $(imgs[nextIndex]);
			nextImage.css('left', horTrans * 2);
			nextImage.animate({
				left: 0,
				opacity: 1.0
			}, speed);
			
			slideshow.data('ssIndex', nextIndex);
		} else {
			var config = {
				interval: 5000,
				speed: 1000,
				horizontalTransition: 0,
				extraImages: []
			};
			if ( arg ) {
				$.extend(config, arg);
			}
			
			if ( config['extraImages'].length + slideshow.children().length > 1 ) {
				for (var i in config['extraImages']) {
					var img = $('<img src="' + config['extraImages'][i] + '" alt="" />');
					img.css('opacity', 0.0);
					slideshow.append(img);
				}
				slideshow.data('ssIndex', -1);
				slideshow.data('ssSpeed', config['speed']);
				slideshow.data('ssHorTrans', config['horizontalTransition']);
				slideshow.css('position', 'relative');
				slideshow.css('overflow', 'hidden');
				slideshow.children().css('position', 'absolute');
				slideshow.children().css('opacity', 0);
				
				slideshow.livionSlideshow('next');				
				setInterval( function() { slideshow.livionSlideshow('next'); }, config['interval']);
			}
		}
	};
})(jQuery);

