var LightBox = new Class({
	animate: false, 
	initialize: function() {
		this.open = false;
		// Get all links associated with the Light Box
		$each($$('a[target=lightbox]'), function(item){
			// Add "HREF" to Internet Explorer for CSS functions (:hover, :active, ect)
			if (Browser.Engine.trident) item.set("href", "#lightbox");
			// Track clicks on links in order to start the Light Box
			var lightbox = this;
			item.addEvent('click', function(e){
				// Stop event to prevent the browser from going to the "href" attribute
				e.stop();
				// Start the Light Box
				if (lightbox.open==false) lightbox.request(this.get('rel'));
			});
		}, this);
		
		// Start video
		if (window.location.toString().test('#video'))
			this.request('/sync/video/?lang='+language);
	},
	
	request: function(url) {
		var lightbox = this;
		this.open = true;
		// Request content from URL
		new Request.HTML({
			url: url, 
			evalScripts: false, 
			onSuccess: function (responseTree, responseElements, responseHTML, responseJavaScript) {
				lightbox.responseJavaScript = responseJavaScript;
				lightbox.prepare(responseHTML);
			}
		}).send();
	},
	
	prepare: function(responseHTML) {
		var lightbox = this;
		// Prepare Light Box
		this.box = new Element('div', {
			'class': 'lightbox', 
			'html': responseHTML, 
			'styles': {
				'opacity': 0,
				'position': 'absolute',
				'z-index': 5001
			}
		}).inject($(document.body));
		// Place Lightbox
		this.box.setStyles({
			'top': (window.getSize().y.toInt() - this.box.getSize().y.toInt()) / 2, 
			'left': (window.getSize().x.toInt() - this.box.getSize().x.toInt()) / 2
		});
		// Show Light Box
		setTimeout("lightbox.display();", 100);
	},
	
	displayShadow: function() {
		// Get browser window size
		var winx = window.getSize().x.toInt();
		var winy = window.getScrollSize().y.toInt();
		// Background Shadow
		this.shadow = new Element('div', {'class':'lightbox-shadow', 'styles': {
		    'width': winx-15, 
		    'height': winy, 
		    'position': 'absolute', 
		    'z-index': 5000, 
		    'top': 0,
		    'left': 0
		}}).inject(this.box, 'before');
	},

	display: function() {
		var lightbox = this;
		if (this.animate) {
			// Get Light Box Background
/*
			var background = this.box.getStyle('background-image');
			var RegExp = /^url\("?(.*?)"?\)$/ig;
			var matches = RegExp.exec(background);
*/
			var image = new Element('img', {'src': '/images/pages/video/shadow.png', 'styles': {
				'width': 0, 
				'height': 0, 
				'position': 'absolute', 
				'z-index': 5000, 
				'top': window.getSize().y.toInt() / 2, 
				'left': window.getSize().x.toInt() / 2
			}}).inject(lightbox.box, 'before');
			// Toggle Shadow
			lightbox.displayShadow();
			// Start animation
			new Fx.Morph(image, {
			    transition: Fx.Transitions.Quint.easeOut, 
			    duration: 500, 
			    onComplete: function() {
			    	// Replace image by box
			    	lightbox.box.setStyle('opacity', 1.0);
			    	image.destroy();
			    	// Initialize Ligth Box window
			    	lightbox.initLightBox();
			    }
			}).start({
			    'width': lightbox.box.getSize().x, 
			    'height': lightbox.box.getSize().y, 
			    'top': lightbox.box.getStyle('top'), 
			    'left': lightbox.box.getStyle('left')
			});
		} else {
			// Toggle Shadow
			this.displayShadow();
			// Show Light Box
			this.box.setStyle('opacity', 1.0);
			lightbox.initLightBox();
		}
	},
	
	initLightBox: function() {
		animation = false;
		// Active javascript
		$exec(this.responseJavaScript);
		// Get close button
		if ($chk(this.box.getElement('a.close'))) {
			// Add event to close button
			var lightbox = this;
			this.box.getElement('a.close').addEvent('click', function(e) {
				e.stop();
				lightbox.close();
			});
		}
	},
		
	close: function() {
		animation = true;
		// Remove Light Box
		this.shadow.destroy();
		this.box.destroy();
		this.open = false;
	}
});

var lightbox; window.addEvent('domready', function(e) { lightbox = new LightBox; });
