var Ajax = new Class({
    Implements : Options,
    options : {
        pr : true,      //Page ready refresh
        url : '/',
        transparent : false, //Do not process results
        onRequest : function(){},
        onFailure : function(){},
        onComplete : function(){},
        onSuccess : function(){},
        aid : false
    },
    initialize : function(options)
    {
        this.setOptions(options);
        this.initForm();
        this.send();
    },
    initForm : function()
    {
        if(typeOf(this.options.post) == "element")
        {
            var h = new Element('input', {
                'type' : 'hidden',
                'name' : 'ajax',
                'value' : 'true'
            }).inject(this.options.post);
        }
    },
    send : function()
    {
        var uri = new URI(this.options.url);
        this.options.url = uri.get('directory') + uri.get('file') + (uri.get('query') != null ? '?' + uri.get('query') : '');


        var req = {
            Implements : Chain,
            url : this.options.url,
            headers : {
                'Content-Type': 'application/x-www-form-urlencoded'
            },
            stat : this.options.stat,
            pr : this.options.pr,
            transparent : this.options.transparent,
            onRequest:function()
            {
                window.removeEvents('hashchange');

                if(this.options.transparent) return;
                
                {
                    $(document.body).setStyle('cursor','wait');
                    $(document.body).getElements('a,input').each(function(item){
                        item.setStyle('cursor','wait')
                    });                 
                }
                this.options.onRequest(this.options.aid);
            }.bind(this),
            onFailure:function()
            {
                this.options.onFailure(this.options.aid);
            }.bind(this),
            onComplete:function()
            {
                if(!this.options.transparent)
                {
                    $(document.body).setStyle('cursor','default');
                    $(document.body).getElements('a,input').each(function(item){
                        item.setStyle('cursor','default')
                    });
                  
                }
                
                this.options.onComplete(this.options.aid);
            }.bind(this),
            onSuccess:function(json, text)
            {
                if(!json)
                    return;
                this.array = [];

                var show = function(nodes)
                {
                    nodes.each(function(node){
                        var type = node.type;
                        var name = node.name;
                        var content = node.content;
                        
                        
                        switch(type)
                        {
                            case "eval":
                                $exec(content);
                                break;

                            case "js":
                                Asset.javascript(content);
                                break;

                            case "appendHtml":
                                if($type($(name)) == "element"){
                                    var el = new Element('div', {
                                        'html' : content
                                    });
                                    $(name).grab(el.getFirst());
                                }
                                break;

                            case "value":
                                if($type($(name)) == "element")
                                {
                                    if($(name).get('type') == 'checkbox')
                                        $(name).checked = (content == 'checked' ? 'checked' : '');
                                    else
                                        $(name).value = content;
                                }
                                break;

                            case 'redirect':
                                window.location.href = content;
                                break;

                            case 'goTo':
                                if($(content))
                                {
                                    var fx2=new Fx.Scroll($(document.body));
                                    fx2.toElement($(content));
                                }
                                break;
                                
                            case 'html':
                            default:
                                $(name).set('html', content);
                                break;
                        }
                    });
                    
                    
                }.bind(this);

                show(json);

                this.options.onSuccess(this.options.aid);
            }.bind(this)
        };

        new Request.JSON(req).send(this.options.post);
    }    
});
