$(document).ready(function() {
	//Parse typo3 content elements
	$('.promo-slideshow').append('<div class="ps-next" style="display:block;"><img id="next-arrow" class="right-button-image" src="/fileadmin/dbw/img/next.gif" alt="Next"/></div>');
	$('.promo-slideshow').append('<div class="ps-prev" style="display:block;"><img id="prev-arrow" class="right-button-image" src="/fileadmin/dbw/img/prev.gif" alt="Prev"/></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');
		}
	);
	$.bottles = new Object();
	$.bottles.running = false;
	$.bottles.stops = new Array( /* Bottom, Left, Opacity, Height */
		Array("80px","50px", "0.33", "90px"),//Far left
		Array("50px","118px", "0.66", "150px"),//Left
		Array("25px","232px", "1", "215px"),//Middle
		Array("50px","633px", "0.66", "150px"),//Right
		Array("80px","717px", "0.33", "90px"),//Far right
		Array("80px","50px", "0", "90px")//Hidden
	);
	$.bottles.bottles = new Array();
	$('.bottle').each(function() {
		$.bottles.bottles.push(this);
		/*$(this).click(function(){
			cS = $(this).attr('id').split('-')[1];
			if(cS > $.bottles.currentStop) {
				while(cS > $.bottles.currentStop) {
					$.bottles.rotateNP(1);
				}
			} else if(cS < $.bottles.currentStop) {
				while(cS < $.bottles.currentStop) {
					$.bottles.rotateNP(0);
				}
			} else {
				$.bottles.rotateBottles(cS);
			}
		});*/
	});
	$.bottles.currentStop = 0;
	$.bottles.lastStop = 0;
	$.bottles.rotateNP = function(direction) {
		if($.bottles.running) {
			return false;
		} else {
			$.bottles.running = true;
		}
		$.bottles.lastStop = $.bottles.currentStop;
		if(direction) {
			if($.bottles.currentStop >= $.bottles.bottles.length-1) {
				$.bottles.currentStop = 0;
			} else {
				$.bottles.currentStop++;
			}
		} else {
			if($.bottles.currentStop <= 0) {
				$.bottles.currentStop = $.bottles.bottles.length-1;
			} else {
				$.bottles.currentStop--;
			}
		}
		$.bottles.rotateBottles($.bottles.currentStop,'slow');
	}
	$.bottles.rotateBottles = function(cS,speed) {
		if(!speed) speed = 'slow';
		$.bottles.currentStop = cS;
		workOn = calcStops(cS,5,$.bottles.bottles.length-1);
		//$.bottles.currentStop = workOn[0];
		$($.bottles.bottles).each(function() {
			cS =	workOn[$(this).attr('id').split('-')[1]];
			if(cS>=0) {
				if($.bottles.stops[cS][2]==0) {
					$(this).fadeOut();
				} else {
					if($(this).is(':hidden')) {
						$(this).css('height',$.bottles.stops[cS][3]);
						$(this).css('bottom',$.bottles.stops[cS][0]);
						$(this).css('left',$.bottles.stops[cS][1]);
						$(this).css('opacity','0');
						$(this).show();
						$(this).fadeTo(speed,$.bottles.stops[cS][2]);
					} else {
						$(this).animate(
							{
								"height":$.bottles.stops[cS][3],
								"opacity":$.bottles.stops[cS][2],
								"bottom":$.bottles.stops[cS][0],
								"left":$.bottles.stops[cS][1]
							},
							speed,
							'',
							function() {
								$.bottles.running = false;
							}
						);
					}
				}
			} else {
				$(this).fadeOut();
			}
		});
		$('#blurb-'+$.bottles.lastStop).fadeOut('slow');
		$('#blurb-'+$.bottles.currentStop).fadeIn('slow');
		$.bottles.lastStop = $.bottles.currentStop;
	};
	
	$('.ps-next').click(function() {
		$.bottles.rotateNP(1);
		$(document).stopTime('slideshow');
	});
	$('.ps-prev').click(function() {
		$.bottles.rotateNP(0);
		$(document).stopTime('slideshow');
	});
	$.bottles.rotateBottles(0,1);
});
$(window).ready(function() {
	$(document).everyTime('8s','slideshow',function() {$.bottles.rotateNP(1);});
});
function calcStops(start,limit,stopsLength) {
	newStops = new Object();
	i = -Math.floor(limit/2);
	j = 0;
	while(j < limit) {
		if(start+i < 0) {
			//newStops.push(stopsLength+1+(start+i));
			newStops[stopsLength+1+(start+i)] = j;
		} else if(start+i > stopsLength) {
			//newStops.push((start+i)-stopsLength-1);
			newStops[(start+i)-stopsLength-1] = j;
		} else {
			//newStops.push(start+i);
			newStops[start+i] = j;
		}
		i++;
		j++;
	}
	return newStops;
}
