var NXCBox = new Class({
	
	Implements: [Options, Events],

	options:{
		overlayOpacity: 0.7,
		topPosition: 50
	},
		
	initialize: function( formBlockID, options ) {
		this.formBlockID = formBlockID;
		this.setOptions( options );
		this.prepareHTML();
		this.prepareEvents();
	},
	
	prepareHTML: function() {
		this.formBlock = $( this.formBlockID );
		
		this.overlay = new Element( 'div', {
			'class': 'nxcBoxOverlay',
			'styles': {
				'opacity': 0,
				'visibility': 'visible',
				'height': 0,
				'overflow': 'hidden'
			}
		} ).inject( $( document.body ) );
		this.overlay.get( 'tween' ).addEvent( 'onComplete', function() {
			if( this.overlay.getStyle( 'opacity' ) == this.options.overlayOpacity ) { 
				this.center.tween( 'opacity', 1 );
			} else if( this.overlay.getStyle( 'opacity' ) == 0 ) {
				this.overlay.setStyles( {
					'height': 0,
					'top': ''
				} );
			};
		}.bindWithEvent( this ) );
		window.addEvent( 'resize', function() {
			if( this.overlay.getStyle( 'opacity' ) != 0 ) {
				var scrollSize = $( window ).getScrollSize().y;
				var scrollTop = $( window ).getScroll().y;
				this.overlay.setStyles( {
					'height': scrollSize + scrollTop,
					'top': -scrollTop
				} );
			}
		}.bindWithEvent(this));		
		
		var formWidth  = this.formBlock.getStyle( 'width' ).toInt()
		var formHeight = this.formBlock.getStyle( 'height' ).toInt()
		this.center = new Element( 'div', {
			'class': 'nxcBoxCenter',
			'styles': {
				'width': formWidth + 2,
				'margin-left': -( formWidth / 2 ),
				'opacity': 0
			}
		} ).inject( $( document.body ) );
		this.formBlock.inject( this.center );		
	},
	
	prepareEvents: function() {
		this.formBlock.getElements( '.closeNXCBox' ).each( function( el ) {
			el.addEvent( 'click', function( e ) {
				e.stop();
				this.hide();
			}.bind( this ) );
		}.bind( this ) );
	},
	
	show: function( reciverEmail ) {
		this.formBlock.getElements( 'input.reciverEmail' )[0].set( 'value', reciverEmail );
		this.formBlock.getElements( 'div.contactform-form' )[0].setStyle( 'display', 'block' );
		this.formBlock.getElements( 'div.contactform-success' )[0].setStyle( 'display', 'none' );

		this.overlay.setStyles( {
			'top': -$( window ).getScroll().y,
			'height': $( window ).getScrollSize().y + $( window ).getScroll().y 
		} );
		this.center.setStyle( 'top', $( window ).getScroll().y + this.options.topPosition );
		
		this.overlay.tween( 'opacity', this.options.overlayOpacity );		
	},

	hide: function() {
		this.center.setStyle( 'opacity', 0 );
		this.overlay.tween( 'opacity', 0 );
	}
} );