$(document).ready(function(){
	$('ul.nav > li:last').css({
		background: 'none'
	});
	$('ul.metanav > li:first').css({
		background: 'none'
	});
	$('h1.fbh').click(function(){
		$(this).next('.fbc').toggle;
	});
	$('ul.slider').css({
		width: ($('ul.slider > li').length * 981)
	})
	
	$("#banner").easySlider({
		prevText:' ',
		nextText:' '
	});
	
	$('.news-single-img').width($('.news-single-img > img').width());
	
	$('ul.nav > li').each(function(){
		$(this).css({
			width: $(this).outerWidth()+12
		});
	});
	
	function formatTitle(title, currentArray, currentIndex, currentOpts) {
	    return '<div id="tip7-title"><span><a href="javascript:;" onclick="$.fancybox.close();"><img src="fileadmin/templates/img/icons/closelabel.gif" /></a></span>' + (title && title.length ? '<b>' + title + '</b>' : '' ) + 'Image ' + (currentIndex + 1) + ' of ' + currentArray.length + '</div>';
	}	
	$("a.flb").fancybox({
		'speedIn'			: 600, 
		'speedOut'			: 200, 
		'overlayShow'		: true,
		'showCloseButton'	: false,
		'titlePosition' 	: 'inside',
		'titleFormat'		: formatTitle
	});
	
	if ($("div.plastflow").length > 0) {
		// steuerungsvariablen
		var verkleinerung	= 3;
		var showItemsAnz	= 3;	// immer plus 2 bzw. ungerade
		var animationSpeed	= 500;	// mili second
		
		var thumbHeight		= 100;	
		var thumbWidth		= 100;
		var bigThumbHeight	= 180;	
		var bigThumbWidth	= 206;
		
		var versatz			= 100;	// könnte ein array sein je nachdem wieviel elemente angezeigt werden sollen
		var firstPic		= 1;	// number ob Fist big Thumb beginns at 0 (zero)
	

		$("div.plastflow").each(function(intIndex){	
		
			// initialisierung
			var liCount 	= $('> ul > li',this).length;
			
			$('span.pfgespic',this).text(liCount);
			$('span.pfcurrpic').text(firstPic+1);
			
			var pfWidth 	= $(this).outerWidth();
			var pfHMiddle 	= Math.round(pfWidth/2);
			var pfHeight 	= $(this).outerHeight();
			var pfVMiddle 	= Math.round(pfHeight/2);
	
			$('ul.pflist > li',this).css({
				top: (pfVMiddle - thumbHeight/2),
				left: (pfHMiddle - thumbWidth/2)
			});
			
			// versatzposition - später mehrere anhand des versatz arrays und der showItemAnz
			var posItemLeft 	= Math.round(pfHMiddle - (versatz+thumbWidth));
			var posItemRight	= Math.round(pfHMiddle + versatz);

			// daten dem Objekt mitgeben
			$(this).data('liCount', liCount);
			$(this).data('pfWidth', pfWidth);
			$(this).data('pfHMiddle', pfHMiddle);
			$(this).data('pfHeight', pfHeight);
			$(this).data('pfVMiddle', pfVMiddle);	
			$(this).data('posItemLeft', posItemLeft);
			$(this).data('posItemRight', posItemRight);	
					
			// build Thumbnails
			$('> ul.pflist > li > a > img',this).each(function(intIndex){
				$(this).parent().parent().css({
					zIndex: (liCount - intIndex)
				});
	
				$(this).parent().wrap('<div class="thumbwrap"></div>');	
				$(this).parent().parent('div.thumbwrap').css({
					height: thumbHeight,
					width: thumbWidth
				});
				
				//$(this).parent().parent().before('<div class="preload">&nbsp;</div>');
				
				var newHeight = Math.round($(this).height() / verkleinerung);
				var newWidth = Math.round($(this).width() / verkleinerung);
				$(this).attr({
					height: newHeight
				}).attr({
					width: newWidth
				}).css({
					position: 'absolute',
					top: ((thumbHeight / 2) - (newHeight / 2)) + 'px',
					left: ((thumbWidth / 2) - (newWidth / 2)) + 'px'
				});
			});
			
			// Startbild initialisieren
			$('> ul > li:eq('+firstPic+')',this).addClass('open').css({
				zIndex: liCount
			});
			$('li.open > div.thumbwrap',this).css({
					height: bigThumbHeight,
					width: bigThumbWidth
			});
			$('li.open > div.thumbwrap > a > img',this).css({
					top: ((bigThumbHeight / 2) - ( parseInt($('li.open > div.thumbwrap > a > img',this).attr('height')) / 2)) + 'px',
					left: ((bigThumbWidth / 2) - ( parseInt($('li.open > div.thumbwrap > a > img',this).attr('width')) / 2)) + 'px'
			});
			$('li.open',this).css({
					left: (pfHMiddle - bigThumbWidth/2),
					top: (pfVMiddle - bigThumbHeight/2)
			});
			
			// restliche Bilder Positionieren und verändern
			$('> ul > li:gt('+firstPic+')',this).css({left: posItemRight});
			var tmpzIndex = $('> ul > li:lt('+firstPic+')',this).css('zIndex');
			$('> ul > li:lt('+firstPic+')',this).css({
				left: posItemLeft,
				zIndex: 1
			});
			$('> ul > li:not(:eq('+firstPic+')) > div.thumbwrap > a  > img',this).css({opacity: 0.5});				

			// Preloade images
			var preloaded = 0;
			$('img.thumb',this).preload({
				onRequest: function(data){			
					if(preloaded == 0){
						var imgSrc = $("> ul.pflist > li:nth-child("+ (data.index+2) +") > .thumbwrap > a > img.thumb",this).attr('src');
						var imgAlt = $("> ul.pflist > li:nth-child("+ (data.index+2) +") > .thumbwrap > a > img.thumb",this).attr('alt');
						$("div.plastflow > ul.pflist > li:nth-child("+ (data.index+2) +")").addClass('open');
						preloaded = 1;
					}
					$("> ul.pflist > li:nth-child("+ (data.index+2) +") > .thumbwrap > a > img.thumb",this).attr('id',(data.index+2));
					$("> ul.pflist > li:nth-child("+ (data.index+2) +") > .thumbwrap > a > img.thumb",this).prev('div.preload').remove();
				}
			});
		});

		function move($plastflowObject,moveToPos){
			
			// initialisierung
			var actPic 		= $('> ul > li.open',$plastflowObject).index();	 
			var liCount 	= $plastflowObject.data('liCount');

			if ((actPic != 0 || moveToPos == "left") && ((actPic + 1) != liCount || moveToPos == "right")) {
			
				var pfWidth = $plastflowObject.data('pfWidth');
				var pfHMiddle = $plastflowObject.data('pfHMiddle');
				var pfHeight = $plastflowObject.data('pfHeight');
				var pfVMiddle = $plastflowObject.data('pfVMiddle');
				var posItemLeft = $plastflowObject.data('posItemLeft', posItemLeft);
				var posItemRight = $plastflowObject.data('posItemRight', posItemRight);
				
				var direction = 0;
				var nextPic = 0;
				var chZindex = 0;
				
				if (moveToPos == "right") {
					chZindex = 1;
					nextPic = actPic - 1;
					direction = posItemRight;
					
				}else if (moveToPos == "left") {
					chZindex = -1;
					nextPic = actPic + 1;
					direction = posItemLeft;
				}
				
				// nächste Bild zu Big
				$('> ul > li:eq(' + nextPic + ')', $plastflowObject).addClass('open').animate({
					left: (pfHMiddle - bigThumbWidth / 2),
					top: (pfVMiddle - bigThumbHeight / 2)
				}, animationSpeed);
				$('> ul > li:eq(' + nextPic + ') > div.thumbwrap', $plastflowObject).animate({
					height: bigThumbHeight,
					width: bigThumbWidth
				}, animationSpeed);
				$('> ul > li:eq(' + nextPic + ')', $plastflowObject).css({
					zIndex: liCount
				});
				setTimeout(function(){
					$('> ul > li:eq(' + nextPic + ') > div.thumbwrap > a > img', $plastflowObject).css({
						opacity: 1
					});
				}, animationSpeed);
				
				var tmpItemHeight = parseInt($('> ul > li:eq(' + nextPic + ') > div.thumbwrap > a > img.thumb', $plastflowObject).attr('height'));
				var tmpItemWidth = parseInt($('> ul > li:eq(' + nextPic + ') > div.thumbwrap > a > img.thumb', $plastflowObject).attr('width'));
				$('> ul > li:eq(' + nextPic + ') > div.thumbwrap > a > img.thumb', $plastflowObject).animate({
					top: ((bigThumbHeight / 2) - (tmpItemHeight / 2)) + 'px',
					left: ((bigThumbWidth / 2) - (tmpItemWidth / 2)) + 'px'
				}, animationSpeed);
				
				
				// aktuelles Bild zu Small
				$('> ul > li:eq(' + actPic + ')', $plastflowObject).removeClass('open').animate({
					left: direction,
					top: (pfVMiddle - thumbHeight / 2)
				}, animationSpeed);
				$('> ul > li:eq(' + actPic + ') > div.thumbwrap', $plastflowObject).animate({
					height: thumbHeight,
					width: thumbWidth
				}, animationSpeed);
				$('> ul > li:eq(' + actPic + ') > div.thumbwrap > a > img', $plastflowObject).css({
					opacity: 0.5
				});
				
				var tmpItemHeight = parseInt($('> ul > li:eq(' + actPic + ') > div.thumbwrap > a > img.thumb', $plastflowObject).attr('height'));
				var tmpItemWidth = parseInt($('> ul > li:eq(' + actPic + ') > div.thumbwrap > a > img.thumb', $plastflowObject).attr('width'));
				$('> ul > li:eq(' + actPic + ') > div.thumbwrap > a > img.thumb', $plastflowObject).animate({
					top: ((thumbHeight / 2) - (tmpItemHeight / 2)) + 'px',
					left: ((thumbWidth / 2) - (tmpItemWidth / 2)) + 'px'
				}, animationSpeed);
				
				
				// z-index manipulation
				var tmpzIndex = parseInt($('> ul > li:eq(' + (actPic + chZindex) + ')', $plastflowObject).css('zIndex'));
				if (!tmpzIndex) 
					tmpzIndex = 0;
				$('> ul > li:eq(' + actPic + ')', $plastflowObject).css({
					zIndex: (tmpzIndex + 1)
				});
				
				// return value		
				$('span.pfcurrpic', $plastflowObject).text(nextPic + 1);
			}				
		};		
		

		$('.pfnav > .pfnext').click(function(){
			move($(this).parent().parent(),"left");
			openpicpath = $('.pflist > li.open').find('img').attr('src');
			$('span.pfog').attr('onclick','$("a.flb[rel=group11],a.flb[href='+ openpicpath +'.jpg]").trigger("click");');
		});
		$('.pfnav > .pfback').click(function(){
			move($(this).parent().parent(),"right")
			openpicpath = $('.pflist > li.open').find('img').attr('src');
			$('span.pfog').attr('onclick','$("a.flb[rel=group11],a.flb[href='+ openpicpath +'.jpg]").trigger("click");');
		});
		
		openpicpath = $('.pflist > li.open').find('img').attr('src');
		$('span.pfog').attr('onclick','hmmm');
	}	
});
