/************************************************************\
 Extend Prototype's Ajax:
 Example:
 new Ajax.Upload({
    url: BaseURL+'/cms/uploads',
    uploadform: document['frm_upload'],
    onComplete: callback
 });
\************************************************************/
Ajax.Upload = Class.create();
Ajax.Upload.prototype = {
    initialize: function (options) {
        this.options = {
            url: '',
            uploadform: null,
            onComplete: new Function
        }
        Object.extend(this.options, options);
        this._createTargetIframe();
        this._initObserve();
        this._upload();
    },
    
    _createTargetIframe: function () {
        this.WndTartget = $('ajax_upload_target');
        if (this.WndTartget) this.WndTartget.remove();
        this.WndTartget = new Element('iframe', {
            src: '#',
            id: 'ajax_upload_target',
            name: 'ajax_upload_target',
            style:'width:0; height:0; border:none;'
        });
        document.body.appendChild(this.WndTartget);
    },
    
    _initObserve: function () {
        window.onComplete = this.options.onComplete;
        var wnd = this.WndTartget.contentWindow;
//        wnd.onload = function () {
//            var wnd = this.parent;
//            this.responseText = this.document.body.textContent;
//            if(typeof wnd.onComplete=='function') wnd.onComplete(this);
//        }
    },
    
    _upload: function () {
        var frm = this.options.uploadform;
        frm.method = 'post';
        frm.action = this.options['url'];
        frm.enctype = 'multipart/form-data';
        frm.target = 'ajax_upload_target';
        this.options.uploadform.submit();
    }
}