var RegioDialog_Form = new Class({

	Implements: [Events, Options],
	Extends: RegioDialog,

	options: {
		json: null,
		flipbook_key: 0,
		key : 0,
		ImageIndex: 0
	},

	initialize : function(options){
		this.root = document.getElement('body');
		this.setOptions(options);

		this.Mask = new RegioMask();

		document.addEvent('keydown', function (event) {
			var PressedKey = event.key;

			if( PressedKey == 'esc') {
				event.stop();
				this._close();
			}

		}.bind(this));
	},

	open: function ( TargetUrl ) {

		this.TargetUrl = TargetUrl;

		// Checks if the element Dialog exists and delete it
		if( $chk($('dialog')) ) {
			$('dialog').destroy();
		}

		var dialog = new Element('div', {
			'id': 'dialog',
			'class': 'dialog form-dialog',
			'styles': {
				'top': document.getScroll().y + 50
			}
		});

		var dialog_header  = new Element('div',{
			'class': 'dialog_header'
		}).inject(dialog);

		var dialog_header_close  = new Element('div',{
			'class': 'close',
			'events': {
				'click': function () {
					this._close()
				}.bind(this)
			}
		}).inject(dialog_header);

		var dialog_content  = new Element('div',{
			'class': 'dialog_content'
		}).inject(dialog);

		dialog.inject(this.root);

		this._request(this.TargetUrl,'get');

	},

	_observeDialog: function () {
		
		if( $chk($('dialog').getElement('div.form-success')) == true ) {
			this._close(10000);
		}

		$('dialog').getElement('form').addEvent('submit', function (event) {
			event.stop();

			this._request(this.TargetUrl,'post');
		}.bind(this));

	},

	_request: function (Target, type) {

		var DialogRequest = new Request.HTML({
			url: Target,
			async: false,
			onComplete: function (responseTree, responseElements, responseHTML, responseJavaScript) {
				$('dialog').getElement('.dialog_content').set('html',responseHTML);
				this._observeDialog();

				// send google analytics event
				gaLabel = $('dialog').getElement('.dialog_content .box-header').get('html').stripTags();
				gaLabel = (gaLabel.length > 0) ? gaLabel : Target;
				_gaq.push(['_trackEvent', 'RegioDialog_Form', 'RequestCompleted - ' + type, gaLabel]);
			}.bind(this)
		});

		if( type == 'post') {
			DialogRequest.post($('dialog').getElement('form'));
		} else {
			DialogRequest.get();
		}


	}


});
