var Form3 = Class.create(Dialog, {
	
	defaultOptions: {
		close: {overlay: false, esc: true, link: true},
		width: 700
	},

	ajaxOptions: {
		requestHeaders: {
			'X-Target': 'popup'
		}
	},

	initialize: function($super, options) {

		Object.extend(this.defaultOptions, options || {});
		if (typeof this.defaultOptions.ajax.options == 'object')
			Object.extend(this.defaultOptions.ajax.options, this.ajaxOptions);
		else
			this.defaultOptions.ajax.options = this.ajaxOptions;
		$super(this.defaultOptions);
	}
});


var Form2 = Class.create(Dialog, {
	initialize: function($super, options) {
		this._saved = false;

		options.close = {overlay: false, esc: true, link: true}
		if (!options.width)
			options.width = 700;
		options.height = 'max';
		
		$super(options);
	},
	setClickedElement: function($super, element)
	{
		this.id = element.rel;
		$super('setClickedElement', element);
	}, 
	setAjax: function()
	{
		this.ajaxurl = this.opt.ajax.url;
		if (this.id)
			this.ajaxurl += '/' + this.id;
		var o = this.prepareAjax();

		var oldFunction = o.onComplete;
		var a = function(t)
		{
			oldFunction(t);
			this.attachFormEvents();
			document.fire('dom:loaded');
		}.bind(this)
		Object.extend(o, {onComplete: a});

		new Ajax.Request(this.ajaxurl, o)
	},
	attachFormEvents: function($super)
	{
		this.form = Dialogs.elm('content').select('form')[0];
		this.saveButton.observe('click', function(){
			ajax_submitForm(this.form, function(response){
				if (response.res)
				{
					this._saved = true;
					this.close();
				}
			}.bind(this));
		}.bindAsEventListener(this));
	},
	setContent: function($super)
	{
		$super('setContent');

		var b = Dialogs.elm('bottom');
		b.update('').insert(
			this.saveButton = new Element('input', {type: 'button', 'class': 'button save', value: 'Mentés'})
		).insert(
			this.cancelButton = new Element('input', {type: 'button', 'class': 'button', value: 'Mégse'})
		);
		this.cancelButton.setStyle({margin: '0 0 0 5px'});
		this.cancelButton.observe('click', this.close.bindAsEventListener(this));
		if (!b.visible())
		{
			b.childElements().invoke('show');
			b.show();
		}
		this.setDimensions.bind(this).defer();
	},
	
	close: function($super) 
	{
		if (this._saved || confirm('Valóban mentés nélkül bezárja az ablakot?'))
			$super('close');
	}
});


var MegbizoCegForm = Class.create(Form2, {
	initialize: function($super, options) {
		var childOptions = {
			width: 900,
			title: (options.id) ? 'Megbízó cég szerkesztése' : 'Megbízó cég felvitele',
			ajax: {url:'/megbizoceg/form'}
		}
		Object.extend(options || { }, childOptions);
		$super(Object.extend(options));
	}
});


var IrodaTipusForm = Class.create(Form2, {
	initialize: function($super, options) {
		var childOptions = {
			title: (options.id) ? 'Irodatípus szerkesztése' : 'Irodatípus felvitele',
			ajax: {url:'/irodatipus/form'}
		}
		Object.extend(options || { }, childOptions);
		$super(Object.extend(options));
	}
});

var LakastipusForm = Class.create(Form2, {
	initialize: function($super, options) {
		var childOptions = {
			width: 900,
			title: (options.id) ? 'Lakástípus szerkesztése' : 'Lakástípus felvitele',
			ajax: {url:'/lakastipus/form'}
		}
		Object.extend(options || { }, childOptions);
		$super(Object.extend(options));
	}
});


var IrodaForm = Class.create(Form2, {
	initialize: function($super, options) {
		var childOptions = {
			title: (options.id) ? 'Iroda szerkesztése' : 'Iroda felvitele',
			ajax: {url:'/iroda/form'}
		}
		Object.extend(options || { }, childOptions);
		$super(Object.extend(options));
	}
});


var ProjektlakasForm = Class.create(Form2, {
	initialize: function($super, options) {
		var childOptions = {
			title: (options.id) ? 'Projektlakás szerkesztése' : 'Projektlakás felvitele',
			ajax: {url:'/projektlakas/form'}
		}
		Object.extend(options || { }, childOptions);
		$super(Object.extend(options));
	}
});


var SelectFieldForm = Class.create(Form2, {
	initialize: function($super, options) {
		var childOptions = {
			title: (options.id) ? 'Választólistás mező szerkesztése' : 'Választólistás mező felvitele',
			ajax: {url:'/selectmezok/form'}
		}
		Object.extend(options || { }, childOptions);
		$super(Object.extend(options));
	}
});


/*
var EmailDialog = Class.create(Dialog, {
	initialize: function($super, options) {
		var childOptions = {
			title: 'E-mail szerkesztése',
			ajax: {url:'/megbizoceg/form'}
		}
		Object.extend(options || { }, childOptions);
		$super(Object.extend(options));
	}
});
*/


var MyForm = Class.create({

	initialize: function(connectorURL, options) {
		
		this.processOptions(options);
		
		this.connectorURL = connectorURL;
		
		this.overlay = new Overlay('#A6C5E3', 40);

		this.window = new Element('div', {'class': 'window'});
		this.window.setStyle({
			width: '700px',
			position: 'absolute',
			top: 20 + 'px',
			left: ((document.viewport.getWidth() - 713) / 2) + 'px',
			zIndex: this.overlay.getZindex() + 5, 
			display: 'none'
		});

		if(this.options.title)
			this.window.insert(this.title = new Element('h2').update(this.options.title));

		ajax_submitURL(this.connectorURL, function(response){
			
			this.window.insert(response.form);
			this.window.insert(new Element('a', {'class': 'close'}));

			document.body.insert(this.window);
			this.resizeWindow();
			this.window.show();
			this.addObservers();
			this.form.focusFirstElement();
		}.bind(this));
	},
	
	
	resizeWindow: function()
	{
		cHeight = this.window.getHeight();
		wpHeight = document.viewport.getHeight();
		if (cHeight < wpHeight - 53)
			this.window.setStyle({top: ((document.viewport.getHeight() - cHeight) / 2) + 'px',})
	},
	
	processOptions: function(options) {
		if (typeof options == 'object')
			this.options = options;
		else
			this.options = {};
	},
	
	addObservers: function() {
		this.form = this.window.select('form')[0];
		if (!this.form.action)
			this.form.action = this.connectorURL;
		new AjaxForm(this.form, {
			onComplete: function(response) {
				if (response.res)
					this.cancelButtonClick();
			}.bind(this)
		});

		this.cancelButton = this.window.select('a.close')[0];
		this.cancelButton.observe('click', function(){
			this.cancelButtonClick();
		}.bindAsEventListener(this));

		this.saveButton = this.window.select('input.save')[0];
		this.saveButton.observe('click', function(){
			this.saveButtonClick();
		}.bindAsEventListener(this));

	},
	
	cancelButtonClick: function(memo) {
		this.window.remove();
		this.overlay.remove();
	},

	saveButtonClick: function() {
	}

});

