function slideshowNext() {
	last = $.slideShowCurrent;
	if($.slideShowCurrent >= ($.slideShowItems.length-1)) {
		$.slideShowCurrent=0;
	} else {
		$.slideShowCurrent++;
	}
	changeSlide(last);
}
function changeSlide(last) {
	if(typeof document.body.style.maxHeight == "undefined") {
		$('#'+$($.slideShowItems[$.slideShowCurrent]).attr('id')+' img:first').css('width','800px');
		$($.slideShowItems[last]).fadeOut('slow');
		$($.slideShowItems[$.slideShowCurrent]).fadeIn('slow');
	} else if($.browser.msie) {
		$('#'+$($.slideShowItems[$.slideShowCurrent]).attr('id')+' img:first').css('width','790px');
		$('#promo-dot-'+last).removeClass('filled');
		$('#promo-dot-'+$.slideShowCurrent).addClass('filled');
		$($.slideShowItems[last]).fadeOut('slow');
		$($.slideShowItems[$.slideShowCurrent]).fadeIn('slow');
	} else {
		$('#promo-dot-'+last).removeClass('filled');
		$('#promo-dot-'+$.slideShowCurrent).addClass('filled');
		$($.slideShowItems[last]).fadeOut('slow');
		$($.slideShowItems[$.slideShowCurrent]).fadeIn('slow');
	}
}
function slideshowPrev() {
	last = $.slideShowCurrent;
	if($.slideShowCurrent <= 0) {
		$.slideShowCurrent=($.slideShowItems.length-1);
	} else {
		$.slideShowCurrent--;
	}
	changeSlide(last);
}
$(document).ready(function() {
	var i = 1;
	$.maxHeight = 200;
	$.slideShowItems = new Array();
	$.slideShowDots = '<div id="promo-dots"><ul>';
	$.slideShowI = 0;
	$('.blurb').hide();
	$('.blurb').append('<span class="blurb-tr">&nbsp;</span><span class="blurb-tl">&nbsp;</span><span class="blurb-br">&nbsp;</span><span class="blurb-bl">&nbsp;</span>');
	$('.promo-slideshow div').each(function() {
		if( !$(this).hasClass('blurb') ) {
			$.slideShowItems.push(this);
			$.maxHeight = Math.max($.maxHeight,$(this).find('img').height());
			$(this).css('position','absolute');
			$(this).css('top','0');
			$(this).css('left','0');
			$(this).hide();
			$.slideShowDots += '<li id="promo-dot-'+$.slideShowI+'"></li>';
			$.slideShowI++;
		} else {
			$(this).css('position','absolute');
		}
	});
	$.slideShowDots += '</ul></div>';
	$('.promo-slideshow').append($.slideShowDots);
	$($.slideShowItems[0]).show();
	$('#promo-dot-0').addClass('filled');
	$('#promo-dots ul li').each(function() {
		$(this).click(function() {
			$(document).stopTime('slideshow');
			last = $.slideShowCurrent;
			$.slideShowCurrent=$(this).attr('id').split('-')[2];
			if(last!=$.slideShowCurrent) {
				changeSlide(last);
			}
		});
	});
	$.slideShowCurrent = 0;
	if($.maxHeight>200) {
		$('.promo-slideshow').animate({height:$.maxHeight},300);
	}
	$('.promo-slideshow').append('<div class="ps-next"><img alt="Next" src="/fileadmin/dbw/img/next.gif" style="opacity:0.5;" class="right-button-image" id="next-arrow"/></div>');
	$('.promo-slideshow').append('<div class="ps-prev"><img alt="Previous" src="/fileadmin/dbw/img/prev.gif" style="opacity:0.5;" class="right-button-image" id="prev-arrow"/></div>');
	$('#next-arrow').css('opacity','0.5');
	$('#prev-arrow').css('opacity','0.5');
	
	$('.ps-next').hover(
		function() {
			//$('#next-arrow').attr('src','/fileadmin/dbw/img/arrow_right_hover.png');
			$('#next-arrow').css('opacity','0.99');
		},
		function() {
			//$('#next-arrow').attr('src','/fileadmin/dbw/img/arrow_right.png');
			$('#next-arrow').css('opacity','0.5');
		}
	);
	$('.ps-prev').hover(
		function() {
			//$('#prev-arrow').attr('src','/fileadmin/dbw/img/arrow_left_hover.png');
			$('#prev-arrow').css('opacity','0.99');
		},
		function() {
			//$('#prev-arrow').attr('src','/fileadmin/dbw/img/arrow_left.png');
			$('#prev-arrow').css('opacity','0.5');
		}
	);
	$('.ps-next').click(function() {
		slideshowNext();
		$(document).stopTime('slideshow');
	});
	$('.ps-prev').click(function() {
		slideshowPrev();
		$(document).stopTime('slideshow');
	});
	if($.browser.msie) {
		$('#'+$($.slideShowItems[$.slideShowCurrent]).attr('id')+' img').css('width','800px');	
	}
});
$(window).ready(function() {
	$('.blurb').show();
	$(document).everyTime('8s','slideshow',function() {slideshowNext();});
	height = $('#slide_show').height()
	if(height ==239 || height ==0 || height > 300) { //Happens when the images aren't loaded before the script runs
		maxH = 0;
		 $($.slideShowItems).each(function() {
			maxH = Math.max(maxH,$(this).find('img').height());
		});
		if(maxH==0 || maxH > 300) {
			maxH = 241; //Giving up, just guess the height
		}
		$('#slide_show').animate({height: maxH+'px'},1000);
	}
});
