/****************** MAIN OBJECT ********/
window.addEvent('domready', function() {
	browserEngine = new browserObj( );
} );

window.addEvent('unload', function() {
	browserEngine = null;
} );

function browserObj( )
{
	this.loops = new Array();
	
	this.initDocument();
	this.initControls(document);
	this.initActions(document);
	
	document.addEvent('updateblock', function(obj) {
			browserEngine.initControls(obj);
			browserEngine.initActions(obj);
		}
	);
}

browserObj.prototype.initDocument = function( )
{
	var thisObj = this;
	
	this.gallery = new Lightbox( );
	this.gallery.init();
	this.gallery.anchors = [];
	var gallery_imgs = $$('.gallery-img', document);
	for (var i=0; i<gallery_imgs.length; i++)
	{
		this.parseGallery( gallery_imgs[i] );
	}
}

browserObj.prototype.initControls = function( obj )
{
	var thisObj = this;
	
	var openWindows = $(obj).getElements('.open-window');
	for (var i=0; i<openWindows.length; i++)
	{
		openWindows[i].addEvent('click', function() {
			newWindow = window.open(this.href, 'window', "menubar=0,toolbar=0,location=0,status=0,scrollbars=0,width="+screen.width+",height=" + screen.height);
  			newWindow.moveTo(0,0);
		});
		openWindows[i].onclick = function() { return false; }
 	}
	
	var openPopups = $(obj).getElements('.open-popup');
	for (var i=0; i<openPopups.length; i++)
	{
		openPopups[i].addEvent('click', function() {
			//browserEngine.wait(1);
			new Ajax(this.getProperty('href'), {
			method: 'get',
			evalScripts: true,
			onComplete: browserEngine.startPopup
			}).request();
		});
		openPopups[i].onclick = function() { return false; }
 	}
}

browserObj.prototype.initActions = function( obj )
{
	var thisObj = this;
	
	var ghosts = $(obj).getElements('[class^=after-]');
	for (var p=0; p<ghosts.length; p++)
	{
		var css = ghosts[p].getProperty('class');
		ghosts[p].setProperty( 'class', css.replace('after-','') );
 	}
	
	/**** AJAX RELATED *****/	
	var elements = $(obj).getElements('.exec');
	for (var i=0; i<elements.length; i++)
	{
		thisObj.importRel( elements[i] );
		thisObj.parseExec( elements[i] );
	}
	
	var loops = $(obj).getElements('.slideshow');
	for (var i=0; i<loops.length; i++)
	{
		thisObj.importRel( loops[i] );
		thisObj.parseLoop( loops[i] );
	}
	
	var tips = $(obj).getElements('.tip');
	for (var i=0; i<tips.length; i++)
	{
		thisObj.importRel( tips[i] );
		thisObj.parseTip( tips[i] );
	}
	
	var confirms = $(obj).getElements('.confirm');
	for (var i=0; i<confirms.length; i++)
	{
		confirms[i].onclick = function() {
			return confirm('Sei sicuro di voler continuare');
		}
	}
	
	var updates = $(obj).getElements('.update');
	for (var v=0; v<updates.length; v++)
	{
		thisObj.importRel( updates[v] );
		if (updates[v].tagName == 'FORM')
			thisObj.updateFormDelay(updates[v]);
		if (updates[v].tagName == 'SELECT')
		{
			$(updates[v]).addEvent('change', function(event) {
				new Event(event).stop();
				browserEngine.feedSelect(this);
				return false;
			   	}
			);
		}
	}
	
	var saves = $(obj).getElements('.save');
	for (var v=0; v<saves.length; v++)
	{
		thisObj.importRel( saves[v] );
		saves[v].addEvent('submit', function(event) {
			new Event(event).stop();
			this.send( {
				evalScripts: true
			} );
			return false;
		} );
	}
	
	var loops = $(obj).getElements('.loop');
	for (var y=0; y<loops.length; y++)
	{
		thisObj.importRel( loops[y] );
		thisObj.startLoop( loops[y] );
	}
	
}

