$().ready(function()
{
	$('.product_img_switches ol li').remove();
	
	var products_to_cycle = ($('#products li').length > 1);
	
	var product_cycle = false;
	
	$('ol#products > li').each(function(productnum)
	{
		//console.log(productnum);
		var lis = $(this).find('.product_imgs li').length;
		if ( lis > 1 )
		{
			var _pager = $(this).find('.product_img_switches ol');
			//console.log(_pager.attr('id'));
			
			var product_imgs = $(this).find('.product_imgs');
			var product_imgs_first_id = product_imgs.find('li:first').attr('id');
			
			product_imgs.cycle(
			{
				fx:'fade',
				timeout:4000,
				pager: _pager,
				pause:true,
				pauseOnPagerHover:true,
				pagerAnchorBuilder: function(index, DOMelement)
				{
					return '<li><a href="#"><!-- --></a></li>';
				},
				before: function(currSlideElement, nextSlideElement, options, forwardFlag)
				{
					var next_id = $(nextSlideElement).attr('id');
					//console.log(next_id);
					//console.log(product_imgs_first_id);
					if ( next_id === product_imgs_first_id )
					{
						//$('#products').cycle('next');
						/*if ( $('#product_switches').find('a.activeSlide').parents('li').next().length === 1 )
						{
							var foo = $('#product_switches').find('a.activeSlide').parents('li').next();
						}
						else
						{
							var foo = $('#product_switches').find('a.activeSlide').parents('li').prev();
						}
						console.log(foo);
						foo.click();*/
					}
				}
			});
			
			if ( productnum !== 0 )
			{
				product_imgs.cycle('pause');
			}
		}
	});
	
	$('#product_switches li').remove();
	
	if ( products_to_cycle  )
	{
		product_cycle = $('#products').cycle(
		{
			fx:'fade',
			timeout:0,
			pager:'#product_switches ol',
			pagerAnchorBuilder: function(index, DOMelement)
			{
				return '<li><a href="#"><!-- --></a></li>';
			},
			pagerClick: function(zeroBasedSlideIndex, slideElement)
			{
				//console.log('PagerClick');
				// Produktbilder-Animation starten
				//console.log($(slideElement).find('.product_imgs'));
				$(slideElement).find('.product_imgs').cycle(0);
				$(slideElement).find('.product_imgs').cycle('resume');
				
			},
			prevNextClick: function(isNext, zeroBasedSlideIndex, slideElement)
			{
				//console.log('prevNextClick');
				//console.log(slideElement);
				//$(slideElement).find('.product_imgs').cycle(0);
				//$(slideElement).find('.product_imgs').cycle('resume');
			}
		});
	}
	

	$('#teaser_close').click(function()
	{
		$('#teaser').remove();
		return false;
	
	});

});