
/* XHttpRequest Arrows */

var XHttpArrow = new Class({
    Extends: ArrowA,

    initialize: function(init){
        init = init || {};
        this.parent( function(param, k) {
            param = param || {};
            if($type(param) === 'string'){ param = {url: param}; }

            var opts = {};
            var type = init.type || 'raw';

            opts.url = param.url || init.url || null;
            opts.method = param.method || init.method || 'post';
            opts.data = param.data || init.data || '';
            opts.encoding = param.encoding || init.encoding || 'utf-8';
            opts.header = param.headers || init.headers || null;
            opts.evalScripts = $defined(param.evalScripts) ? 
                                   param.evalScripts :
                                   $defined(init.evalScripts) ?
                                        init.evalScripts : true;
            opts.evalResponse = $defined(param.evalResponse) ?
                                    para.evalResponse :
                                        $defined(init.evalResponse) ?
                                            init.evalResponse : false;
            if(type === 'json' && false){ 
                opts.secure = $defined(param.jsonSecure) ? 
                                  param.jsonSecure :
                                  $defined(init.jsonSecure) ?
                                    init.jsonSecure : true;
            }

            var successFunctionUnpacker;
            var request = null;

            function onComplete(x){
                k.removeEvent('cancel', onCancel);
                k.cont(x);
            }

            function onCancel(){
                if(request) request.cancel();
                k.removeEvent('cancel', onCancel);
            }
            k.addEvent('cancel', onCancel);

            opts.onSuccess = function(){ onComplete(successFunctionUnpacker( arguments )); }
            opts.onFailure = function(){ onComplete(null); }

            if(type === 'raw'){
                successFunctionUnpacker = function(a){
                    return {text: a[0], xml: a[1]};
                }
                request = new Request(opts);
            } else if( type === 'html' ){
                successFunctionUnpacker = function(a){
                    return { tree: a[0], elements: a[1], html: a[2], javascript: a[3] };
                }
                request = new Request.HTML(opts);
            } else if( type === 'json' ){
                successFunctionUnpacker = function(a){
                    return { json:a[0], text:a[1] };
                }
                request = new Request.JSON(opts);
                /*successFunctionUnpacker = function(a) {
                    return { text: a[0], json: JSON.decode(a[0]) };
                }
                request = new Request(opts);*/
            }

            request.send();
        });
    }
});

function $jsonRequest(param){
    var p = param || {};
    p.type = 'json';
    return new XHttpArrow( p );
}

function $htmlRequest(param){
    var p = param || {};
    p.type = 'html';
    return new XHttpArrow( p );
}

function $request(param){
    var p = param || {};
    p.type = 'raw';
    return new XHttpArrow( p );
}

loadImage = new ArrowA(function(x, k) {
            var canceled = false;
            function onCancel(){ 
                canceled = true;
                k.removeEvent('cancel', onCancel);
            }
            k.addEvent('cancel', onCancel);

            Asset.image( x, { onload:
                function(i) {
                    if(!canceled) k.cont(i);
                }
            });
        });

