/*
	GalleryBox v0.7 - Lightbox Gallery
	by Andreas Hermann (http://www.squelsh.de) - MIT-style license.
	Code adapted from Slimbox by Christophe Beyls (http://www.digitalia.be)
*/

var Gallerybox = {

	init: function(options){
		this.options = $extend({
			resizeDuration: 400,
			resizeTransition: false,	// default transition
			initialWidth: 1000,
			initialHeight: 750,
			thumbWidth: 160,
			thumbHeight: 106,
			animateCaption: true,
			showCounter: true
		}, options || {});

		this.anchors = [];
		$each(document.links, function(el){
			if (el.rel && el.rel.test(/^gallerybox/i)){
				el.onclick = this.click.pass(el, this);
				this.anchors.push(el);
			}
		}, this);
		
		this.eventKeyDown = this.keyboardListener.bindAsEventListener(this);
		this.eventPosition = this.position.bind(this);

		this.overlay = new Element('div', {'id': 'lbOverlay'}).injectInside(document.body);

		this.center = new Element('div', {'id': 'lbCenter', 'styles': {'width': this.options.initialWidth, 'height': this.options.initialHeight, 'marginLeft': -(this.options.initialWidth/2), 'display': 'none'}}).injectInside(document.body);

		this.canvas = new Element('div', {'id': 'canvas', 'styles': {'width': this.options.initialWidth, 'height': this.options.initialHeight  }}).injectInside(this.center);

		this.leftThumbs = new Element('div', {'id': 'leftThumbs', 'styles': {'width': (this.options.thumbWidth + 20), 'height': this.options.initialHeight}}).injectInside(this.canvas);

		this.centerColumn = new Element('div', {'id': 'centerColumn', 'styles': {'position':'absolute','top':'0', 'left':(this.options.thumbWidth + 20), 'width': (this.options.initialWidth - this.options.thumbWidth - 20), 'height': this.options.initialHeight}}).injectInside(this.canvas);

		this.image = new Element('div', {'id': 'lbImage'}).injectInside(this.centerColumn);
		this.imageImg = new Element('div', {'id': 'imgimg', 'styles': {'position':'relative','top':'0', 'left':'0'}}).injectInside(this.image);;


		this.prevLink = new Element('a', {'id': 'lbPrevLink', 'href': '#', 'styles': {'display': 'none'}}).injectInside(this.image);
		this.nextLink = this.prevLink.clone().setProperty('id', 'lbNextLink').injectInside(this.image);
		this.prevLink.onclick = this.previous.bind(this);
		this.nextLink.onclick = this.next.bind(this);


		this.bottomContainer = new Element('div', {'id': 'lbBottomContainer', 'styles': {'display': 'none'}}).injectInside(this.centerColumn);
		this.bottom = new Element('div', {'id': 'lbBottom'}).injectInside(this.bottomContainer);
		new Element('a', {'id': 'lbCloseLink', 'href': '#'}).injectInside(this.bottom).onclick = this.overlay.onclick = this.close.bind(this);
		this.caption = new Element('div', {'id': 'lbCaption'}).injectInside(this.bottom);
		this.number = new Element('div', {'id': 'lbNumber'}).injectInside(this.bottom);
		new Element('div', {'styles': {'clear': 'both'}}).injectInside(this.bottom);

		var nextEffect = this.nextEffect.bind(this);
		this.fx = {
			overlay: this.overlay.effect('opacity', {duration: 500}).hide(),
			resize: this.image.effects($extend({duration: this.options.resizeDuration, onComplete: nextEffect}, this.options.resizeTransition ? {transition: this.options.resizeTransition} : {})),
			image: this.imageImg.effect('opacity', {duration: 500, onComplete: nextEffect}),
			bottom: this.bottom.effect('margin-top', {duration: 400})
		};

		this.preloadPrev = new Image();
		this.preloadNext = new Image();
	},

	click: function(link){
		this.caption.setHTML(link.title || '');
		var images = [];
		this.thumbs = [];
		
		this.imageBasename = link.rel.substring(link.rel.indexOf('[')+1, link.rel.indexOf(','));
		var numberOfImages = parseInt(link.rel.substring(link.rel.indexOf(',')+1, link.rel.indexOf(']')));
		
		//   ONLY IF FLASH VIDEO IS INCLUDED
		if(link.rel.test('video')) {
			this.vid_thumb = new Element('div', {'class': 'thumb', 'id': 'vid_thumb', 'styles': {'background-image': 'url(images/play.jpg)', 'width': this.options.thumbWidth, 'height': this.options.thumbHeight}}).injectInside(this.leftThumbs);
			this.videoWidth = parseInt(link.rel.substring(link.rel.indexOf('video')+5, link.rel.indexOf('*')));
			this.videoHeight = parseInt(link.rel.substring(link.rel.indexOf('*')+1, link.rel.length));
			this.vid_thumb.onclick = this.changeImage.pass('video', this);
		}

		// Push adrsses of big pics
		for (j = 0; j < numberOfImages; j++) {
			images.push(('images/produktionen/'+this.imageBasename+'_'+(j+1)+'.jpg'));
		}

		// Push FIRST thumb only
		this.thumbs.push( new Element('div', {'class':'thumb', 'id': '0_thumb', 'styles': {'background-image': 'url(images/produktionen/'+this.imageBasename+'_1t.jpg)', 'width': this.options.thumbWidth, 'height': this.options.thumbHeight }}).injectInside(this.leftThumbs) );
		this.thumbs[0].onclick = this.changeImage.pass(0, this);

		this.open(images, 0);

		for (j = 1; j < numberOfImages; j++) {
			this.thumbs.push( new Element('div', {'class':'thumb', 'id': (j)+'_thumb', 'styles': {'background-image': 'url(images/produktionen/'+this.imageBasename+'_'+(j+1)+'t.jpg)', 'width': this.options.thumbWidth, 'height': this.options.thumbHeight }}).injectInside(this.leftThumbs) );
			this.thumbs[j].onclick = this.changeImage.pass(j, this);
		}
		return false;

	},

	open: function(images, imageNum){
		this.images = images;
		this.videoFlag = false;
		this.position();
		this.setup(true);
		this.top = window.getScrollTop() + (window.getHeight() / 30);
		this.center.setStyles({top: this.top, display: ''});
		this.fx.overlay.start(0.8);
		return this.changeImage(imageNum);
	},

	position: function(){
		this.overlay.setStyles({'top': window.getScrollTop(), 'height': window.getHeight()});
	},

	setup: function(open){
		var elements = $A(document.getElementsByTagName('object'));
		elements.extend(document.getElementsByTagName(window.ie ? 'select' : 'embed'));
		elements.each(function(el){
			if (open) el.lbBackupStyle = el.style.visibility;
			el.style.visibility = open ? 'hidden' : el.lbBackupStyle;
		});
		var fn = open ? 'addEvent' : 'removeEvent';
		window[fn]('scroll', this.eventPosition)[fn]('resize', this.eventPosition);
		document[fn]('keydown', this.eventKeyDown);
		this.step = 0;
		this.activeImage = 0;
	},

	keyboardListener: function(event){
		switch (event.keyCode){
			case 27: case 88: case 67: this.close(); break;
			case 37: case 80: this.previous(); break;	
			case 39: case 78: this.next();
		}
	},

	previous: function(){
		return this.changeImage(this.activeImage-1);
	},

	next: function(){
		return this.changeImage(this.activeImage+1);
	},

	changeImage: function(imageNum){
		// VIDEO FUNCTION
		if(imageNum == 'video') {
			this.videoFlag = true;
			this.thumbs[this.activeImage].style.border = '1px solid #000';
			this.vid_thumb.style.border = '1px solid #fff';
			this.bottomContainer.style.display = this.prevLink.style.display = this.nextLink.style.display = 'none';
			this.fx.image.hide();
			this.centerColumn.className = 'lbLoading';
			
			this.step = 1;
			this.preload = new Image();
			this.preload.width = this.videoWidth;
			this.preload.height = this.videoHeight;
			this.nextEffect();
			return false;
		} else {
		// END VIDEO FUNCTION
			if(this.videoFlag) {
				this.videoPlayer.remove();
				this.videoFlag = false;
				this.vid_thumb.style.border = '1px solid #000';
			}
			if (this.step || (imageNum < 0) || (imageNum >= this.images.length)) return false;
			
			this.thumbs[this.activeImage].style.border = '1px solid #000';
			this.activeImage = imageNum;
			this.thumbs[imageNum].style.border = '1px solid #fff';
	
			this.bottomContainer.style.display = this.prevLink.style.display = this.nextLink.style.display = 'none';
			this.fx.image.hide();
			this.centerColumn.className = 'lbLoading';
			
			this.step = 1;
			this.preload = new Image();
			this.preload.onload = this.nextEffect.bind(this);
			this.preload.src = this.images[imageNum];
			return false;
		}
	},

	nextEffect: function(){
		switch (this.step++){
		case 1:
			this.centerColumn.className = '';
			this.bottom.style.width = this.preload.width+'px';
			if(! this.videoFlag) this.prevLink.style.height = this.nextLink.style.height = this.preload.height+'px';

			this.imgWidth = this.preload.width;
			this.imgHeight = this.preload.height;

			if(! this.videoFlag) {
				this.number.setHTML((!this.options.showCounter || (this.images.length == 1)) ? '' : 'Bild '+(this.activeImage+1)+' von '+this.images.length);
				if (this.activeImage) this.preloadPrev.src = this.images[this.activeImage-1];
				if (this.activeImage != (this.images.length - 1)) this.preloadNext.src = this.images[this.activeImage+1];
			} else {
				this.number.setHTML('Video');
			}
			
			if(this.preload.height != parseInt(this.image.style.height)) {
				this.resizeFlag = true;
				this.fx.resize.start({height: this.preload.height, top: (this.options.initialHeight - parseInt(this.imgHeight) - 40)/2 });
				break;
			}
			this.step++;
		case 2:
			if(this.preload.width != parseInt(this.image.style.width)) {
				this.resizeFlag = true;
				this.fx.resize.start({width: this.preload.width, left: ((this.options.initialWidth - this.options.thumbWidth - 40) - parseInt(this.imgWidth) )/2});
				break;
			}
			this.step++;
		case 3:
			this.imageImg.setStyles({width: this.preload.width, height: this.preload.height});
			if(! this.videoFlag) {
				this.imageImg.style.backgroundImage = 'url('+this.images[this.activeImage]+')';
			} else {
				this.videoPlayer = new Element('div', {'id': 'player', 'styles': {'position':'absolute','top':'0', 'left':'0'}}).injectInside(this.image);
				this.videoPlayer.setHTML('<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" width="'+this.videoWidth+'" height="'+this.videoHeight+'" id="FLVPlayer">  <param name="movie" value="FLVPlayer_Progressive.swf" /> <param name="salign" value="lt" /> <param name="quality" value="high" / ><param name="scale" value="noscale" />  <param name="FlashVars" value="&MM_ComponentVersion=1&skinName=Clear_Skin_1&streamName=flashvideos/'+this.imageBasename+'.flv&autoPlay=true&autoRewind=false" />  <embed src="FLVPlayer_Progressive.swf" flashvars="&MM_ComponentVersion=1&skinName=Clear_Skin_1&streamName=flashvideos/'+this.imageBasename+'.flv&autoPlay=true&autoRewind=false" quality="high" scale="noscale" width="'+this.videoWidth+'" height="'+this.videoHeight+'" name="FLVPlayer" salign="LT" type="application/x-shockwave-flash" pluginspage="http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash" /> </object>');


			}

			this.bottomContainer.setStyles({top: (parseInt(this.image.style.top) + parseInt(this.imgHeight) + 20), height: 0, left: this.image.style.left, display: ''});

			if(this.resizeFlag) {
				this.fx.bottom.set(-this.bottom.offsetHeight);
				this.bottomContainer.style.height = '';
				this.fx.bottom.start(0);
			}else{
				this.bottomContainer.style.height = '';
			}
			if(! this.videoFlag) {
				this.fx.image.start(1);
				break;
			}
		case 4:
			if(! this.videoFlag) if (this.activeImage) this.prevLink.style.display = '';
			if(! this.videoFlag) if (this.activeImage != (this.images.length - 1)) this.nextLink.style.display = '';
			this.step = 0;
			this.resizeFlag = false;
		}
	},


	close: function(){
		if (this.step < 0) return;
		this.step = -1;
		if(this.videoFlag) this.videoPlayer.remove();
		this.leftThumbs.empty();
		if (this.preload){
			this.preload.onload = Class.empty;
			this.preload = null;
		}
		for (var f in this.fx) this.fx[f].stop();
		this.center.style.display = this.bottomContainer.style.display = 'none';
		this.fx.overlay.chain(this.setup.pass(false, this)).start(0);
		return false;
	}
};

window.addEvent('domready', Gallerybox.init.bind(Gallerybox));












































































