browserObj.prototype.importRel = function( obj )
{
	if (obj.getProperty("rel"))
	{
		// import rel settings
		var parts = obj.getProperty("rel").split('|');
		for (var j=0; j<parts.length; j++)
		{
			kv = parts[j].split(':');
			eval("obj."+kv[0]+" = '"+kv[1]+"'");
		}
	}
}

browserObj.prototype.controlFx = function( obj, transaction )
{
	if (obj.fxTransaction != transaction)
	{
		obj.fxTransaction = transaction;
		switch (transaction)
		{
			case 'fade':
				obj.fx = obj.effects({duration: 500});
			break;
			case 'slide':
				obj.fx = new Fx.Slide(obj, {mode: 'horizontal', duration: 800});
			break;
		}
	}
}

browserObj.prototype.startFx = function( obj, transaction, HTML )
{
	var thisObj = this;

	thisObj.controlFx( obj, transaction );
	switch (transaction)
	{
		case 'fade':
			obj.fx.start ({'opacity': 1}).chain(function() {
				this.start({'opacity': 0});
			}).chain(function() {
				this.element.setHTML( HTML );
				this.start ({'opacity': 1});
				document.fireEvent('updateblock', this.element);
			});
		break;
		case 'slide':
			obj.fx.slideOut().chain(function() {
				this.element.setHTML( HTML );
				this.slideIn();
				document.fireEvent('updateblock', this.element);
			});
		break;
	}
}


browserObj.prototype.parseExec = function( obj )
{
	obj.addEvent(obj.action, function(event) {
		new Event(event).stop();
		browserEngine.feedElement(this);
		return false;
		}
	);
}

browserObj.prototype.parseLoop = function( obj )
{
	var thisObj = this;
	
	obj.transaction = 'fade';
	thisObj.feedElement.periodical(10000, thisObj, obj);
}

browserObj.prototype.feedElement = function( obj )
{
	var thisObj = this;
	
	if (!obj)
		return;
			
	if (obj.get == 'same')
		var url = obj.href;
	else
		var url = obj.get;
	if (obj.tagName == 'SELECT')
		url += obj.options[obj.selectedIndex].value
	if ( (obj.tagName == 'A') && (obj.container) )
		thisObj.selectRow( obj );
	$(obj.targetEl).sourceEl = obj;
	$(obj.targetEl).setProperty('lasturl', url);
	if (obj.cachedHTML)
	{
		var func = thisObj.updateBlock.bind($(obj.targetEl));
		func( obj.cachedHTML );
	}
	else
	{
		thisObj.wait($(obj.targetEl), 1);
		new Ajax(url, {
			method: 'get',
			evalScripts: true,
			data: $(obj.targetEl),
			onComplete: browserEngine.updateBlock.bind($(obj.targetEl))//,
			//update: $(obj.targetEl)
		}).request();
	}
}

browserObj.prototype.feedSelect = function( obj )
{
	var thisObj = this;
	
	var url = obj.get
	url += obj.options[obj.selectedIndex].value
	url += '&ajax=true';
	//thisObj.wait(1);
	new Ajax(url, {
		method: 'get',
		evalScripts: true
	}).request();
}

browserObj.prototype.updateFormDelay = function( obj )
{
	var thisObj = this;
	
	var inputs = obj.getElementsBySelector('input,select,textarea')
	for (var i=0; i<inputs.length; i++)
	{
		inputs[i].formObj = obj;
		var func = function() { browserEngine.waitFunction( browserEngine.updateForm, this.formObj.delay, { 'obj': this.formObj } ); return false; }
		$(inputs[i]).addEvent('change', func);
		$(inputs[i]).addEvent('keypress', func);
	}
}

