var DU_AJAX_finder = new Class({

  Implements: [Chain, Options],

  options : {
		default_options: new Array(),
		script_base: null,
		script_url: null,
		force_refresh: true
	},

	initialize : function(element, options)
  {
    if ( this.form = $(element) ) {
			this.setOptions(options);

			this.changer = false;

			this._initializeFields();

			window.addEvent('domready', this._AJAXselect.bind(this, this.options.force_refresh));
		}; // endif
	},

	_initializeFields : function()
  {
		this.form.getElements('select').each(function(el_select) {
			if ( size = el_select.getSize() )
				el_select.defaultWidth = size.x.toInt();
			el_select.request = new Request.HTML({
				url: this.options.script_url+'?type='+el_select.get('name').replace("education",""),
				onRequest: function() {
					this.requesting = true;
					el_select.set("html", '<option>l&auml;dt...</option>').setStyle('width', el_select.defaultWidth);
				}.bind(this),
				onComplete: function(responseTree, responseElements, responseHTML, responseJavaScript) {
					this.requesting = false;
					el_select.set("html", responseHTML).setStyle('width', el_select.defaultWidth);
					this.callChain();
					//this._initializeFields();
				}.bind(this)
			});
			el_select.addEvent('change', function() {
				this.changer = $(el_select);
				if ( $('education_location') === $(el_select) )
					this._individualField(el_select);
				this._AJAXselect();
			}.bind(this)); // addEvent
		}.bind(this));
	},

	_individualField : function( el_select )
	{
		var selected = el_select.getSelected();
		if ( selected.length > 0 && 21 === selected[0].getProperty('value').toInt() ) {
			$('education_kind').setStyle('display', "none");

			if ( el_input = $('education_location_free-text') ) {
				$('education_location_free-text').getParent().setStyle('display', "");
			}
			else {
				var el_container = $(new Element('p', {'style': "position:relative;width:245px;"})).inject($('education_location'), 'after');
				var el_input = $(new Element('input', {
					'class': "text validate['required']",
					'id': "education_location_free-text",
					'name': "education[location_free-text]"
				})).inject(el_container, 'bottom');
				var el_label = $(new Element('label', {
					'class': "overlay-label",
					'for': "education_location_free-text",
					'text': "gewünschter Schulungsort"
				})).inject(el_container, 'top');
			}; // endif

			if ( btn_submit = this.form.getElement('input[type=submit]') ) {
				if ( ! $defined(btn_submit.title_default) )
					btn_submit.title_default = btn_submit.get('value');
				if ( ! $defined(this.form.action_default) )
					this.form.action_default = this.form.getProperty('action');

				btn_submit.set('value', "Anfrage");
				this.form.setProperty('action', this.form.action_default + "?request");
			}; // endif

			if ( $defined(ZS) )
				ZS.init_overlay_label.delay(1); // delay fix for IE6
		}
		else {
			if ( $('education_kind') )
				$('education_kind').setStyle('display', "");
			if ( $('education_location_free-text') )
				$('education_location_free-text').set('value', "").getParent().setStyle('display', "none");
			if ( btn_submit = this.form.getElement('input[type=submit]') ) {
				if ( $defined(btn_submit.title_default) )
					btn_submit.set('value', btn_submit.title_default);
			}; // endif
			if ( $defined(this.form.action_default) )
				this.form.setProperty('action', this.form.action_default);
		}; // endif
	}.bind(this),

	_AJAXselect : function( force )
	{
		if ( false === this.changer ) {
			if ( force ) {
				this.changer = this.form.getElements('select')[0];
			}
			else {
				this.form.getElements('select').each(function(el_select, i) {
					var selected = el_select.getSelected();
					if ( selected.length > 0 && ( value = selected[0].get('value') ) )
						this.changer = el_select;
			}.bind(this)); // each
			}; // endif
		}

		if ( $(this.changer) ) {
			this.changeMode = false;
			this.form.getElements('select').each(function(el_select, i) {
				if ( !this.changeMode && el_select == $(this.changer) )
					this.changeMode = true;
				if ( this.changeMode && el_select != $(this.changer) ) {
					el_select.request.options.url = this.options.script_url+'?type='+el_select.get('name').replace("education","")+'&type_no='+i;
					this.chain(function(){el_select.request.post(this.form)});
				}; // endif
			}.bind(this)); // each
			this.callChain();
		}; // endif
	}

});