var CaseHistory = {

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

		this.anchors = [];
		this.eventKeyDown = this.keyboardListener.bindAsEventListener( this );
		this.eventPosition = this.position.bind( this );

		this.overlay = new Element( 'div', { 'id': 'chOverlay' } ).injectInside( document.body );

		this.shadow1 = new Element( 'div', { 'id': 'chShadow1',
											'styles': { 'width': this.options.initialWidth,
														'height': this.options.initialHeight,
														'marginLeft': -( this.options.initialWidth / 2 - 6 ),
														'display': 'none' } } ).injectInside( document.body );
		this.shadow2 = new Element( 'div', { 'id': 'chShadow2',
											'styles': { 'width': this.options.initialWidth,
														'height': this.options.initialHeight,
														'marginLeft': -( this.options.initialWidth / 2 - 4 ),
														'display': 'none' } } ).injectInside( document.body );
		this.shadow3 = new Element( 'div', { 'id': 'chShadow3',
											'styles': { 'width': this.options.initialWidth,
														'height': this.options.initialHeight,
														'marginLeft': -( this.options.initialWidth / 2 - 2 ),
														'display': 'none' } } ).injectInside( document.body );
		this.center = new Element( 'div', { 'id': 'chCenter',
											'styles': { 'width': this.options.initialWidth,
														'height': this.options.initialHeight,
														'marginLeft': -( this.options.initialWidth / 2 ),
														'display': 'none' } } ).injectInside( document.body );
		this.frame = new Element( 'iframe', { 'id': 'chFrame',
											  'styles': { 'width': this.options.initialWidth - 8,
														  'height': this.options.initialHeight - 8 },
											  'src': 'loading.html'
											} ).injectInside( this.center );

		this.closebtn = new Element( 'a', { 'id': 'chCloseLink',
											'styles': { 'marginLeft': -( this.options.initialWidth / 2 + 12 ),
											            'display': 'none' }, 
		                                    'href': '#' } ).injectInside( document.body );
		this.closebtn.onclick = this.overlay.onclick = this.close.bind( this );

		var nextEffect = this.nextEffect.bind( this );
		this.fx = {
			overlay: this.overlay.effect( 'opacity', { duration: 500 } ).hide(),
			shadow1: this.shadow1.effect( 'opacity', { duration: 500 } ).hide(),
			shadow2: this.shadow2.effect( 'opacity', { duration: 500 } ).hide(),
			shadow3: this.shadow3.effect( 'opacity', { duration: 500 } ).hide(),
			resize: this.center.effects( $extend( { duration: this.options.resizeDuration, onComplete: nextEffect }, this.options.resizeTransition ? { transition: this.options.resizeTransition } : {} ) ),
			frame: this.frame.effect( 'opacity', { duration: 500, onComplete: nextEffect } )
		};

	},

	show: function( url ) {
		return this.open( url );
	},

	open: function( url ) {
		this.position();
		this.setup( true );
		this.top = window.getScrollTop() + ( window.getHeight() / 7 );
		this.shadow1.setStyles( { top: this.top + 6, display: '' } );
		this.shadow2.setStyles( { top: this.top + 4, display: '' } );
		this.shadow3.setStyles( { top: this.top + 2, display: '' } );
		this.center.setStyles( { top: this.top, display: '' } );
		this.closebtn.setStyles( { top: this.top - 16,
		                           display: '' } );
		this.fx.overlay.start( 0.8 );
		this.fx.shadow1.start( 0.1 );
		this.fx.shadow2.start( 0.1 );
		this.fx.shadow3.start( 0.1 );
		return this.changeURL( url );
	},

	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.chBackupStyle = el.style.visibility;
			el.style.visibility = open ? 'hidden' : el.chBackupStyle;
		} );
		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();
		}
	},

	changeURL: function( url ){
		if( this.step || url.length == 0 ) return false;
		this.step = 1;

		this.frame.src = url;
		return false;
	},

	nextEffect: function() {
		switch( this.step++ ) {
		case 1:
			this.center.className = '';

			if( this.center.clientHeight != this.frame.offsetHeight ) {
				this.fx.resize.start( { height: this.frame.offsetHeight } );
				break;
			}
			this.step++;
		case 2:
			if( this.center.clientWidth != this.frame.offsetWidth ) {
				this.fx.resize.start( { width: this.frame.offsetWidth, marginLeft: -this.frame.offsetWidth / 2 } );
				break;
			}
			this.step = 0;
		}
	},

	close: function() {
		if( this.step < 0 ) return;
		this.step = -1;
		for( var f in this.fx ) this.fx[ f ].stop();
		this.center.style.display = this.closebtn.style.display = this.shadow1.style.display = this.shadow2.style.display = this.shadow3.style.display = 'none';
		this.fx.overlay.chain( this.setup.pass( false, this ) ).start( 0 );
		return false;
	}
};

function openCH( object, url ) {
	object.open( url );
}

window.addEvent( 'domready', CaseHistory.init.bind( CaseHistory ) );
