var Lightbox = new Class({
	
	initialize: function() {
		var links = $(document.body).getElements('a[target=lightbox]');
		$each(links, function(item){
			item.addEvent('click', function(e){
				e.stop();
				var url = item.get('href');
				lightbox.get(url);
			});
		});
	},
	
	get: function(url) {
		new Request.HTML({ 
			url: url,
			onSuccess: function (responseTree, responseElements, responseHTML, responseJavaScript) {
				lightbox.show(responseHTML, responseJavaScript);
			}
		}).send(); },
	
	show: function(responseHTML, responseJavaScript) {
		// Window size
		var winx = window.getSize().x;
		var winy = window.getScrollSize().y;
		// Background Shadow
		var shadow = new Element('div', {'class': 'lightbox-shadow', 'styles': {
			'width': winx-15, 
			'height': winy, 
			'background-color': '#000', 
			'z-index': 5000, 
			'position': 'absolute', 
			'top': 0,
			'left': 0, 
			'opacity': 0.8
		}}).inject($(document.body));
		// Box
		var box = new Element('div', {'class': 'lightbox', 'styles': {
			'width': 740, 
			'z-index': 6000, 
			'position': 'absolute', 
			'background-color': '#4c4c4c',
			'top': (window.getScroll().y+150),
			'left': ((winx-740)/2)
		}}).inject(shadow, 'after');
		// Inject content
		$exec(responseJavaScript);
		//active la function trouver
		flash();
		//var result = new Element('div', {'html': responseHTML}).inject(box);
		// FOR FLASH PLAYER
		// Get each buttons
		$each(box.getElements('a.button'), function(item) { Buttons(item); });
		// Check for close button
		if ($chk(box.getElement('a[target=close]'))) {
			box.getElement('a[target=close]').addEvent('click', function(e) {
				e.stop();
				shadow.destroy();
				box.destroy();
			});
		}
		// Check for action button
		if ($chk(box.getElement('a[target=action]'))) {
			box.getElement('a[target=action]').addEvent('click', function(e) {
				e.stop();
				var action = url.toString().split('.html');
				var newurl = action[0]+'/action.html'+action[1];
				new Request({url: newurl, onSuccess: function () { window.location = window.location; }}).send();
			});
		}
	},

	close: function () {
		new Fx.Tween($(document.body).getElement('div.lightbox-shadow'), {'property': 'opacity', onComplete: function() {$(document.body).getElement('div.lightbox-shadow').destroy();}}).start(0);
		$(document.body).getElement('div.lightbox').destroy();
	}

});

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