var $j = jQuery.noConflict();
$j(document).ready(function() {
	if ($j('#image_repository img').length > 1) begin_slideshow();
});
function begin_slideshow() {
	var curr = 0;
	var fade_speed = 350;
	var slideshow_speed = 6000;
	var total = $j('#image_repository img').length;
	
	// init slideshow
	var myID = window.setInterval(fade_slideshow,slideshow_speed);
	
	$j('#image_repository img').each(function() {
		var index = $j('#image_repository img').index(this);
		if (index != 0) {
			$j(this).fadeTo(0,0);
			$j('#captions span').eq(index).fadeTo(0,0,function() {
				$j(this).css({'display':'none'});
			});
			$j(this).mouseover(function() { window.clearInterval(myID); });
			$j(this).mouseout(function() { myID = window.setInterval(fade_slideshow,slideshow_speed); });
		}
	});
	$j('#home_slideshow a.b_previous, #home_slideshow a.b_next').click(function() {
		var direction = $j(this).attr('href');
		fade_slideshow(direction);
		window.clearInterval(myID);
		myID = window.setInterval(fade_slideshow,slideshow_speed);
		return false;
	});
	
	function fade_slideshow(dir) {
		$j('#image_repository img').eq(curr).fadeTo(fade_speed,0);
		$j('#captions span').eq(curr).fadeTo(fade_speed,0,function() {
			$j(this).css({'display':'none'});
		});
		if (dir == 'previous') {
			// back
			if (curr <= 0)
				curr = (total-1);
			else
				curr--;			
		} else {
			// forward
			if (curr < (total-1))
				curr++;
			else 
				curr = 0;
		}
		$j('#image_repository img').eq(curr).fadeTo(fade_speed,1);
		$j('#captions span').eq(curr).fadeTo(fade_speed,1,function() {
			$j(this).css({'display':'block'});
		});
	}
}
