<?xml version="1.0" encoding="Win-1251"?>
<rmml xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
	<composition name="COrderedCompositionBase">
		<script><![CDATA[
			// Промежуток между упорядоченными элементами
			this.orderSpacing = 0;
			// Способ упорядочивания
			this.OT_HORIZONTAL = 0;  // По горизотали
			this.OT_VERTICAL = 1;    // По вертикали
			this.orderType = this.OT_HORIZONTAL;
			// Способ выравнивания упорядоченных элементов
			this.OA_UNDEFINED = null;
			this.OA_LEFT = 1;
			this.OA_CENTER = 0;
			this.OA_RIGHT = -1;
			this.orderAlignH = this.OA_LEFT;
			this.OA_TOP = 1;
			this.OA_MIDDLE = 0;
			this.OA_BOTTOM = -1;
			this.orderAlignV = this.OA_TOP;
			//
			this.orderOffsetX = 0;
			this.orderOffsetY = 0;
			//
			this.fixedWidth = 0;
			this.fixedHeight = 0;

			function getOrderedItems( aProcessAll)
			{
				var all = (aProcessAll ? true : false);
				var ords = new Array();
				var obj;
				// Выбираем упорячиваемые элементы
				for (var nm in this)
				{
					if (nm == 'parent' || nm == '_parent') continue;
					obj = this[ nm];
					if (this.isOrdered( obj) && (obj.visible || all))
						ords.push( obj);
				}

				// Сортируем
				ords.sort( function (a, b) {
					if (a.order < b.order) return -1;
					if (a.order == b.order) return 0;
					if (a.order > b.order) return 1;
				});
				return ords;
			}
			function update()
			{
				// Корректирует способы выравнивания
				if (this.orderType == this.OT_HORIZONTAL && this.orderAlignH == this.OA_UNDEFINED)
					this.orderAlignH = this.OA_LEFT;
				if (this.orderType == this.OT_VERTICAL && this.orderAlignV == this.OA_UNDEFINED)
					this.orderAlignV = this.OA_TOP;
				this.fixedWidth = Math.max( this.fixedWidth, 0);
				this.fixedHeight = Math.max( this.fixedHeight, 0);

				var ordW = 0;
				var ordH = 0;
				var bounds;
				var ords = getOrderedItems();
				var obj;
				// Выбираем упорячиваемые элементы
				for (var i = 0;  i < ords.length;  i++)
				{
					obj = ords[ i];
					ordW += obj.width + this.orderSpacing;
					ordH += obj.height + this.orderSpacing;
					if (typeof( obj.getBounds) == 'function')
						bounds = obj.getBounds();
					else
					{
						bounds = new Object();
						bounds.xMin = 0;
						bounds.xMax = obj.width;
						bounds.yMin = 0;
						bounds.yMax = obj.height;
					}
					if (this.fixedWidth > 0)
						bounds.xMax = bounds.xMin + this.fixedWidth;
					if (this.fixedHeight > 0)
						bounds.yMax = bounds.yMin + this.fixedHeight;

					if (this.orderAlignH != this.OA_UNDEFINED)
						obj.x = -bounds.xMin + this.orderOffsetX;
					if (this.orderAlignV != this.OA_UNDEFINED)
						obj.y = -bounds.yMin + this.orderOffsetY;
				}
				if (ordW > 0) ordW -= this.orderSpacing;
				if (ordH > 0) ordH -= this.orderSpacing;

				// Сортируем
				ords.sort( function (a, b) {
					if (a.order < b.order) return -1;
					if (a.order == b.order) return 0;
					if (a.order > b.order) return 1;
				});

				// Упорядачиваем
				if (this.orderType == this.OT_HORIZONTAL)
				{
					var ordX = Math.round( ordW * (this.orderAlignH - 1) / 2);
					for (var i = 0;  i < ords.length;  i++)
					{
						ords[ i].x += ordX;
						if (this.orderAlignV != this.OA_UNDEFINED)
							ords[ i].y += ords[ i].height * (this.orderAlignV - 1) / 2 + ords[ i].alignOffset;
						ordX += (this.fixedWidth <= 0 ? ords[ i].width : this.fixedWidth) + this.orderSpacing;
					}
				}
				else if (this.orderType == this.OT_VERTICAL)
				{
					var ordY = ordH * (this.orderAlignV - 1) / 2;
					for (var i = 0;  i < ords.length;  i++)
					{
						if (this.orderAlignH != this.OA_UNDEFINED)
							ords[ i].x += ords[ i].width * (this.orderAlignH - 1) / 2 + ords[ i].alignOffset;
						ords[ i].y += ordY;
						ordY += (this.fixedHeight <= 0 ? ords[ i].height : this.fixedHeight) + this.orderSpacing;
					}
				}
			}
			function isOrdered( aObj)
			{
				return (typeof( aObj) == 'object' && aObj != null &&
					typeof( aObj.parent) == 'object' && aObj.parent == this &&
					typeof( aObj.visible) == 'boolean' && typeof( aObj.order) == 'number');
			}
			function getOrderedCount()
			{
				var count = 0;
				for (var nm in this)
					if (this.isOrdered( this[ nm])) count++;
				return count;
			}
			function getOrderedMax()
			{
				var count = 0;
				for (var nm in this)
					if (nm != 'parent' && nm != '_parent' &&
						nm != 'class' && nm != '_class' && this.isOrdered( this[ nm]))
						count=Math.max(count,this[nm].order);
				return count+1;
			}
			function addOrdered( aObj, aUpdate)
			{
				if (aObj instanceof Array)
				{
					for (var i in aObj)
					{
						aObj[ i].order = this.getOrderedMax();
						if (aObj[ i].name == null || aObj[ i].name == '')
							aObj[ i].name = 'item' + aObj[ i].order;
						if (typeof( aObj[ i].alignOffset) == 'undefined')   // Смещение при выравнивании
							aObj[ i].alignOffset = 0;
					}
				}
				else
				{
					aObj.order = this.getOrderedMax();
					if (aObj.name == null || aObj.name == '')
						aObj.name = 'item' + aObj.order;
					if (typeof( aObj.alignOffset) == 'undefined')   // Смещение при выравнивании
						aObj.alignOffset = 0;
				}
				if (aUpdate)
					this.update();
			}
			function setOrdered( aObjArray, aType, aAlignH, aAlignV)
			{
				for (var i = 0;  i < aObjArray.length;  i++)
					this.addOrdered( aObjArray[ i], false);
				if (typeof( aType) == 'number')
					this.orderType = aType;
				if (typeof( aAlignH) == 'number')
					this.orderAlignH = aAlignH;
				if (typeof( aAlignV) == 'number')
					this.orderAlignV = aAlignV;
				this.update();
			}
		]]></script>
	</composition>
</rmml>
