﻿var Box = new Class(
{
	initialize: function()
	{
		this.boxes = [];
		ui.registerResizeHandler(this.resizeBoxes);
		ui.registerCleanupHandler(this.removeBoxes);
	},

	resizeBoxes: function()
	{
		for (var i = 0; i < box.boxes.length; i++)
		{
			box.resizeBox(box.boxes[i]);
		}
	},

	removeBoxes: function(rootElement)
	{
		var filteredBoxes = [];

		for (var i = 0; i < box.boxes.length; i++)
		{
			var boxContainer = box.boxes[i];
			var boxControl = ui.getNestedControl($(boxContainer.retrieve("dialogID")), boxContainer.controlID);

			if (boxControl != null && !rootElement.hasChild(boxControl))
			{
				filteredBoxes.push(boxContainer);
			}
		}

		box.boxes = filteredBoxes;
	},

	setBoxWidth: function(parentWidth, pnlBox)
	{
		var id = pnlBox.id;
		var dialogID = pnlBox.retrieve("dialogID");

		var t = ui.getNestedControl(dialogID, id + "_tbl_t");
		var tImg = t.getElement("img");
		var c = ui.getNestedControl(dialogID, id + "_tbl_c");
		var b = ui.getNestedControl(dialogID, id + "_tbl_b");
		var bImg = b.getElement("img");

		var innerWidth = parentWidth - pnlBox.retrieve("borderWidth") * 2;

		t.set("width", innerWidth);
		tImg.set("width", innerWidth);
		c.set("width", innerWidth);
		b.set("width", innerWidth);
		bImg.set("width", innerWidth);
	},

	setBoxHeight: function(parentHeight, pnlBox)
	{
		var id = pnlBox.id;
		var dialogID = pnlBox.retrieve("dialogID");

		var l = ui.getNestedControl(dialogID, id + "_tbl_l");
		var lImg = l.getElement("img");
		var c = ui.getNestedControl(dialogID, id + "_tbl_c");
		var r = ui.getNestedControl(dialogID, id + "_tbl_r");
		var rImg = r.getElement("img");

		var innerHeight = parentHeight - pnlBox.retrieve("borderHeight") * 2;

		l.set("height", innerHeight);
		lImg.set("height", innerHeight);
		c.setStyle("backgroundColor", "#ffffff");
		c.set("height", innerHeight);
		r.set("height", innerHeight);
		rImg.set("height", innerHeight);
	},

	resizeBox: function(pnlBox)
	{
		var control = ui.getNestedControl(pnlBox.retrieve("dialogID"), pnlBox.controlID);
		var parent = control.getParent();
		var lcSizerElement = control.getElement(".lcSizer");

		if (parent == null || control == null)
		{
			return;
		}

		var parentSize = parent.getSize();

		if (lcSizerElement != null)
		{
			var lcSizerSize = lcSizerElement.getSize();
			var lcSizerHeight = lcSizerSize.y + control.retrieve("borderHeight") * 2;
			parentSize.y = lcSizerHeight;
		}

		if (control.fixedWidth == false)
		{
			box.setBoxWidth(parentSize.x, control);
		}

		if (control.fixedHeight == false)
		{
			box.setBoxHeight(parentSize.y, control);
		}
	},

	initControl: function(dialogID, controlID, borderWidth, borderHeight, fixedWidth, fixedHeight)
	{
		var control = ui.getNestedControl(dialogID, controlID);

		if (control == null)
		{
			return;
		}

		control.store("dialogID", dialogID);
		control.store("borderWidth", borderWidth);
		control.store("borderHeight", borderHeight);
		control.store("fixedWidth", fixedWidth);
		control.store("fixedHeight", fixedHeight);

		if (fixedWidth == false || fixedHeight == false)
		{
			box.boxes.push({ "dialogID": dialogID, "controlID": controlID });
		}
	}
});

var box = new Box();