var componentPath = null;
var componentsOnload = [];

var windowonload = null;

var toolbars = {};
var zoneStates = {};

function resizeTo (object, w, h, steps, interval)
{
	var ch = object.offsetHeight;
	var cw = object.offsetWidth;

	var dh = h - ch;
	var dw = w - cw;

	var sh = dh / steps;
	var sw = dw / steps;
	
	function doResize (object, i)
	{
		ch += sh;
		cw += sw;

		object.style.width = Math.round (cw) + 'px';
		object.style.height = Math.round (ch) + 'px';

		if (i < steps)
		{
			setTimeout (function(){doResize(object, i+1);}, interval);
		}
	}
	
	doResize (object,1);
}

function getChildWithClass(node, className)
{
	if( node == null )
	{
		return null;
	}
	
	for(var i = 0; i < node.childNodes.length ; i ++ )
	{
		var child = node.childNodes[i];

		if( child.nodeType != 1 )
		{
			continue;
		}

		if( child.className.indexOf(className) == 0 )
		{
			return child;
		}
	}

	return null;
}
function toggleToolbar (toolbar)
{
	if( toolbar == null )
	{
		return;
	}
	
	var container 	= getChildWithClass(toolbar, "tb_container");
	var content 	= getChildWithClass(container, "tb_content");
	var grip 	= getChildWithClass(container, "grip");
	
	if( content == null || grip == null )
	{
		return;
	}
	
	var s = content.style;

	if (!toolbar.opened)
	{
		//grip.className = "grip opened";
		//s.display = 'block';
		resizeTo (toolbar,92,28,5,20);
		toolbar.opened = true;
	}
	else
	{
		//grip.className = "grip";
		//s.display = 'none';
		resizeTo (toolbar,8,8,5, 20);
		toolbar.opened = false;
	}
}
function tb_click(zoneCode, url, mode)
{
	url += "&ajax=true";
	
	formContent = null;

	var zone = document.getElementById("zone-" + zoneCode);
	if( zone == null )
	{
		return;
	}

	Bw.Core.load( zone, url, true);
	
	for(z in zoneStates)
	{
		var state = zoneStates[z];
		
		if( state != 'display' && z != zoneCode)
		{
			tb_click(z, toolbars[z], null, "display");
		}
	}

	zoneStates[zoneCode] = mode;
	
}

function evalQueryText(q)
{
	var status = q.getStatus();

	if(status != 200)
	{
		return;
	}
	
	eval(q.getText());
}

function addComponentOnload(func)
{
	if( windowonload == null )
	{
		windowonload = window.onload;
		
		window.onload = function()
		{
			windowonload();
			
			executeOnload();
		}
	}
	
	componentsOnload.push(func);		
}

function executeOnload()
{
	for(var i = 0; i < componentsOnload.length ; i ++ )
	{
		componentsOnload[i]();
	}	
}

function registerToolbar(zoneCode, displayPath)
{
	toolbars[zoneCode] = displayPath;
}

