var picarray = [];
var activePic;

window.addEvent('domready',function(){
	overlay = new Element('div', {
		'id': 'BoxOverlay',
		'styles': {
		    'display': 'block',
		    'z-index': '990',
		    'position': 'absolute',
		    'top': '0',
		    'left': '0',
		    'background-color': '#000',
		    'opacity': '0'
		}
	});
	
	overlay.injectInside(document.body);
	
	$('BoxOverlay').setStyles({
		width: window.getScrollWidth() + 'px',
		height: window.getScrollHeight() + 'px'
	});

	popy = new Element('div', {
		'id': 'PopUp',
		'styles': {
		    'display': 'block',
		    'z-index': '999',
		    'position': 'absolute',
		    'top': 150,
		    'left': 300,
		    'opacity': '0'
		}
	});
	
	popy.injectInside(document.body);
	
	if($defined(tags) && (tags.gallery == 1)){
		
		$$('#content_text img').each(function(img){
			zoomPath = img.src.split('/');
			zoomPathLength = zoomPath.length;
			zoomSrc = 'zoom_'+zoomPath[zoomPathLength-1];
			zoomSrc = zoomPath[zoomPathLength-1];
			
			var tmpSrc = "";
			for(var i=0;i<zoomPathLength-1;i++){
				tmpSrc += zoomPath[i]+'/';		
			}
			
			zoomSrc = tmpSrc+zoomSrc;
			picarray.push(zoomSrc);
		});
		
		$$('#content_text img').each(function(img,key){
			img.setStyles({
				'cursor':'pointer'
			})
			img.addEvents({
				'click': function(){
					lightBox(key,picarray);
				}
			})
		});
	}
});

function lightBox(key,picarray){
	if($defined($('PopUp'))) $('PopUp').empty();
	mO = new Fx.Morph($('BoxOverlay'), {
		duration: 700
	});
	mO.start({
		'opacity': '0.3'
	});
	
	mO = new Fx.Morph($('PopUp'), {
		duration: 700
	});
	mO.start({
		'opacity': '1'
	});
	activePic = key+1;
	var preImages = new Asset.images(picarray[key], {
		onComplete: function(){
			new Element('div', {
				'id': 'box',
				'styles': {
					'display': 'block',
					'z-index': '995',
					'padding':40,
					'background-color': '#fff'
					
				}
			}).injectInside($('PopUp'));
			pImg = new Element('img', {
				'src': picarray[key],
				'id': 'popImg',
				'styles': {
					'display': 'block',
					'z-index': '995',
					'background-color': '#fff'
				}
			}).injectInside($('box'));
			
		
		
			new Element('div', {
				'html':'Bild '+(key+1)+ ' von '+picarray.length,
				'id': 'activePictxt',
				'styles': {
				    'display': 'block',
				    'z-index': '995',
				    'position': 'absolute',
				    'top': 10,
				    'right': 40
				}
			}).injectInside($('PopUp'));
			
			new Element ('img', {
				'src':root+'images/next.gif',
				'id': 'next',
				'styles': {
					'display':'block',
					'z-index': '995',
					'position':'absolute',
					'top':($('PopUp').getStyle('height').toInt()/2)-10,
					'right':5
				},events:{
					'click':function(){
					
						activePic++;
					
						if(activePic > picarray.length){
							activePic = 1;
						}
						
						$('activePictxt').set('html','Bild '+activePic+ ' von '+picarray.length);
											
						var preImages = new Asset.images(picarray[activePic-1], {
							onComplete: function(){
								
								$('popImg').destroy();
								
								pImg = new Element('img', {
									'src': picarray[activePic-1],
									'id': 'popImg'
									,'styles': {
										'display': 'block',
										'z-index': '995',
										'background-color': '#fff'

									}
								}).injectInside($('box'));
								
								
								$('next').setStyle('top',($('PopUp').getStyle('height').toInt()/2)-10);
								$('prev').setStyle('top',($('PopUp').getStyle('height').toInt()/2)-10);
							}
						});			
					}
				}
			}).injectInside($('PopUp'));
			
			new Element ('img', {
				'src':root+'images/prev.gif',
				'id': 'prev',
				'styles': {
					'display':'block',
					'z-index': '995',
					'position':'absolute',
					'top':($('PopUp').getStyle('height').toInt()/2)-10,
					'left':5
				},events:{
					'click':function(){
						
						activePic--;
						
						if(activePic < 1){
							activePic = picarray.length;
						}
						
						$('activePictxt').set('html','Bild '+activePic+ ' von '+picarray.length);
						
						var preImages = new Asset.images(picarray[activePic-1], {
							onComplete: function(){
								
								$('popImg').destroy();
								
								pImg = new Element('img', {
									'src': picarray[activePic-1],
									'id': 'popImg'
									,'styles': {
									    'display': 'block',
									    'z-index': '995',
									    'padding': 40,
									    'background-color': '#fff'								    	
									}
								}).injectInside($('box'));
								$('next').setStyle('top',($('PopUp').getStyle('height').toInt()/2)-10);
								$('prev').setStyle('top',($('PopUp').getStyle('height').toInt()/2)-10);
							}
						});					
					}
				}
			}).injectInside($('PopUp'));
			
			new Element ('img', {
				'src':root+'images/close.gif',
				'id': 'ff',
				'styles': {
					'display':'block',
					'z-index': '995',
					'position':'absolute',
					'top':5,
					'left':5
				},events:{
					'click':function(){
					
					mO = new Fx.Morph($('BoxOverlay'), {
						duration: 700
					});
					mO.start({
						'opacity': '0'
					});
					
					mO = new Fx.Morph($('PopUp'), {
						duration: 700
					});
					mO.start({
						'opacity': '0'
					});			
						
					}
				}
			}).injectInside($('PopUp'));
			
		}.bind(this)
	});
}