browserObj.prototype.updateForm = function( )
{
	var thisObj = browserEngine;
	
	var params = thisObj.waitFunctionParams;
	var obj = params.obj;
	var url = obj.get;
	if (url.indexOf('?') > -1) url += '&'; else url += '?';
	var queryString = [];
	obj.getElementsByClassName('formSave').each(function(el){
		var name = el.name;
		var value = el.getValue();
		if (value === false || !name || el.disabled) return;
		var qs = function(val){
			queryString.push(name + '=' + encodeURIComponent(val));
		};
		if ($type(value) == 'array') value.each(qs);
		else qs(value);
	});
	url += queryString.join('&');
	thisObj.wait($(obj.targetEl), 1);
	new Ajax(url, {
		method: 'get',
		data: $(obj.targetEl),
		onComplete: browserEngine.updateBlock,
		evalScripts: true
	}).request();
}

browserObj.prototype.updateBlock = function( response )
{
	if (this.options)
		var obj = this.options.data;
	else
		var obj = this;
	
	browserEngine.wait(obj, 0);
	browserEngine.importRel( obj );
	
	if (obj.sourceEl)
	if (obj.sourceEl.transaction)
	{
		browserEngine.startFx( obj, obj.sourceEl.transaction, response );
		if (obj.sourceEl.cache == 'true') obj.sourceEl.cachedHTML = response;
		return;
	}
	// default action
	obj.setHTML(response);
	if (obj.sourceEl)
	if (obj.sourceEl.cache)
	if (obj.sourceEl.cache == 'true') obj.sourceEl.cachedHTML = response;
	document.fireEvent('updateblock', obj);
}

browserObj.prototype.parseGallery = function( obj )
{
	//this.gallery.anchors.length = 0;
	if (obj.rel && obj.rel.test(/^flashbox/i)){
		obj.addEvent('click', function(event) {
			new Event(event).stop();
		} );
		obj.onclick = this.gallery.clickFlash.pass(obj, this.gallery);
	}
	
	if (obj.rel && obj.rel.test(/^lightbox/i)){
		obj.addEvent('click', function(event) {
			new Event(event).stop();
	} );
	obj.onclick = this.gallery.click.pass(obj, this.gallery);
	this.gallery.anchors.push(obj);
	}
}

browserObj.prototype.parseTip = function( obj )
{
	var pos = obj.getCoordinates();
	var size = obj.getSize().size;
	obj.tip = new Element('div', {'class': 'tooltip'})
	.setStyles({'left': pos.left - 30, 'top': pos.top - 170})
	.setStyles({'position':'absolute'})
	.setHTML('<span class="' + obj.type + '">'+obj.title+'</span>')
	.inject(document.body);
	obj.tip.fx = new Fx.Style(obj.tip, 'opacity', { duration: 200, wait: false }).set(0);
	obj.addEvent('mouseenter', function(e, obj) {
		obj.tip.fx.start(1);
	}.bindWithEvent(this, obj) );
	obj.addEvent('mouseleave', function(e, obj) {
		obj.tip.fx.start(0);
	}.bindWithEvent(this, obj) );
}

browserObj.prototype.refreshPlot = function( )
{
	var thisObj = this;
	
	thisObj.waitFunction( browserEngine.updateForm, 10, { 'obj': $('form-plot') } );	
}


browserObj.prototype.startPopup = function( response )
{
	var thisObj = browserEngine;
	
	thisObj.openPopup( response );
	//thisObj.wait(0);
}

browserObj.prototype.startLoop = function( obj )
{
	var thisObj = this;
	
	var indexLoop = thisObj.loops.length;
	var items = obj.getElementsByClassName('exec');
	thisObj.loops[indexLoop] = { 
		'obj': obj,
		'items': items,
		'interval': window.setInterval( "browserEngine.stepLoop("+indexLoop+")", obj.timer ),
		'loopIndex': obj.startIndex || 0,
		'transaction': obj.transaction
		};
	for (var i=0; i<items.length; i++)
	{
		if (obj.cache == 'true')
			items[i].cache = 'true';
		items[i].indexLoop = indexLoop;
		items[i].addEvent(items[i].action, function(event) {
			new Event(event).stop();
			browserEngine.stopLoop(this.indexLoop);
			return false;
			}
		);	
	}
}

