var formLoader = new Class( {
	
	options: {
		firstHeight: 10,
		firstMargin: {
			top: 1,
			bottom: 2
		},
		secondHeight: 56,
		unit: 'em',
		firstDuration: 1000,
		secondDuration: 1000,
		loaderClass: 'formLoader_loader',
		specific: {
			tradesman: {
				secondHeight: 46
			},
			commercial: {
				secondHeight: 13
			},
			liability: {
				secondHeight: 13
			},
			leisure: {
				secondHeight: 13
			}
		}
	},
	
	initialize: function ( holder, buttons, options ) {
		this.assetsLoaded = false;
		
		this.setOptions( options );
		this.holder = $(holder);
		this.buttons = $A( buttons );
		
		this.scrollEffect = new Fx.Scroll( window, { duration: this.options.secondDuration } );
		this.secondEffect = this.holder.effect( 'height', { transition: Fx.Transitions.Circ.easeInOut, duration: this.options.secondDuration, unit: this.options.unit, onComplete: this.nextStage.bind( this ) } );
		
		this.mainItems = $('main_items');
		this.contentToHide = $('content_to_hide');
		
		objs = [];
		this.contentToHideParamsHide = {};
		this.contentToHideParamsShow = {};
		i = 0;
		if( this.contentToHide ) {
			objs.push( this.contentToHide );
			this.contentToHide.originalHeight = this.contentToHide.getSize().size.y;
			this.contentToHideParamsHide[ ''+i+'' ] = { 'height': [ this.contentToHide.originalHeight / 10, 0 ] };
			this.contentToHideParamsShow[ ''+i+'' ] = { 'height': [ 0, this.contentToHide.originalHeight / 10 ] };
			i++;
		}
		if( this.mainItems ) {
			objs.push( this.mainItems );
			this.contentToHideParamsHide[ ''+i+'' ] = { 'opacity': [ 1, 0 ] };
			this.contentToHideParamsShow[ ''+i+'' ] = { 'opacity': [ 0, 1 ] };
		}
		
		if( objs.length > 0 )
			this.hideContentEffect = new Fx.Elements( objs, { transition: Fx.Transitions.Circ.easeInOut, duration: this.options.secondDuration, unit: this.options.unit, onComplete: this.nextStage.bind( this ) } );
		else
			this.hideContentEffect = false;
		
		//this.hideContentEffect = this.contentToHide.effect( 'height', { transition: Fx.Transitions.Circ.easeInOut, duration: this.options.secondDuration, unit: this.options.unit, onComplete: this.nextStage.bind( this ) } );
		//this.mainItemsEffect = this.mainItems.effect( 'opacity', { transition: Fx.Transitions.Circ.easeInOut, duration: 300, unit: this.options.unit, onComplete: this.nextStage.bind( this ) } );
		
		this.overlay = new Element( 'div' ).addClass( this.options.loaderClass ).setStyles( {
			'position': 'absolute',
			'z-index': 2000
		} );
		
		this.firstEffect = new Fx.Elements( [this.holder, this.overlay], { transition: Fx.Transitions.Circ.easeInOut, duration: this.options.firstDuration, unit: this.options.unit, onComplete: this.nextStage.bind( this ) } );
		
		this.buttons.each( function ( obj ) {
			obj.addEvent( 'click', this.load.bind( this, obj.title ) );
			obj.removeAttribute( 'title' );
		}, this );
	},
	
	nextStage: function () {
		//this.addMessage( this.stageSet + ': ' + this.stage );
		this.stage++;
		this[this.stageSet + 'Step' + (this.stage-1)].delay( 20, this );
	},
	
// BEGIN: LOAD SECTION
	
	load: function( type ) {
		this.holder.empty();
		this.holder.setHTML( '' );
		this.type = type;
		this.jsToExec = '';
		
		var dims = this.holder.getCoordinates();
		this.overlay.setStyles( {
			'width': dims.width,
			'top': dims.top,
			'left':  dims.left,
			'display': 'block',
			'height': '0em'
		} ).injectInside( document.body );
		
		this.stageSet = 'load';
		this.stage = 0;
		
		this.nextStage();
	},
	
	loadStep0: function () {
		if( this.contentToHide ) {
			this.contentToHide.originalHeight = this.contentToHide.getSize().size.y;
			this.contentToHide.setStyles( { 'overflow': 'hidden', 'height': this.contentToHide.originalHeight + 'px', 'position': 'relative' } );
		}
		if( this.hideContentEffect )
			this.hideContentEffect.start( this.contentToHideParamsHide );
		else
			this.nextStage();
	},
	
	loadStep1: function () {
		this.firstEffect.start( {
			'0': {
				'height': this.options.firstHeight,
				'margin-top': this.options.firstMargin.top,
				'margin-bottom': this.options.firstMargin.bottom
			},
			'1': {
				'height': this.options.firstHeight + this.options.firstMargin.top + this.options.firstMargin.bottom
			}
		} );
	},
	
	loadStep2: function () {
		//this.addMessage( 'loadStep2: begin' );
		new Ajax( '/quoteForm.php', {
			method: 'post',
			data: {
				'type': this.type
			},
			onComplete: this.handleResult.bind( this ),
			onFailure: function ( response ) {
				alert( "error\n" + response.responseText );
			}
		} ).request();
	},
	
	loadStep3: function () {
		this.overlay.setStyle( 'display', 'none' );
		if( $defined( this.options.specific[this.type] ) ) {
			if( $defined( this.options.specific[this.type].secondHeight ) )
				this.secondEffect.start( this.options.specific[this.type].secondHeight );
			else
				this.secondEffect.start( this.options.secondHeight );
		}
		else
			this.secondEffect.start( this.options.secondHeight );
	},
	
	loadStep4: function () {
		this.scroll();
	},
	
	loadStep5: function () {},

// END: LOAD SECTION

// BEGIN: HIDE SECTION

	hide: function () {
		this.stageSet = 'hide';
		this.stage = 0;
		
		this.nextStage();
	},
	
	hideStep0: function () {
		this.firstEffect.start( {
			'0': {
				'height': 0,
				'margin-top': 0,
				'margin-bottom': 0
			},
			'1': {
				'height': 0
			}
		} );
	},
	
	hideStep1: function () {
		if( this.hideContentEffect )
			this.hideContentEffect.start( this.contentToHideParamsShow );
		else
			this.nextStage();
	},
	
	hideStep2: function () {
		if( this.contentToHide ) {
			this.contentToHide.setStyles( { 'overflow': 'visible', 'height': 'auto', 'position': 'static' } );
		}
	},
	
	hideStep3: function () {
		
	},

// END: HIDE SECTION

	handleResult: function ( response ) {
		this.response = Json.evaluate( response );
		if( response.length > 0 ) {
			this.responseCounter = 0;
			this.handleResultStep();
		}
		else
			this.nextStage();
	},
	
	loadJS: function () {
		//this.addMessage( 'loadJS: ' + this.jsItems[this.jsStep] );
		if( this.jsStep >= this.jsItems.length )
			this.handleResultStep();
		else {
			new Ajax( this.jsItems[this.jsStep], {
					method: 'get',
					onComplete: function( res ) {
						this.jsToExec += res;
						this.jsStep++;
						this.loadJS();
					}.bind( this ),
					onFailure: function() {
						this.jsToExec += '';
						this.jsStep++;
						this.loadJS();
					}.bind( this )
					//onFailure: this.addMessage.bind( this, this.jsItems[this.jsStep] + ' error' )
				}
			).request();
		}
	},
	
	handleResultStep: function () {
		var item = this.response[this.responseCounter];
		
		if( this.responseCounter < this.response.length ) {
			this.responseCounter++;
			switch( item.type ) {
				case 'images':
					if( item.value.length > 0 ) {
						new Asset.images( item.value, {
							onComplete: this.handleResultStep.bind( this )
						} );
					}
					else
						this.handleResultStep();
					break;
				case 'js':
					if( item.value.length > 0 ) {
						this.jsItems = item.value;
						this.jsStep = 0;
						this.loadJS();
					}
					//this.handleResultStep();
					break;
				case 'evaljs':
					if( item.value.length > 0 ) {
						this.jsItems = item.value;
						this.jsStep = 0;
						this.loadJS();
					}
					this.handleResultStep();
					break;
				case 'css':
					if( item.value.length > 0 ) {
						for( i=0; i<item.value.length; i++ ) {
							new Asset.css( item.value[i] );
						}
					}
					this.handleResultStep();
					break;
				case 'content':
					this.holder.setHTML( item.value );
					this.handleResultStep();
					break;
			}
		}
		else {
			window.setTimeout( this.jsToExec, 20 );
			this.nextStage.delay( 20, this );
		}
	},
	
	scroll: function () {
		this.scrollEffect.toElement( this.holder );
	},
	
	submit: function () {
		this.stageSet = 'submit';
		this.stage = 0;
		
		this.nextStage();
	},
	
	submitStep0: function () {
		this.overlay.setStyle( 'display', 'block' ).removeClass( this.options.loaderClass ).addClass( this.options.loaderClass + '_submit' );
		this.holder.setStyle( 'opacity', 0.1 );
		this.firstEffect.start( {
			'0': {
				'height': this.options.firstHeight,
				'margin-top': this.options.firstMargin.top,
				'margin-bottom': this.options.firstMargin.bottom
			},
			'1': {
				'height': this.options.firstHeight + this.options.firstMargin.top + this.options.firstMargin.bottom
			}
		} );
	},
	
	submitStep1: function () {
		document.forms['quoteForm'].send.delay( this.options.firstDuration, document.forms['quoteForm'], {
			onComplete: function () {
				this.overlay.setStyle( 'display', 'none' ).removeClass( this.options.loaderClass + '_submit' ).addClass( this.options.loaderClass );
				this.holder.setStyle( 'opacity', 1 );
				new Element( 'div' ).addClass( 'closeFormBtt' ).setStyles( {
					'margin': '1em 20em 0em 20em',
					'float': 'left',
					'clear': 'both'
				} ).addEvent( 'click', this.hide.bind( this ) ).setHTML( 'Go Back' ).injectInside( this.holder );
			}.bind( this ),
			update: this.holder
		} );
	},
	
	submitStep2: function () {
		
	}
	
} );

formLoader.implement( new Options );
formLoader.implement( new Debug );

var objFormLoader = Class.empty;

window.addEvent( 'load', function () {
	if( $('formHolder') )
		objFormLoader = new formLoader( $('formHolder'), $ES('.quote, .another_quote') );
} );
