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

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

// wait for loading all images
$.event.add(window, 'load', function(){

	// resize all images
	var MAX_W = 984, MAX_H = 325;
	$('.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');
	var first = $('.slide_wrapper > a:first').children().first();
	var FRAME_W = frame.width(), FRAME_H = frame.height();
	var switching = function(){
		var cur = $('.slide_wrapper > a > img:visible');
		var next = cur.parent().next('a').children().first();
		if (next.size() == 0) {
			next = first;
		}
		cur.delay(2000).fadeOut(1000);
		var pos = {left: (FRAME_W - next.width()) / 2, top: (FRAME_H - next.height()) / 2};
		next.css(pos);
		next.delay(2000).fadeIn(1000);
	};

	// hide static image
	setTimeout(
		function() {
			$('.slide_cover img').fadeOut(
				400, 
				function() {
					$('.slide_cover').hide();
				}
			);
		},
		4000
	);

	// run switcher
	setTimeout(
		function() {
			switching();
			setTimeout(arguments.callee, 4000);
		},
		0
	);
});
