var ImageWidth;
var ImageHeight;
var LeftMargin;
var TopMargin;

function slideSwitch() {
	var $active = $('#slideshow IMG.active');
	
	if ( $active.length == 0 ) $active = $('#slideshow IMG:last');
	
	var $next = $active.next().length ? $active.next()
		: $('#slideshow IMG:first');
		
	setImageDimensions($next);
	setMargins();
	
	$active.addClass('last-active');	
	
	$active.animate({opacity: 0.0}, 500, function(){
		$active.hide();
		$next.css({opacity: 0.0})
			.css('margin-left',LeftMargin)
			.css('margin-top',TopMargin)
			.css('height',ImageHeight)
			.css('width',ImageWidth)
			.addClass('active')
			.show()
			.animate({opacity: 1.0}, 500, function() {
				$active.removeClass('active last-active');
		});
	});
}
	
function setImageDimensions(_image){
	var multiplier = 1;
	
	if (_image.width() > 500 || _image.height() > 332) 
	{
		if ((500/_image.width()) > (332/_image.height())){
			multiplier = 332/_image.height();
		} else {
			multiplier = 500/_image.width();
		}
	}
	ImageWidth = _image.width() * multiplier;
	ImageHeight = _image.height() * multiplier;
}

function setMargins(){
	if(ImageWidth < 550){
		LeftMargin = ((550 - ImageWidth) / 2);
	}
	
	if(ImageHeight < 382){
		TopMargin = ((382 - ImageHeight) / 2);
	}
}
