Bw = {};

Bw.Core =
{
	bind: function (el, proto)
	{
		var p = null;
		var n = null;
			
		try	{
			p = (typeof proto == 'string') ? eval(proto) : proto;
		}
		catch (e){}
		
		if (p && p.selfclassName)
		{
			p.selfclass = eval (p.selfclassName);
			if (p.superclassName)
			{
				p.superclass = eval (p.superclassName);
				Bw.Core.bind (el, p.superclassName);
			}
			
			for (e in p) el[e] = p[e];
			
			el.className = p.selfclassName.replace (/\./g, "");
			
			return p;
		}		
	},
	
	bootstrap: function (el, c)
	{
		var n = c || el.className;
		

		
		if (n && Bw.Core.bind(el, n) && !el.initialize()) return;
		
		var e = el.firstChild;
		while (e != null)
		{
			try
			{
				Bw.Core.bootstrap(e);
			}
			catch (ex){
			//	log (ex);
			}
			
			e = e.nextSibling;
		}
	},
	
	load: function (parent, url, force)
	{
		var q = Bw.IO.Query.create();
		if (force) q.setNoCache();
		q.get (url);
		parent.innerHTML = q.getText();
		
		Bw.Core.bootstrap (parent);
		Bw.Core.evalScript(parent.innerHTML, force);
	},
	
	evalScript : function (html, force)
	{
		var idx = html.toLowerCase().indexOf('<script');

		while(idx > -1)
		{
			var idxEnd = html.toLowerCase().indexOf('>', idx);
			
			var balise = html.substring(idx + 8, idxEnd);
			
			var idxSrc = balise.toLowerCase().indexOf("src=");
			
			if( idxSrc == -1 )
			{
				var idxSlashScript = html.toLowerCase().indexOf('</script>', idx + 8);
				
				var script = html.substring(idxEnd + 1, idxSlashScript);
				eval(script);
			}
			else
			{
				if( balise.charAt(idxSrc + 4) == '"' || balise.charAt(idxSrc + 4) == "'" )
				{
					idxSrc++;	
				}
				
				var idxSrcEnd = -1;
				
				for(var i = idxSrc + 4; i < balise.length ; i ++ )
				{
					if( balise.charAt(i) == ' ' || balise.charAt(i) == '"' || balise.charAt(i) == "'" )
					{
						idxSrcEnd = i;	
					}
				}
				
				var source = balise.substring(idxSrc + 4, idxSrcEnd);
				
				var q = Bw.IO.Query.create();
				if (force) q.setNoCache();
				q.get (source);
				if(q.getStatus() == 200)
				{
					eval(q.getText());
				}
			}
			
			idx = html.toLowerCase().indexOf('<script', idx + 8);
		}
	},
	
	check: function ()
	{
		
	},
	
	start: function ()
	{
		if (self.dialogArguments) self.opener = self.dialogArguments;
		
		Bw.Core.check();
		Bw.Core.bootstrap(document.body);
	}
	
	
};

Bw.instanceOf = function (el, type)
{
	return (el && ((el.selfclass && el.selfclass == type) || (el.className && el.className == type.selfclassName)));
};

Bw.inherits = function (el, type)
{
	if (!el) return false;
	var t = el.selfclass;
	while (t)
	{
		if (t == type) return true;
		t = t.superclass;
	}
	return false;
};

Bw.getById = function (id)
{
	return document.getElementById (id);
};

Bw.getEvent = function (e)
{
	if (!e) {
		e = window.event;
		e.target = e.srcElement;
	}
		
	return e; 
};

Bw.getGlobalMousePosition = function (e)
{
	var p = { x: e.clientX, y: e.clientY };
	var d = document.documentElement;
	var b = document.body;
	var w = window;

	p.x += (w.scrollX) ? w.scrollX : (d.scrollLeft + b.scrollLeft);
	p.y += (w.scrollY) ? w.scrollY : (d.scrollTop + b.scrollTop);

	return p;
};


Array.prototype.lookup = function (o)
{
	var l = this.length;
	for (var i = 0; i < l; i++) {
		if (this[i] == o) return i;
	}
	return -1;
};

function log (str)
{
	var l = Bw.getById ("log");
	if (!l)
	{
		l = document.createElement("DIV");
		l.style.font="menu";
		l.style.clear="both";
		l.id = "log";
		document.body.appendChild (l);
	}
	l.innerHTML += (str + "<br>");
}

window.onload = function () { Bw.Core.start(); };
