var Slimbox;
(function() {

	var state = 0, options, images, activeImage, prevImage, nextImage, top, eventKeyDown, fx, preload,
	overlay, center, image, prevLink, nextLink, bottomContainer, bottom, caption, number;
	
	var gFlashFile;
	var myPano;
	
	var myFloorPlan = '';
	var mySlideShowLang = '';
	var mySlideShowAlbum = '';
	var mySlideShowGallery = 0;
	
	
	window.addEvent("domready", function() {
		eventKeyDown = keyDown.bindWithEvent();
		$(document.body).adopt(
			$$([
				overlay = new Element("div", {id: "lbOverlay"}),
				center = new Element("div", {id: "lbCenter"}),
				bottomContainer = new Element("div", {id: "lbBottomContainer"})
			]).setStyle("display", "none")
		);

		image = new Element("div", {id: "lbImage"}).injectInside(center).adopt();

		var closeLink;
		bottom = new Element("div", {id: "lbBottom"}).injectInside(bottomContainer).adopt(
			closeLink = new Element("a", {id: "lbCloseLink", href: "#"}),
			caption = new Element("div", {id: "lbCaption"}),
			number = new Element("div", {id: "lbNumber"}),
			new Element("div", {styles: {clear: "both"}})
		);
		closeLink.onclick = close;
		overlay.onclick = Slimbox.closeOverlay;
		
		fx = {
			overlay: overlay.effect("opacity", {duration: 800, onComplete: nextEffect}).set(0),
			image: image.effect("opacity", {duration: 520, onComplete: nextEffect}),
			bottom: bottom.effect("margin-top", {duration: 0})
		};
	});


	/*
		API
	*/

	Slimbox = {
	
		getFloorPlan: function() {
			return myFloorPlan;
		},
		
		openFinal: function(_images, pano, _options) {
			options = $extend({
				loop: false,				// Allows to navigate between first and last images
				overlayOpacity: 0.75,			// 1 is opaque, 0 is completely transparent (change the color in the CSS file)
				resizeDuration: 1400,			// Duration of each of the box resize animations (in milliseconds)
				resizeTransition: false,		// Default transition in mootools
				initialWidth: 800,			// Initial width of the box (in pixels)
				initialHeight: 520,			// Initial height of the box (in pixels)
				animateCaption: false,
				showCounter: false,			// If true, a counter will only be shown if there is more than 1 image to display
				counterText: ""		// Translate or change as you wish
			}, _options || {});

			if( _images == 'panoholder.swf' ) {
				_images = 'panorama/embedPano.swf';
				mySlideShowLang = 'de';
			}
			
			if( _images == 'panoholder_fr.swf' ) {
				_images = 'panorama/embedPano.swf';
				mySlideShowLang = 'fr';
			}
			
			
			if( _images == 'panoholder_it.swf' ) {
				_images = 'panorama/embedPano.swf';
				mySlideShowLang = 'it';
			}
			
			if( _images == 'panoholder_en.swf' ) {
				_images = 'panorama/embedPano.swf';
				mySlideShowLang = 'en';
			}
			
			
			
			if( _images =='panorama/embedPano.swf' ) {
				$('lbCenter').style.background = '#ffffff';
			} else if( _images == 'beautyplayer/beautyplayer.swf' ) {
				$('lbCenter').style.background = '#ECE2C9';
			} else if( _images == 'slideshows/slideshow.swf' ) {
				$('lbCenter').style.background = '#ECE2C9';
			} else {
				$('lbCenter').style.background = 'transparent url(/web/media/base/images/whiteBgEbrochure.gif) top center no-repeat';
			}
			
			if (typeof _images == "string") {
				_images = "/web/media/base/" + _images; //[[_images,startImage]];
				myPano = pano;
			}
			
	

			images = _images;
			options.loop = options.loop && (images.length > 1);
			position();
			setup(true);
			top = window.getScrollTop();// + (window.getHeight() / 15);
			center.setStyles({top: top, width: options.initialWidth, height: options.initialHeight, marginLeft: -(options.initialWidth/2), display: ""});
			fx.overlay.start(options.overlayOpacity);
			state = 1;
			return changeImage(_images);
		},
		
		open: function( _images, pano, _options ) {
			myFloorPlan = '';
			
			if( _options ) {
				if( _options.length == 2 )
					mySlideShowLang = _options;
			}

			myFloorPlan = '';
			mySlideShowAlbum = '';
			mySlideShowGallery = 0;
			
			return this.openFinal( _images, pano, _options );
		},
		
		openWithPlan: function( _images, pano, plan ) {
			
			if( plan ) {
				if( plan.substring( 0, 4) == 'map:' ) {
					myFloorPlan = plan.substring( 4 );
				}	
			}
			
			mySlideShowAlbum = '';
			mySlideShowGallery = 0;
			
			return this.openFinal( _images, pano );
		},
		
		openSlideshow: function( _images, lang, album, gallery ) {
			myFloorPlan = '';
			
			mySlideShowLang = lang;
			mySlideShowAlbum = album;
			mySlideShowGallery = gallery;
			
			return this.openFinal( _images, album );
		},
		
		close: function() {
			close();
		},
		
		closeOverlay: function() {
			c = document.getElementById( 'lbImage' );

			myParent = c.parentNode;
			
			swfobject.removeSWF( 'lbImage' );
			
			d = new Element('div', {id: 'lbImage'});
			d.inject( myParent );
			
			close();
		}
	};

	Element.extend({
		slimbox: function(_options, linkMapper) {
			$$(this).slimbox(_options, linkMapper);

			return this;
		}
	});

	Elements.extend({
		slimbox: function(_options, linkMapper, linksFilter) {
			linkMapper = linkMapper || function(el) {
				return [el.href, el.title];
			};

			linksFilter = linksFilter || function() {
				return true;
			};

			var links = this;

			links.forEach(function(link) {
				link.onclick = function() {
					// Build the list of images that will be displayed
					var filteredLinks = links.filter(linksFilter, this);
					return Slimbox.open(filteredLinks.map(linkMapper), filteredLinks.indexOf(this), _options);
				};
			});

			return links;
		}
	});


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

	function setup(open) {
		["object", window.ie ? "select" : "embed"].forEach(function(tag) {
			$each(document.getElementsByTagName(tag), function(el) {
				if (open) el._slimbox = el.style.visibility;
				
				if( el._slimbox == undefined )
					el._slimbox = "visible";
					
				el.style.visibility = open ? "hidden" : el._slimbox;
			});
		});

		overlay.style.display = open ? "" : "none";

		var fn = open ? "addEvent" : "removeEvent";
		window[fn]("scroll", position)[fn]("resize", position);
		document[fn]("keydown", eventKeyDown);
	}

	function keyDown(event) {
		switch(event.code) {
			case 27:	// Esc
			case 88:	// 'x'
			case 67:	// 'c'
				close();
				break;
		}
		// Prevent default keyboard action (like navigating inside the page)
		event.preventDefault();
	}


	function changeImage(imageIndex) {
		if ((state == 1) ) {
			state = 2;
			nextImage = -1;
			center.className = "lbLoading";

			gFlashFile = imageIndex;
			//nextEffect( imageIndex );
		}

		return false;
	}

	function nextEffect() {
		flashfile = gFlashFile;
		
		switch (state++) {
			case 2:
				center.className = "";
				
				//image.setStyles({backgroundImage: "url(" + images[activeImage][0] + ")", display: ""});			
			
				myPano = myPano.replace( "_small", "" );
				var flashvars = {
					
					language: mySlideShowLang,
					loadPano: myPano,
					loadParams: '',

					xmlfile: "/web/media/base/slideshows/gallery_" + mySlideShowLang + ".xml", // Slideshow
					xmlfiletype: "Default",
					gallery: mySlideShowGallery,
					album : mySlideShowAlbum,
					
					beautyxml: "/web/media/base/beautyplayer/gallery_" + mySlideShowLang + ".xml" // Beauty Gallery
				};
				
				if( myFloorPlan && myFloorPlan != '' ) {
					flashvars.overlayMap = true;
				} else {
					flashvars.overlayMap = false;
				}

				var params = {
					wmode: "transparent",
					allowFullScreen: "true",
					allowScriptAccess: "sameDomain"
				}

				
				if( swfobject.hasFlashPlayerVersion("9.0.0") ) {
				  // has Flash
				} else {
				  $('lbCenter').style.background = '#fff url(/web/media/base/images/panorama-back.jpg) top center no-repeat';
				}
				
				swfobject.embedSWF(
					flashfile, 
					"lbImage", 
					"800",
					"520", 
					"9.0.0", 
					"/web/js/expressInstall.swf",
					flashvars,
					params
				);				
				
	
				
				$$(image, bottom).setStyle("width", 800 );
				$$(image, prevLink, nextLink).setStyle("height", 520 );

				caption.setHTML("");
				number.setHTML("");

				if (center.clientHeight != image.offsetHeight) {
					//fx.resize.start({height: image.offsetHeight});
					break;
				}
				state++;
			case 3:
				if (center.clientWidth != image.offsetWidth) {
					//fx.resize.start({width: image.offsetWidth, marginLeft: -image.offsetWidth/2});
					break;
				}
				state++;
			case 4:
				bottomContainer.setStyles({top: top + center.clientHeight, marginLeft: center.style.marginLeft, visibility: "hidden", display: ""});
				fx.image.start(1);
				break;
			case 5:
				if (prevImage >= 0) prevLink.style.display = "";
				if (nextImage >= 0) nextLink.style.display = "";
				if (options.animateCaption) {
					fx.bottom.set(-bottom.offsetHeight).start(0);
				}
				bottomContainer.style.visibility = "";
				state = 1;
		}
	}


	function close() {
		if (state) {
			state = 0;
			//preload.onload = Class.empty;
			for (var f in fx) fx[f].stop();
			$$(center, bottomContainer).setStyle("display", "none");
			fx.overlay.chain(setup).start(0);
		}

		return false;
	}
	

	

})();

function thisMovie( movieName ) {
	if(navigator.appName.indexOf('Microsoft') != -1) {
		return window[movieName];
	} else {
		return document[movieName];
	}
}

function closeFlashPanorama( e ) {
	Slimbox.close()
}

function ClosePano() {
	Slimbox.close()
}

