/*
*   Requires: base.js
*   Uses:     dynLib.js, effectLib.js
*/

function ajaxLib() {
    if (UserAgent.knowsAjax) {
               
        /*******************
        *   Ajax-Objects   *
        *******************/
        var AbstractAjaxObject = function() {
            this.object     = null;
            this.uri        = null;
            this.renderer   = null;
            this.request    = null;
            this.response   = null;
            this.loadTime   = null;
            this.loaded     = false;
            this.loading    = false;
            this.rendering  = false;
            this.isRendered = false;
            this.isShown    = false;
            this.isError    = false;
            
            this.onerror = function(transport, json) {
                this.setLoading(false);
                this.loaded   = false;
                this.response = transport;
                if (this.response.status == 302) {
                    this.uri = this.response.getResponseHeader('Location');
                    this.load();
                    return;
                }
                if (this.options['showOnload']) { this.showError(); }
            }
        }
        
        
        DynAjaxObject = function(obj, uri, renderer, outputEngine, effects, options) {
            this.options = Object.extend({}, this.options);
            this.content = getCorrespondingLi(obj);
            this.replacements = Object.extend({
                'url':      function() { return this.object.getElementsByTagName('a')[0].getAttribute('href'); }.bind(this),
                'title':    function() { return this.object.getText ? this.object.getText() : this.object.innerText.trim(); }.bind(this),
                'link':     function() { return '<a href="'+this.object.getElementsByTagName('a')[0].getAttribute('href')+'">Link zur Seite<\/a>'; }.bind(this),
                '_':''
            }, this.replacements);
            this.init(obj, uri, renderer, outputEngine, effects, options);
            if (isSet(this.options['module'])) {
                this.setIvwPath(this.options['module'], this.replacements['title']());
            }
            
            
            this.getContentPart = function(no) {
                var c = this.getContentElement();
                var n = 0;
                for (var i=0; i<c.childNodes.length; i++) {
                    if (new RegExp(/\bmodulePart\b/).test(c.childNodes[i].className)) {
                        if (n++ == no) { return c.childNodes[i]; }
                    }
                }
                var newNode = null;
                while (n++ <= no) {
                    newNode = document.createElement('div');
                    newNode.className = 'modulePart';
                    c.appendChild(newNode);
                }
                return newNode;
            }
        }
        DynAjaxObject.prototype = new AbstractAjaxObject();
    }
    
	jsLoaded('ajaxLib.js');
}

requires('dynLib.js', ajaxLib);
