function iFrameUploader(inputElement, options) {
    options              = options || {};
    this.autoUpload      = options.autoUpload || false;
    this.debug           = options.debug || false;
    this.element         = inputElement;
    this.form            = inputElement.up('form');
    this.containerParent = options.containerParent || document.body;
    this.container       = null;
}

iFrameUploader.prototype = {
    _startUpload: function(uploadForm) {
        uploadForm.appendChild(this.element.cloneNode(true));
        uploadForm.submit();
        this._fire('uploadStart', { uploader: this, filename: this.element.value });
    },
    _fire: function(event, params) {
        event = 'if:' + event
        this.element.fire(event, params);
    },
    subscribe: function(event, listener) {
        if (!this.element) {
            Log.error('It is too soon to watch for events, object is not initialized yet');
            return;
        }

        event = 'if:' + event;
        this.element.observe(event, listener);
    },
    transform: function(response) {
        var hidden = new Element('input', { type: 'hidden', name: this.element.readAttribute('name'), value: response.token });
        var text   = new Element('input', { type: 'text', value: 'Feltöltött fájl: ' + response.origName, disabled: 'true' });
        this.element.insert({ before: text, after: hidden });
        this._fire('uploadFinish', { uploader: this, token: response.token, filename: response.origName });
        if (!this.debug) {
            this.element.remove();
            this.container.remove();
        }
    },
    init: function() {
        var options = { id: this.element.name + 'uploader', style: 'display: none' };
        if (this.debug) {
            options.style = 'display: block; width: 600px; height: 600px';
        }
        this.container = this.containerParent.appendChild(new Element('div', options));

        var iframeName = this.element.name + 'frame';
        var uploadForm = new Element('form', { enctype: 'multipart/form-data', action: $N.loc('/store'), method: 'post', target: iframeName });
        var iframe     = new Element('iframe', { src: 'about:blank', name: iframeName });
        var fieldName  = new Element('input', { type: 'hidden', name: 'fieldName', value: this.element.readAttribute('name') });

        this.container.insert(uploadForm);
        this.container.insert(iframe);
        uploadForm.insert(fieldName);

        iframe.observe('load', function(e) {
            var contw = e.findElement().contentWindow;
            if (contw.document.URL != 'about:blank') {
                var response     = contw.$('response');
                var responseJSON = response.innerHTML.evalJSON();
                if (responseJSON.result) {
                    this.transform(responseJSON);
                } else {
                    this._fire('uploadFail', { error: responseJSON.error, element: this.element, filename: this.element.value });
                }
            }
        }.bind(this));

        if (this.autoUpload) {
            this.element.observe('change', function(e) {
                this._startUpload(uploadForm);
            }.bind(this));
        } else {
            this.form.observe('submit', function(e) {
                e.stop();
                this._startUpload(uploadForm);
            }.bind(this));
        }
    }
}