browserObj.prototype.stepLoop = function( indexLoop )
{
	var thisObj = this;
	
	var _item = thisObj.loops[indexLoop].items[thisObj.loops[indexLoop].loopIndex];
	thisObj.feedElement(_item);
	if (thisObj.loops[indexLoop].loopIndex >= (thisObj.loops[indexLoop].items.length - 1) )
		thisObj.loops[indexLoop].loopIndex = 0;
	else
		thisObj.loops[indexLoop].loopIndex++;
}

browserObj.prototype.stopLoop = function( indexLoop )
{
	var thisObj = this;
	window.clearInterval( thisObj.loops[indexLoop].interval );
}

browserObj.prototype.selectRow = function( obj )
{
	var container = $(obj.container);
	var items = container.getElementsByClassName('selectable');
	for (var i=0; i<items.length; i++)
	{
		items[i].removeClass('row-selected');
	}
	obj.addClass('row-selected');
}

browserObj.prototype.waitFunction = function( func, delay, params )
{
	var thisObj = this;
	
	if (thisObj.waitFunctionTimer)
		clearTimeout(thisObj.waitFunctionTimer);
	thisObj.waitFunctionParams = params;
	thisObj.waitFunctionTimer = window.setTimeout(func, delay);
}

/********** POPUP ******************/
browserObj.prototype.openPopup = function( HTML )
{
	var thisObj = this;
	
	if (!thisObj.popup)
	{
		// construct popup layer
		thisObj.popup = new Element('div', {'id': 'popup'} ).injectTop($E('body'));
		thisObj.popupContainer = new Element('div').injectTop(thisObj.popup);
		thisObj.popup.setStyles( {'position': 'absolute', 'zIndex': 1000} );
		thisObj.popup.fx = new Fx.Style(thisObj.popup, 'opacity').set(0);
	}
	thisObj.obscure('all');
	thisObj.popupContainer.setHTML( HTML );
	thisObj.initControls( thisObj.popup );
	thisObj.initActions( thisObj.popup );
	thisObj.putClose( );
	thisObj.centerElement( thisObj.popup );
	thisObj.popup.setStyle('display', 'block');
	thisObj.popup.fx.start(0,1);
}

browserObj.prototype.centerElement = function( popup )
{
	var popupPos = popup.getCoordinates();
	var win = {'x': window.getWidth(), 'y': window.getHeight()};
	var scroll = {'x': window.getScrollLeft(), 'y': window.getScrollTop()};
	popupPos.left = ( win.x - popupPos.width ) / 2 + scroll.x;
	popupPos.top = ( win.y - popupPos.height ) / 2 + scroll.y ;
	if (popupPos.left < 0)	popupPos.left = 0;
	if (popupPos.top < 0)	popupPos.top = 0;
	popup.setStyle('left', popupPos.left);
	popup.setStyle('top', popupPos.top);
}


browserObj.prototype.putClose = function( )
{
	var thisObj = this;
	
	var popupPos = thisObj.popup.getCoordinates();
	if (!thisObj.popup.closeEl)
	{
		thisObj.popup.closeEl = new Element('a', {'class':'close'}).setHTML('Chiudi');
		thisObj.popup.closeEl.setProperty('title', 'Chiudi');
		thisObj.popup.closeEl.setProperty('href', '#');
		thisObj.popup.closeEl.toClose = thisObj.popup;
		thisObj.popup.closeEl.addEvent("click", function() 
		{
			this.toClose.fx.start(1,0).chain( function() { browserEngine.obscure('all'); browserEngine.popup.remove(); browserEngine.popup = null; } );
		});
		thisObj.popup.closeEl.onclick = function() { return false; }
		thisObj.popup.closeEl.injectTop( thisObj.popup );
	}
	//thisObj.popup.closeEl.setStyle('margin-left', popupPos.width - thisObj.popup.closeEl.getCoordinates().width);
	//thisObj.popup.closeEl.setStyle('margin-top', thisObj.popup.closeEl.getCoordinates().height );
}


