/* Slide show [based on http://tortoisehg.bitbucket.org/] */

function slideshow(parent, delay, MAX_W, MAX_H) {

	// init
	$('.slide_wrapper > a', parent).hide();
	$('.slide_wrapper', parent).css('display', 'block');

	// resize all images
	$('.slide_wrapper > a > img').each(function(){
		var img = $(this);
		if (img.width() > MAX_W) {
			img.width(MAX_W);
		}
		if (img.height() > MAX_H) {
			img.width('');
			img.height(MAX_H);
		}
	});

	// define switcher
	var frame = $('.slide_show', parent);
	var first = $('.slide_wrapper > a:first', parent).first();
	var FRAME_W = frame.width(), FRAME_H = frame.height();
	var switching = function(){
		var cur = $('.slide_wrapper > a:visible', parent);
		var next = cur.next('a');
		if (next.size() == 0) {
			next = first;
		}
		cur.delay(delay/2).fadeOut(1000);
		next.delay(delay/2).fadeIn(1000);
		if (frame.height() < next.height()) {
			frame.css({height: next.height()});
		}
	};
	
	if (FRAME_H < $('.slide_cover a', parent).height()) {
		frame.css({height: $('.slide_cover a', parent).height()});
	}

	// hide static image
	setTimeout(
		function() {
			$('.slide_cover a', parent).fadeOut(
				400, 
				function() {
					$('.slide_cover', parent).hide();
				}
			);
		},
		delay
	);

	// run switcher
	setTimeout(
		function() {
			switching();
			setTimeout(arguments.callee, delay);
		},
		delay/2
	);
}
