/////////////////////////////////////
// create by 
//        Roberto Alonso-Gomez, 2007
////////////////////////////////////
var $em = 0.0625;

if (typeof $xx =="undefined")
{
    $xx = 
    {
        kernel :
        {
            doc: window.document,
            
            cdelay: 100,
            
            browser : function()
                {    
                    var u = navigator.userAgent;
                    return (u.indexOf('MSIE 7')>=0? 1: 
                            (u.indexOf('Gecko')>0 ? 2: 
                             (u.indexOf('MSIE 6')>0 ? 3 : 4)));
                }(),
                
            pc : [],    // postcreate call
            
            checkPc: function()
            {
                if (this.pc.length>0)
                    this.timeout(this, $xx.kernel.acreate, $xx.kernel.cdelay);
            },
            
            acreate: function()
            {
                if (this.pc.length>0) 
                {   
                    var pc = null;
                    while (pc=this.pc.pop())
                        pc();
                }
            },
            
            timeout : function(t, h, d)
            {
		        var w = function(){h.apply(t)};
		        return window.setTimeout(w, d ? d : 0);
	        },

            mix : function(f, t)
            {
                for (var i in f)
                    if (!t[i])
                        t[i] = f[i];
            }, 
            
            eval : function (sf, self)
            {
                var r = null;
                try {r=eval(sf);}catch(e){}
	            return r;
            },
            
            toJson : function(o, name, ind) 
            {
                var json = name ? ('"'+name+'"') : "";
                if (o instanceof Array) 
                {
                    a = [];
                    for (var i=0,n=o.length; i<n; i++)
                      a[i] = this.toJson(o[i], "", ind);
                    json += (name?":[":"[") + (a.length > 1 ? (ind+a.join(","+ind)+ind) : a.join("")) + "]";
                }
                else 
                    if (o == null)
                        json += (name&&":") + "null";
                else 
                    if (typeof(o) == "object") 
                    {
                        var arr = [];
                        for (var m in o)
                            arr[arr.length] = this.toJson(o[m], m, ind);
                        json += (name?":{":"{") + (arr.length > 1 ? (" "+ind+" "+arr.join(", "+ind+" ")+" "+ind) : arr.join("")) + "}";
                    }
                else 
                    if (typeof(o) == "string")
                        json += (name&&":") + '"' + o.toString() + '"';
                    else
                        json += (name&&":") + o.toString();
                return json;
            },
            
            matches : function(tmp, obj, self)
            {
                var m = tmp.match(/\$\{([^\}]+)\}/g);
                if (m) 
                {
			        for(var i=0; i<m.length; i++) 
			        {
				        var k = m[i];
				        k = k.substring(2, k.length-1);
				        var v = this.eval.apply(obj, [k, self]);
				        if (typeof v == "object") 
					        v = escape(this.toJson(v, "", ""));
				        tmp = tmp.replace(m[i], v);
			        }
                }
                return tmp;
            },
            
            _build : function(h, p)
            {
                h.normalize();
                var xx = h.getAttribute("xx");
                if (xx) 
                {
                    var x = '['+unescape(xx)+']';
                    var d = this.eval(x);
                    if (d && d[0]) 
                    {
                        var xxv = d[0];
                        h.xxv = xxv;
                        if (xxv.cn)
                        {
                            var cn = xxv.cn;
                            if ($xx.widgets[cn]) 
                            {
                                var self = $xx.widgets[cn];
                                h.xxself = self;
                                var evts = self.events;
                                if (evts) 
                                    for (evt in evts)
                                        this.addEvent(h, evt, evts[evt]);
                                  
                                var xxlv = {};
                                h.xxlv = xxlv;
                                this.mix(self.lv, xxlv);
                                
                                var ac = self.afterCreate;
                                if (typeof ac!="undefined") 
                                    this.pc.push(function(){ac.apply(h);});
                            }
                                
                            if (p)
                            { 
                                if (!p.xxi) 
                                    p.xxi = [];
                                p.xxi.push(h);
                            }
                            h.xxp = p;
                            p = h;
                        }
                    }
                }
                
                var xxev = h.getAttribute('xxev');
                if (xxev && p)
                {
                    var self = p.xxself;
                    var evts = self[xxev];
                    if (typeof evts!="undefined") 
                        for (evt in evts)
                            this.addEvent(h, evt, evts[evt], p);
                }
                
                for (var n = h.firstChild; n; n=n.nextSibling) 
                {
                    if (n.nodeType==1) 
                        this._build(n, p);
                }
            },
            
            build : function(h)
            {
                this._build(h, null);
                this.checkPc();
            },
            
            addEvent : function (obj, en, afn, p)
            {
                if (afn) 
                {
                    var fn = function(evt){return afn.apply(obj, [evt||window.event, p]);};
	                if (obj.attachEvent)
		                obj.attachEvent("on"+en, fn);
	                else 
	                    if(obj.addEventListener)
		                    obj.addEventListener(en, fn, true);
	                    else
		                    obj["on"+en] = fn;
		            return fn;
                }
            },
            
            changeCSS : function(css)
            {
                if (this.doc.styleSheets.length>0)
                    this.doc.styleSheets[0].href = css;
                else
                {
    	            var link = this.doc.createElement('link');
		            link.setAttribute('href', css);
	                link.setAttribute('type', 'text/css');
	                link.setAttribute('rel', 'stylesheet'); 
                    var _head = this.doc.getElementsByTagName("head");
                    if (_head && _head.length>0)
                        _head[0].appendChild(link);
                }
            }
        } , // *kernel
        
        dom:
        {
            fchild: function(e)
            {
                e = e.firstChild;
                if (e && e.nodeType!=1) 
                    return this.next(e);
                return e;
            },
            
            lchild: function(e)
            {
                e = e.lastChild;
                if (e && e.nodeType!=1) 
                    return this.prev(e);
                return e;
            },
            
            next: function(e)
            {
                while (e = e.nextSibling) 
                    if (e.nodeType==1) 
                        return e;
                return null;
            },
            
            prev: function(e)
            {
                while (e=e.previousSibling) 
                    if (e.nodeType == 1) 
                        return e;
                return null;
            },
            
            left: function(e)
            {
                if ($xx.kernel.browser==2)
                    return e.x;
                else 
                    return e.offsetLeft;
            },
            
            cindex: function(e)
            {
                var r = 0;
                while (e=this.prev(e))
                    r++;
                return r;
            },
            
            getxxId : function(e, n)
            {
				if (e.getAttribute('xxid')==n)
					return e;
				var r = null;
				for (var i=this.fchild(e); i; i=this.next(i))
				{
					r = this.getxxId(i, n);
					if (r)
						return r;
				}
				return r;
            },
            
            createElement : function(n, e)
            {	
				var doc = e? e.ownerDocument : window.document;
				var el = doc.createElement(n);
				doc.body.appendChild(el);
				return el; 
            }
        }, // *dom
        
        xml:
        {
            // call using new
            xmlDoc : null,
            
            load : function(xml, asyn)    
            {
                try //Internet Explorer
                {
                    this.xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
                }
                catch(e)
                {
                    try //Firefox, Mozilla, Opera, etc.
                    {
                        this.xmlDoc = document.implementation.createDocument("","",null);
                    }
                    catch(e)
                    {
                    //alert(e.message);
                        return;
                    }
                }
            },
            
            loadXML : function(text)
            {
                try 
                {
                    this.xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
                    this.xmlDoc.async = "false";
                    this.xmlDoc.loadXML(text);
                }
                catch(e)
                {
                    try 
                    {
                        var parser = new DOMParser();
                        this.xmlDoc = parser.parseFromString(text,"text/xml");
                    }
                    catch(e) 
                    {}
                }
            }
        }, // *xml
        
        widget:
        {
            def : {},
            cn : "widget",
            
            create: function(dt)
            {
                var o = {cn:this.cn};
                $xx.kernel.mix(dt, o);
                $xx.kernel.mix(this.def, o);
                return (this.render(o));
            },
            
            afterCreate: function()
            {
                var v=this.xxv;
                var s = this.style;
                if (v && s)
                {
                    //if (v.wpos)
                        //s.position = "absolute";
                    var st = function(d){if (v[d]) s[d] = v[d]+'px';};
                    if (v.top)
                        s.top = (v.top*$em)+"em";
                    if (v.left)
                        s.left = (v.left*$em)+"em";
                    if (v.width)
                        s.width = (v.width*$em)+"em";
                    if (v.height)
                        s.height = (v.height*$em)+"em";
                    /*st('top');
                    st('left');
                    st('width');
                    st('height');
                    */
                }
            },
            
            write: function()
            {
                var tt = document.write(this.render());
            },
            
            render : function(o)
            {
                var cx = "";
                var r = this.tp?this.tp:"";
                try
                {
                    r = $xx.kernel.matches(r, o);
                    var its = o.items;
                    if (!its) 
                    {
                        var tx = o.xitems;
                        if (tx) 
                        {
                            var xml = new $xx.xml.loadXML(tx);
                            
                        }
                    }
                    if (its)
                    {
                        o.fn = this;
                        var tmp = this.cx;
                        for (var i=0; i<its.length; i++)
                        {
                            var it = its[i];
                            if (it.render)
                                cx += it.render(o);
                            else
                                if (tmp)
                                {
                                    var f = 0;
                                    for (x in it)
                                    {
                                        f++;
                                        break;
                                    }
                                    if (f>0)
                                        cx += $xx.kernel.matches(tmp, it, o);
                                }
                        }
                        r = r.replace("$$", cx);
                    }
                }
                catch(e)
                {}
                return r;
            },
            
            moveTo : function(x,y,w,h)
            {
                return true;
            }
        },
        
        widgets :
        {
        },
        
        addWidget : function(name, fn)
        {
            this.widgets[name]=fn;
        }
    };
    
    $xx.addWidget('widget', $xx.widget);
}