browserObj.prototype.obscure = function( obj )
{
	var thisObj = this;
	
	if (!thisObj.waitDiv)
	{
		// construct wait layer
		thisObj.waitDiv = new Element('div').injectTop(document.body);
		thisObj.waitDiv.setStyles( {'position': 'absolute', 'background': '#000', 'z-index': 999} );
		thisObj.waitDiv.fx = new Fx.Style(thisObj.waitDiv, 'opacity').set(0);	
	}
	
	if (thisObj.waitLayerOpen)
	{
		thisObj.waitDiv.fx = new Fx.Style(thisObj.waitDiv, 'opacity').start(.7,0);
		thisObj.waitLayerOpen = false;
		if (window.ie6) $E('body').removeClass('obscured');
	}
	else
	{
		if (obj == 'all')
			var objPos = {'top': 0, 'left': 0, 'width': window.getScrollWidth(), 'height': window.getScrollHeight()};
		else
			var objPos = obj.getCoordinates();
		thisObj.waitDiv.setStyle('width', objPos.width);
		thisObj.waitDiv.setStyle('height', objPos.height);
		thisObj.waitDiv.setStyle('left', objPos.left);
		thisObj.waitDiv.setStyle('top', objPos.top);
		thisObj.waitDiv.fx = new Fx.Style(thisObj.waitDiv, 'opacity').start(0,.7);
		thisObj.waitLayerOpen = true;
		if (window.ie6) $E('body').addClass('obscured');
	}
}

browserObj.prototype.wait = function( obj, status )
{
	var thisObj = this;
	
	if (status > 0)
	{
		obj.addClass('wait');
	}
	else
	{
		obj.removeClass('wait');
	}
}


/******************* GALLERY *****************/

/*
	Slimbox v1.41 - The ultimate lightweight Lightbox clone
	by Christophe Beyls (http://www.digitalia.be) - MIT-style license.
	Inspired by the original Lightbox v2 by Lokesh Dhakar.
*/

