/*
	Slimbox v1.3 - 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 LightboxForData = {

	init: function( options )
	{
		this.options = Object.extend(
			{
				resizeDuration: 400,
				resizeTransition: Fx.Transitions.sineInOut,
				initialWidth: 250,
				initialHeight: 250,
				animateCaption: true
			}, options || {} );
		this.eventKeyDown = this.keyboardListener.bindAsEventListener( this );
		this.eventPosition = this.position.bind( this );
		this.overlay = new Element( 'div' ).setProperty( 'id', 'lbOverlayData' ).injectInside( document.body );
		this.center = new Element( 'div' ).setProperty( 'id', 'lbCenterData' ).setStyles( { width: this.options.initialWidth+'px', height: this.options.initialHeight+'px', marginLeft: '-'+( this.options.initialWidth/2 )+'px', display: 'none' } ).injectInside( document.body );
		var nextEffect = this.nextEffect.bind( this );
		this.fx = {
			overlayOn: this.overlay.effect( 'opacity', { duration: 600, onComplete: nextEffect } ).hide(),
			resizeY: this.center.effects( { duration: this.options.resizeDuration, transition: this.options.resizeTransition, onComplete: nextEffect } ),
			resizeX: this.center.effects( { duration: this.options.resizeDuration, transition: this.options.resizeTransition, onComplete: nextEffect } ),
			overlayOff: this.overlay.effect( 'opacity', { duration: 300 } ).hide()
		};
	},

	show: function( url, title )
	{
		return this.open( [[url, title]], 0 );
	},

	open: function( urlAndTitle, divNum )
	{
		this.dataDivs = urlAndTitle;
		//this.center.setStyles({width: this.options.initialWidth+'px', height: this.options.initialHeight+'px', marginLeft: '-'+(this.options.initialWidth/2)+'px', display: 'none'});
		this.position();
		this.setup( true );
		this.top = window.getScrollTop()+( window.getHeight()/15 );
		this.center.setStyles( { top: this.top+'px', display: '' } );
		this.fx.overlayOn.start( 0.8 );
		this.center.className = 'lbLoadingData';
		//this.dataDiv = $( this.dataDivs[0][1] ).injectInside( this.center );
		this.nextEffect();
		manageFlash( 0 );
		manageSelectsAll( 'hidden' );
		slimboxVis = 1;
		return false;
	},

	position: function()
	{
		this.overlay.setStyles( { top: window.getScrollTop()+'px', height: window.getHeight()+'px' } );
	},

	setup: function( open )
	{
		var elements = $A( document.getElementsByTagName( 'object' ) );
		elements.each( function( el ){ el.style.visibility = open ? 'hidden' : ''; } );
		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: this.close(); break;
		}
	},

	nextEffect: function()
	{
		switch( this.step++ )
		{
			case 1:
				this.center.className = '';
				this.dataDiv = $( this.dataDivs[0][1] ).injectInside( this.center );
				this.dataDiv.style.display = 'block';
				//alert(this.dataDiv.offsetHeight);
				if( this.center.clientHeight != this.dataDiv.offsetHeight )
				{
					this.fx.resizeY.start( { height: this.dataDiv.offsetHeight } );
					break;
				}
				this.step++;
			case 2:
				if( this.center.clientWidth != this.dataDiv.offsetWidth )
				{
					this.fx.resizeX.start( { width: this.dataDiv.offsetWidth, marginLeft: -this.dataDiv.offsetWidth/2 } );
					break;
				}
				this.step++;
			case 3:
				for( var f in this.fx ) this.fx[f].stop();
				this.step = 0;
				//alert( $( 'productOrderCodeScrollDivIn' ).offsetHeight );
				break;
		}
	},

	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 = 'none';
		$( this.dataDivs[0][1] ).empty();
		this.fx.overlayOff.chain( this.setup.pass( false, this ) ).start( 0 );
		slimboxVis = 0;
		flashesAreManaged = setTimeout( 'manageFlash( 1 )', 1000 );
		timeOutSelect2 = setTimeout( 'manageSelectsAll( "visible" )', 1000 );
		return false;
	}
};

window.addEvent( 'domready', LightboxForData.init.bind( LightboxForData ) );
