function CBinding(constructor)
{
	this.construct = constructor;
	this.onBindingComplete	= null;
	this.onResize			= null;
}

bindings = new Array();

function CGroup(first_el)
{
	this.add = CGroup_add;
	this.size = 0;
	this.els = new Array();
	
	this.add(first_el);
}

function CGroup_add(el)
{
//	dumpObj(this);
	this.els[this.size] = el;
	this.size++;
	
//	alert(this.size);
}

groups = new Array();

function Binder_OnLoad()
{
	var i;
	for (i=0; i < document.all.length; i++)
	{
		el = document.all(i);
	
		if (el.group!=null)
		{
			if (groups[el.group] == null)
			{
				groups[el.group] = new CGroup(el);
			}
			else
			{
				groups[el.group].add(el);
			}
		}
		if (el.binding!=null)
		{
			if (!bindings[el.binding])
			{
				alert("unknown binding: " + el.binding);
				continue; // loop
			}
			bindings[el.binding].construct(el);
		}
	}

	var b;
	for (b in bindings)
	{
		if (bindings[b].onBindingComplete != null)
		{
			bindings[b].onBindingComplete();
		}	
	}
}

window.onload=Binder_OnLoad;

// The above is all you need for the binder.
// Next, the dragging model - note that it binds at the document level!

var _Doc_DragDI = null;

function CDragInfo(el)
{
	this.el = el;
	this.x = 0;
	this.y = 0;
	this.onMove = null;
	this.onDrop = null;
}

function _Doc_DragStart(di)
{
	_Doc_DragDI = di;
	
	_Doc_DragDI.x = event.clientX - di.el.offsetLeft;
	_Doc_DragDI.y = event.clientY - di.el.offsetTop;
	
	CancelSelect();
}

function _Doc_omUp()
{
	if (_Doc_DragDI == null) 
	{
		return;
	}
		
	if (_Doc_DragDI.onDrop != null)
	{
		_Doc_DragDI.onDrop(_Doc_DragDI);
	}
	
	_Doc_DragDI = null;
}

function _Doc_omMove()
{
	if (_Doc_DragDI != null)
	{
		//alert("mmove");
		s = _Doc_DragDI.el.style;
		
		s.left = event.clientX - _Doc_DragDI.x;
		s.top = event.clientY - _Doc_DragDI.y;
			
		if (_Doc_DragDI.onMove != null)
		{
			_Doc_DragDI.onMove(_Doc_DragDI);
		}
	}
}

document.onmousemove = _Doc_omMove;
document.onmouseup = _Doc_omUp;

// Below are some useful functions.

function _Doc_onResize()
{
//	alert("Resize!");
	
	var b;
	for (b in bindings)
	{
		if (bindings[b].onResize != null)
		{
			bindings[b].onResize();
		}	
	}
}

window.onresize = _Doc_onResize;


function allTagProps(tagID)
{
//	alert("hello " + tagID);
	result = new Array();

	var i;
	for (i=0; i < document.all.length; i++)
	{
		el = document.all(i);
/*		alert(el);
		if (el["tabGroup"]!=null) {
			
		}
*/
		if (el[tagID]!= null)
		{
			//dumpObj(el);
			result[i]=el;
			//dumpObj(result);
		}
	}
	return result;
}

function instanceOf(object, constructor)
{
	while (object != null)
	{	
		if (object == constructor.prototype)
			return true;
		
		object = object.__proto__;
	}

	return false;
}

function Check_omOver(obj)
{	
	result = (!obj.contains(window.event.fromElement));
	return result;
}

function Check_omOut(obj)
{
//	TRACE ("checking ");
	result = (!obj.contains(window.event.toElement));
	return result;
}

function GetSource(binding)
{
	var src;
	for (src=event.srcElement; src.tagName != "BODY"; src = src.parentElement)
	{
		if (src.binding == binding)
			return src;
	}

	return null;
}

function CancelEvent()
{
	event.cancelBubble = true;
}

function CancelSelect()
{
	event.cancelBubble = true;
	event.returnValue = false;
}

function dumpObj(dobj)
{
	str = "Dump Object" + dobj + "\n";
		
	for (i in dobj) {
		str += i + ": " + dobj[i] + "\n";
	}
	
	alert (str);
}