var Lightbox = new Class({

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

		this.anchors = [];
		$each(document.links, function(el){
			if (el.rel && el.rel.test(/^lightbox/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.image = new Element('div', {'id': 'lbImage'}).injectInside(this.center);
		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(document.body);
		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.center.effects($extend({duration: this.options.resizeDuration, onComplete: nextEffect}, this.options.resizeTransition ? {transition: this.options.resizeTransition} : {})),
			image: this.image.effect('opacity', {duration: 500, onComplete: nextEffect}),
			bottom: this.bottom.effect('margin-top', {duration: 400, onComplete: nextEffect})
		};

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

	click: function(link){
		if (link.rel.length == 8) return this.show(link.href, link.title);

		var j, imageNum, images = [];
		this.anchors.each(function(el){
			if (el.rel == link.rel){
				for (j = 0; j < images.length; j++) if(images[j][0] == el.href) break;
				if (j == images.length){
					images.push([el.href, el.title]);
					if (el.href == link.href) imageNum = j;
				}
			}
		}, this);
		return this.open(images, imageNum);
	},
	
	clickFlash: function(link){
		this.position();
		this.setup(true);
		this.top = window.getScrollTop() + (window.getHeight() / 15);
		this.center.setStyles({top: this.top, display: ''});
		this.fx.overlay.start(0.8);
		this.fx.image.hide();
		
		var w = 800;
		var h = 400;
		var html = "<object classid=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\""
    	+ "codebase=\"http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0\""
   		+ "width=\""+w+"\" height=\""+h+"\">"
     	+ "<param name='movie' value='"+link.href+"' />\n"
     	+ "<param name='quality' value='high' /><param name='wmode' value='transparent' />\n"
      	+ "<embed src=\""+link.href+"\" quality=\"high\" wmode=\"transparent\" pluginspage=\"http://www.macromedia.com/go/getflashplayer\" type=\"application/x-shockwave-flash\" width=\""+w+"\" height=\""+h+"\"></embed>"
      	+ "</object>";
		this.image.style.width = this.bottom.style.width = w+'px';
		this.image.style.height = h+'px';
		this.center.setStyles( { width: w+20, height: h+20, marginLeft: -(w+20)/2 } );
		this.image.setHTML( html );
		this.bottomContainer.setStyles({top: this.top + this.center.clientHeight, height: '', marginLeft: this.center.style.marginLeft, marginTop: 0, display: 'block'});
		this.fx.image.start(1);
		
		//this.step = 3;
		//this.nextEffect();
	},

	show: function(url, title){
		return this.open([[url, title]], 0);
	},

	open: function(images, imageNum){
		this.images = images;
		this.position();
		this.setup(true);
		this.top = window.getScrollTop() + (window.getHeight() / 15);
		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;
	},

	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){
		if (this.step || (imageNum < 0) || (imageNum >= this.images.length)) return false;
		this.step = 1;
		this.activeImage = imageNum;

		this.bottomContainer.style.display = this.prevLink.style.display = this.nextLink.style.display = 'none';
		this.fx.image.hide();
		this.center.className = 'lbLoading';

		this.preload = new Image();
		this.preload.onload = this.nextEffect.bind(this);
		this.preload.src = this.images[imageNum][0];
		return false;
	},

	nextEffect: function(){
		switch (this.step++){
		case 1:
			this.center.className = '';
			this.image.style.backgroundImage = 'url('+this.images[this.activeImage][0]+')';
			this.image.style.width = this.bottom.style.width = this.preload.width+'px';
			this.image.style.height = this.prevLink.style.height = this.nextLink.style.height = this.preload.height+'px';

			this.caption.setHTML(this.images[this.activeImage][1] || '');
			this.number.setHTML((!this.options.showCounter || (this.images.length == 1)) ? '' : 'Image '+(this.activeImage+1)+' of '+this.images.length);

			if (this.activeImage) this.preloadPrev.src = this.images[this.activeImage-1][0];
			if (this.activeImage != (this.images.length - 1)) this.preloadNext.src = this.images[this.activeImage+1][0];
			if (this.center.clientHeight != this.image.offsetHeight){
				this.fx.resize.start({height: this.image.offsetHeight});
				break;
			}
			this.step++;
		case 2:
			if (this.center.clientWidth != this.image.offsetWidth){
				this.fx.resize.start({width: this.image.offsetWidth, marginLeft: -this.image.offsetWidth/2});
				break;
			}
			this.step++;
		case 3:
			this.bottomContainer.setStyles({top: this.top + this.center.clientHeight, height: 0, marginLeft: this.center.style.marginLeft, display: ''});
			this.fx.image.start(1);
			break;
		case 4:
			if (this.options.animateCaption){
				this.fx.bottom.set(-this.bottom.offsetHeight);
				this.bottomContainer.style.height = '';
				this.fx.bottom.start(0);
				break;
			}
			this.bottomContainer.style.height = '';
		case 5:
			if (this.activeImage) this.prevLink.style.display = '';
			if (this.activeImage != (this.images.length - 1)) this.nextLink.style.display = '';
			this.step = 0;
		}
	},

	close: function(){
		if (this.step < 0) return;
		this.step = -1;
		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;
	}
});

Lightbox.implement(new Events);
Lightbox.implement(new Options);
