if(!qcms){
    var qcms = {}
}
qcms.page = {}
qcms.page.Observer = function (_parentId,_domId,_uri){
    this._parentId  = _parentId == null?'':_parentId
    this._domId     = _domId
    
    this._uri       = _uri
    if (qcms.root){
        //-- bug fix for some adjustments that were needed for streamlined version
        this._uri = this._uri.replace(`${qcms.root}/`,'')
    }
    this.finalize = function(_id){
        var _script = $(`${_id} script`)
        
        if (_script.lenth > 0){
            _script.each( (_index)=>{
                var _code = $(_script)[_index].text
                if (_code){eval(_code)}
                
            })
        }
    }
    this.apply = function (_caller){
        _uri = this._uri
        _finalize = this.finalize
        _domId = this._domId
            
        var _id = `${this._parentId} #${this._domId}`.trim()

        var http = HttpClient.instance()
        http.setHeader('dom',this._domId)
        // http.setHeader('uri',this._uri)
        var _uri = `${qcms.context}/${this._uri}`
        
        http.post(_uri,function(x){
            var _dom = $(x.responseText)
            _dom = $('<div></div>').append(_dom)
            var _found = qcms.html.hasNode( $(_id), $(_dom))
            
            if (_found == 0){
                
                
                $(_id).append (_dom) 
                _finalize(_id)

            }
            _caller.notify()
        })
        
            
        
        
    }
    

}
qcms.page.loader = function(_parentId,_layout){
    
    if (_parentId.constructor == Object) {
        _layout = _parentId 
        _parentId = null
    }
    if (!_layout) {
        return ;
    }
   
    if (_layout.on){
        jx.utils.keys(_layout.on.load).forEach(function(_domId){
                
            var observers = 
            jx.utils.patterns.visitor(_layout.on.load[_domId], function(_uri){
                _uri = _layout.root_prefix != null? (_layout.root_prefix+_uri) : _uri
               
                return new qcms.page.Observer(_parentId,_domId,_uri)
            })
            
            jx.utils.patterns.observer(observers,'apply')
           

        })
    }
}
qcms.page.load = function (_uri,_id){
    
    var http = HttpClient.instance()
    http.get(_uri,(_x)=>{
        // var _id = _id[0] == '#' || _id[0] == '.'?_id : (jx.dom.exists(_id)?`#${_id}`:`.${_id}`)
        // alert(_id)
        $(_id).html(_x.responseText)
    })
}
//
// making backwards compatibility
if(!bootup){
    var bootup = {}
}
bootup.init =qcms.page.loader
    
