/**
 * @author Arian Stolwijk
 * Idea taken from http://www.webtoolkit.info/ajax-file-upload.html
 */

var iFrameFormRequest = new Class({

        Implements: Options,

        options: {
                onRequest: function(){},
                onComplete: function(data){}
        },

        initialize: function(formElmt,options){
                this.formElmt = formElmt;
                this.setOptions(options);
                this.frameId ='f' + Math.floor(Math.random() * 99999);
                this.formElmt.set('target',this.frameId);

                this.formElmt.addEvent('submit',function(){
                        this.options.onRequest();
                }.bind(this));

                this.iframe = new IFrame({
                        name: this.frameId,
                        styles: {
                                display: 'none'
                        },
                        src: 'about:blank',
                        events: {
                                load: function(){
                                        var doc = window.frames[this.iframe.get('id')].document;
                                        if (!doc) {
                                                if (this.contentWindow) {
                                                        var doc = this.contentWindow.document;
                                                } else if (this.contentDocument) {
                                                        var doc = this.contentDocument;
                                                }
                                        }

                                        if (doc.location.href == 'about:blank') {
                                                return;
                                        }

                                        if ($type(this.options.onComplete) == 'function') {
                                                this.options.onComplete(doc.body.innerHTML);
                                        }

                                }.bind(this)
                        }
                }).inject($(document.body),'top');
        },

        toElement: function(){
                return this.iframe;
        }

});
