<?xml version="1.0" encoding="UTF-8"?>
<rmml>
<!--

$RCSfile: rnmc_stdCollection.xml,v $
$Revision: 1038 $

last change: $Date: 2009-03-30 17:48:30 +0400 (Пн, 30 мар 2009) $
             $Author: Pavel $

	Lacerta Project
	version 1.0.0.1
	Copyright (C) 2007-2009 RNMC
 -->

<composition name="rnmc_stdCollection">
<script><![CDATA[
	this.id = null;
	this.visibility = false;

	this.startState = COLLECTION_STATE_ITEM;
	this.currentState = COLLECTION_STATE_LIST;

	this.titleState = TITLE_STATE_VISIBLE; //TITLE_STATE_HIDDEN;

	this.collectionItems = null;
	this.currentItem = 0;

// управление
	this.controlStyle = null;
	this.controlX = 0;
	this.controlY = 0;
	this.controlObject = null;

// отдельные кнопки управления
	this.btnPrevious = null;
	this.btnNext = null;
	this.btnShowTitle = null;
	this.btnHideTitle = null;
	this.btnShowList = null;

	this.listAvailable = false;

// объекты текущего элемента коллекции
	this.currentItemObjects = null;
// объект который будет увеличиваться
	this.zoomTarget = null;

	this.lastTopIndex = 0;
// объект список
	this.listControlX = 45;
	this.listControlY = 34;
	this.listControlWidth = 242;
	this.listControlHeight = 520;
	this.listCurrentColor = null;
	this.listSelectedColor = null;

	this.columnsId = null;
	this.columnsWidth = null;
	this.columnsStyle = null;	

	function initFromNode(srcNode){
//trace("rnmc_stdCollection.initFromNode");
		var xmlNode = null;
		var xmlAttributeMap = null;
		var xmlAttribute = null;

		this.visible = false;
		this.collectionItems = new Array();

	//	настройки колонок
		this.columnsId = new Array();
		this.columnsWidth = new Array();
		this.columnsStyle = new Array();

		xmlAttributeMap = srcNode.attributes;
		if (xmlAttributeMap){
			xmlAttribute = xmlAttributeMap.getNamedItem("id");
			if (xmlAttribute != null && xmlAttribute.specified)
				this.id = xmlAttribute.value;
			xmlAttribute = xmlAttributeMap.getNamedItem("visible");
			if (xmlAttribute != null && xmlAttribute.specified){
				if(xmlAttribute.value == "true")
					this.visibility = OBJECT_VISIBILITY_TRUE;
				else if(xmlAttribute.value == "false")
					this.visibility = OBJECT_VISIBILITY_FALSE;
			}
		}
		xmlNode = srcNode.firstChild;
		while(xmlNode){
			if(xmlNode.nodeName == "settings"){
				var settingNode = xmlNode.firstChild;
				while(settingNode){
					if(settingNode.nodeName == "list"){
					// настройки списка иллюстраций
						var listNode = settingNode.firstChild;
						while(listNode){
							if(listNode.nodeName == "param"){
								xmlAttributeMap = listNode.attributes;
								if (xmlAttributeMap){
									xmlAttribute =  xmlAttributeMap.getNamedItem("x");
									if (xmlAttribute != null && xmlAttribute.specified)
										this.listControlX = parseInt(xmlAttribute.value);
									xmlAttribute =  xmlAttributeMap.getNamedItem("y");
									if (xmlAttribute != null && xmlAttribute.specified)
										this.listControlY = parseInt(xmlAttribute.value);
									xmlAttribute =  xmlAttributeMap.getNamedItem("width");
									if (xmlAttribute != null && xmlAttribute.specified)
										this.listControlWidth = parseInt(xmlAttribute.value);
									xmlAttribute =  xmlAttributeMap.getNamedItem("height");
									if (xmlAttribute != null && xmlAttribute.specified)
										this.listControlHeight = parseInt(xmlAttribute.value);
									xmlAttribute =  xmlAttributeMap.getNamedItem("currentColor");
									if (xmlAttribute != null && xmlAttribute.specified)
										this.listCurrentColor = xmlAttribute.value;
									xmlAttribute =  xmlAttributeMap.getNamedItem("selectedColor");
									if (xmlAttribute != null && xmlAttribute.specified)
										this.listSelectedColor = xmlAttribute.value;
								}
							} else if(listNode.nodeName == "column"){
								var columnId = null;
								var columnWidth = 0;
								var columnStyle = null;
								xmlAttributeMap = listNode.attributes;
								if (xmlAttributeMap){
									xmlAttribute =  xmlAttributeMap.getNamedItem("id");
									if (xmlAttribute != null && xmlAttribute.specified)
										columnId = xmlAttribute.value;
									xmlAttribute =  xmlAttributeMap.getNamedItem("width");
									if (xmlAttribute != null && xmlAttribute.specified)
										columnWidth = parseInt(xmlAttribute.value);
									xmlAttribute =  xmlAttributeMap.getNamedItem("style");
									if (xmlAttribute != null && xmlAttribute.specified)
										columnStyle = xmlAttribute.value;
								}
								if(columnId && columnWidth > 0){
									this.columnsId.push(columnId);
									this.columnsWidth.push(columnWidth);
									if(columnStyle && scene.StyleManager.styleExists(columnStyle)){
										this.columnsStyle.push(columnStyle);
									} else {
										this.columnsStyle.push(null);
									}
								}
							}
							listNode = listNode.nextSibling;
						}
						
						if (this.columnsWidth.length == 0)
							this.columnsWidth.push(this.listControlWidth);

						this.listAvailable = true;
					} else if(settingNode.nodeName == "mode"){
						xmlAttributeMap = settingNode.attributes;
						if (xmlAttributeMap){
							xmlAttribute =  xmlAttributeMap.getNamedItem("style");
							if (xmlAttribute != null && xmlAttribute.specified){
								if((xmlAttribute.value).toLowerCase == "list"){
									this.startState = COLLECTION_STATE_LIST;
								} else if((xmlAttribute.value).toLowerCase == "list"){
									this.startState = COLLECTION_STATE_ITEM;
								}
							}
						}
					} else if(settingNode.nodeName == "control"){
						xmlAttributeMap = settingNode.attributes;
						if (xmlAttributeMap){
							this.controlVisible = true;
							xmlAttribute =  xmlAttributeMap.getNamedItem("style");
							if (xmlAttribute != null && xmlAttribute.specified)
								this.controlStyle = xmlAttribute.value;
							xmlAttribute =  xmlAttributeMap.getNamedItem("x");
							if (xmlAttribute != null && xmlAttribute.specified)
								this.controlX = parseInt(xmlAttribute.value);
							xmlAttribute =  xmlAttributeMap.getNamedItem("y");
							if (xmlAttribute != null && xmlAttribute.specified)
								this.controlY = parseInt(xmlAttribute.value);
						}
					}
					settingNode = settingNode.nextSibling;
				}
			} else if(xmlNode.nodeName == "items"){
				var itemNode = xmlNode.firstChild;
				while(itemNode){
					if(itemNode.nodeName == "item"){
						var contentNode = itemNode.firstChild;
						var currentItem = new Array();
						while(contentNode){
							if(contentNode.nodeName == "title"){
								xmlAttributeMap = contentNode.attributes;
								if (xmlAttributeMap){
									currentItem.id = this.collectionItems.length + 1;
									currentItem.titleVisible = true;
									xmlAttribute =  xmlAttributeMap.getNamedItem("type");
									if (xmlAttribute != null && xmlAttribute.specified)
										currentItem.contentType = xmlAttribute.value;
								// считывание значения подписи 
								// value="" - для совместимости
									xmlAttribute =  xmlAttributeMap.getNamedItem("value");
									if (xmlAttribute != null && xmlAttribute.specified)
										currentItem.titleValue = xmlAttribute.value;
									if(contentNode.textContent != null)
										currentItem.titleValue = contentNode.textContent;
									xmlAttribute =  xmlAttributeMap.getNamedItem("style");
									if (xmlAttribute != null && xmlAttribute.specified)
										currentItem.titleStyle = xmlAttribute.value;
									xmlAttribute =  xmlAttributeMap.getNamedItem("bgcolor");
									if (xmlAttribute != null && xmlAttribute.specified)
										currentItem.titleBgColor = xmlAttribute.value
									else 
										currentItem.titleBgColor = "#00000000";
									xmlAttribute =  xmlAttributeMap.getNamedItem("visible");
									if (xmlAttribute != null && xmlAttribute.specified
										&& xmlAttribute.value == "false")
										currentItem.titleVisible = false;
									xmlAttribute =  xmlAttributeMap.getNamedItem("x");
									if (xmlAttribute != null && xmlAttribute.specified)
										currentItem.titleX = parseInt(xmlAttribute.value);
									xmlAttribute =  xmlAttributeMap.getNamedItem("y");
									if (xmlAttribute != null && xmlAttribute.specified)
										currentItem.titleY = parseInt(xmlAttribute.value);
									xmlAttribute =  xmlAttributeMap.getNamedItem("width");
									if (xmlAttribute != null && xmlAttribute.specified)
										currentItem.titleWidth = parseInt(xmlAttribute.value);
									xmlAttribute =  xmlAttributeMap.getNamedItem("height");
									if (xmlAttribute != null && xmlAttribute.specified)
										currentItem.titleHeight = parseInt(xmlAttribute.value);
								};
							} else {
								currentItem.push(contentNode);
							}
							contentNode = contentNode.nextSibling;
						};
						this.collectionItems.push(currentItem);
					// Если сохранять только указаьели на вершины,
					// объект XML-документ удаляется, что-бы этого не было
					// сделаем на него сишнюю сслку.
						this.collectionItems.owner = itemNode.ownerDocument;
					};
					itemNode = itemNode.nextSibling;
				};
			};
			xmlNode = xmlNode.nextSibling;
		}
	// ==========================================================================
	// создание списка
		this.itemsList = new Composition();
		this.itemsList._class = "common_list";
		this.itemsList.parent  = this;
		this.itemsList.x = this.listControlX;
		this.itemsList.y = this.listControlY;
		this.itemsList.depth = -10;
		if (this.listSelectedColor)
		{
			this.itemsList.selectedItemBgColor = this.listSelectedColor;
			this.itemsList.markSelected = true;
		}
		else
			this.itemsList.markSelected = false;

		if (this.listCurrentColor)
		{
			this.itemsList.currentItemBgColor = this.listCurrentColor;
			this.itemsList.markCurrent = true;
		}
		else
			this.itemsList.markCurrent = false;

		this.itemsList.initList(this.listControlWidth, this.listControlHeight,
			this.columnsWidth.length, this.columnsWidth);
		this.itemsList.setReleaseCallback(this, this.onSelectItem);
				
		// создание новых строк
		for(var i = 0; i  < this.collectionItems.length; i++)
			this.itemsList.addListItem();

		for(var i = 0; i  < this.collectionItems.length; i++){
			var newTxtObject = new Text();
			newTxtObject.value = (i + 1) + ".  ";
			if (this.columnsStyle.length > 0)
				newTxtObject.style = this.columnsStyle[0];
			newTxtObject.adjustHeight = true;
			newTxtObject.width = this.itemsList.getColumnWidth(1);
			this.itemsList.setRowObject(i, 0, newTxtObject);

			var newTxtObject = new Text();
			newTxtObject.value = this.collectionItems[i].titleValue;
			if (this.columnsStyle.length > 1)
				newTxtObject.style = this.columnsStyle[1];
			newTxtObject.adjustHeight = true;
			newTxtObject.width = this.itemsList.getColumnWidth(1);
			this.itemsList.setRowObject(i, 1, newTxtObject);
			
			for (var j = 2; j < this.columnsId.length; ++j){
				if (this.columnsId[j] == "type"){
					var newTxtObject = new Text();
					newTxtObject.value = this.collectionItems[i].contentType;
					if (this.columnsStyle.length > j-1)
						newTxtObject.style = this.columnsStyle[j];
					newTxtObject.adjustHeight = true;
					newTxtObject.width = this.itemsList.getColumnWidth(j);
					this.itemsList.setRowObject(i, j, newTxtObject);
				}
			}
		}

		this.itemsList.listArea.hitArea =
			"[0,0," + this.itemsList.listWidth + "," + this.itemsList.listHeight + "]";
		this.itemsList.topIndex = 0;
		this.itemsList.calculateTotalItemsHeight();
		this.itemsList.calculateMaxIndex();
		//this.itemsList.showObject();
		// 	end создание списка		============================================

		//	создание панели управления коллекцией		
		var controlSrcNode = scene.ControlManager.getControlNode(this.controlStyle)
		if(controlSrcNode){
			this.controlObject = new Composition();
			this.controlObject._class = "rnmc_stdControlGroup";
			this.controlObject.parent = this;
			this.controlObject.initFromNode(controlSrcNode);
			this.controlObject.x = this.controlX;
			this.controlObject.y = this.controlY;
			this.btnPrevious = this.controlObject.getChildControl("previous");
			if(this.btnPrevious)
				this.btnPrevious.setReleaseCallBack(this, this.buttonDispatcher);
			this.btnNext = this.controlObject.getChildControl("next");
			if(this.btnNext)
				this.btnNext.setReleaseCallBack(this, this.buttonDispatcher);
			this.btnShowTitle = this.controlObject.getChildControl("showtitle");
			if(this.btnShowTitle)
				this.btnShowTitle.setReleaseCallBack(this, this.buttonDispatcher);
			this.btnHideTitle = this.controlObject.getChildControl("hidetitle");
			if(this.btnHideTitle)
				this.btnHideTitle.setReleaseCallBack(this, this.buttonDispatcher);
			this.btnShowList = this.controlObject.getChildControl("showlist");
			if(this.btnShowList)
				this.btnShowList.setReleaseCallBack(this, this.buttonDispatcher);
		// кнопки увеличения/уменьшения
			this.btnZoomIn = this.controlObject.getChildControl("zoomin");
			if(this.btnZoomIn)
				this.btnZoomIn.setReleaseCallBack(this, this.buttonDispatcher);
			this.btnZoomOut = this.controlObject.getChildControl("zoomout");
			if(this.btnZoomOut)
				this.btnZoomOut.setReleaseCallBack(this, this.buttonDispatcher);

			this.textBoxItemCounter = this.controlObject.getChildControl("counter");
			this.textBoxItemType = this.controlObject.getChildControl("type");
		} else {
			trace("ОШИБКА: неправильное название группы.");
		}	

		return true;
	}

	function showObject(){
//trace("rnmc_stdCollection.showObject");
		if(this.startState == COLLECTION_STATE_ITEM){
			this.currentState = COLLECTION_STATE_ITEM;
			this.showCurrentItem();
			if (this.itemsList)
				this.itemsList.hideObject();
		} else if(this.startState == COLLECTION_STATE_LIST){
			this.currentState = COLLECTION_STATE_LIST;
			this.itemsList.showObject();
		}
		if(this.controlObject)
			this.controlObject.showObject();
		this.visible = true; 
	};

	function hideObject(){
		if(this.controlObject)
			this.controlObject.hideObject();
		hideCurrentItem();
		this.itemsList.hideObject();
	};

	function onSelectItem(newItemIndex){
		this.lastTopIndex = this.itemsList.topIndex;
		if(this.controlObject)
			this.controlObject.showObject();
		this.itemsList.hideObject();

		this.currentItem = newItemIndex;
		this.showCurrentItem();
	}

	function updateItemTitle(){
		if(this.itemTitle){
			this.itemTitle.parent = null;
			this.itemTitle = null;
		}

		if (!this.currentItemObjects)
			return;

		if(this.titleState == TITLE_STATE_VISIBLE){
			if(collectionItems[currentItem].titleVisible){
				if(this.collectionItems[currentItem].titleValue
					&& this.collectionItems[currentItem].titleValue){
					this.itemTitle = new Text();
					this.itemTitle.depth = "-100";
					this.itemTitle.bkgColor = "#00FFFFFF";
					this.itemTitle.x = this.collectionItems[currentItem].titleX;
					this.itemTitle.y = this.collectionItems[currentItem].titleY;
					if(this.collectionItems[currentItem].titleWidth)
						this.itemTitle.width = this.collectionItems[currentItem].titleWidth;
					if(this.collectionItems[currentItem].titleHeight)
						this.itemTitle.height = this.collectionItems[currentItem].titleHeight;
					if(this.collectionItems[currentItem].titleBgColor)
						this.itemTitle.bkgColor = this.collectionItems[currentItem].titleBgColor;
					if(this.collectionItems[currentItem].titleStyle)
						this.itemTitle.style = this.collectionItems[currentItem].titleStyle;
					this.itemTitle.parent = this;
					this.itemTitle.value = this.collectionItems[currentItem].titleValue;
				}
				if (this.btnShowTitle)
					this.btnShowTitle.hideObject();
				if (this.btnHideTitle)
					this.btnHideTitle.showObject();
			} else {
				if(this.itemTitle){
					this.itemTitle.parent = null;
					this.itemTitle = null;
				}
				if(this.btnShowTitle)
					this.btnShowTitle.hideObject();
				if(this.btnHideTitle)
					this.btnHideTitle.hideObject();
			}
		} else if(this.titleState == TITLE_STATE_HIDDEN){
			if(collectionItems[currentItem].titleVisible){
				if(this.itemTitle){
					this.itemTitle.parent = null;
					this.itemTitle = null;
				}
				if(this.btnShowTitle)
					this.btnShowTitle.showObject();
				if(this.btnHideTitle)
					this.btnHideTitle.hideObject();
			} else {
				if(this.btnShowTitle)
					this.btnShowTitle.hideObject();
				if(this.btnHideTitle)
					this.btnHideTitle.hideObject();
			}
		}
	}

//===================================================================
//								showCurrentItem()
//===================================================================
	function showCurrentItem(){
//trace("rnmc_stdCollection.showCurrentItem()");
	//	очистка объектов текущего элемента
		if(this.currentItemObjects){
			while(this.currentItemObjects.length > 0){
				var object = this.currentItemObjects.pop();
				object.hideObject();
			}
			this.currentItemObjects = null;
		}
		this.zoomTarget = null;

		if(this.btnNext)
			this.btnNext.showObject();
		if(this.btnPrevious)
			this.btnPrevious.showObject();

		if(this.listAvailable)
			if(this.btnShowList)
				this.btnShowList.showObject();
		else
			if(this.btnShowList)
				this.btnShowList.hideObject();

		// установка номера и типа иллюстрации
		if(this.textBoxItemCounter){
			this.textBoxItemCounter.setTextValue(parseInt(this.currentItem + 1) + " из " + this.collectionItems.length);
			this.textBoxItemCounter.showObject();
		}

		if(this.textBoxItemType){
			this.textBoxItemType.setTextValue(this.collectionItems[this.currentItem].contentType);
			this.textBoxItemType.showObject();
		};
		this.currentItemObjects = new Array();
		for(var i = 0; i < this.collectionItems[this.currentItem].length; i++){
			var srcNode = this.collectionItems[this.currentItem][i];
			var newObject = scene.Utils.initStdObjectFromNode(srcNode, this);
			if(newObject)
				this.currentItemObjects.push(newObject);
		}

	// отображение объектов
		for(var i = 0; i < this.currentItemObjects.length; i++){
			this.currentItemObjects[i].showObject();
		}
		if(this.zoomTarget){
			if(this.btnZoomOut)
				this.btnZoomOut.hideObject();
			if(this.btnZoomIn)
				this.btnZoomIn.showObject();
		}
		this.updateItemTitle();
	};

	function hideCurrentItem(){
//trace("rnmc_stdCollection.hideCurrentItem");
		if(this.currentItemObjects){
			while(this.currentItemObjects.length > 0){
				var object = this.currentItemObjects.pop();
				object.hideObject();
			}
			this.currentItemObjects = null;
		}
	//  прячем подпись
		this.updateItemTitle();
		if (this.controlObject)
			this.controlObject.hideObject();
	};

	function saveAs(){
//trace("Save as dialog... ");
	};

	function zoomIn(){
//trace("zoomInIllustration()");
		if(this.zoomTarget){
			this.zoomTarget.zoomIn();
			if(this.btnZoomIn)
				this.btnZoomIn.hideObject();
			if(this.btnZoomOut)
				this.btnZoomOut.showObject();
		}
	};

	function zoomOut(){
//trace("zoomOutIllustration()");
		if(this.zoomTarget){
			this.zoomTarget.zoomOut();
			if(this.btnZoomOut)
				this.btnZoomOut.hideObject();
			if(this.btnZoomIn)
				this.btnZoomIn.showObject();
		}
	};

	function previousItem(){
		if(collectionItems.length > 1){
			if(currentItem == 0){
				currentItem = collectionItems.length - 1;
			} else {
				currentItem--;
			};
		};
		this.showCurrentItem();
	};

	function nextItem(){
		if(collectionItems.length > 1){
			if(currentItem == collectionItems.length - 1){
				currentItem = 0;
			} else {
				currentItem++;
			};
		};
		this.showCurrentItem();
	};

	function buttonDispatcher(btnName){
		switch(btnName){
		case "previous":
			this.previousItem();
			break;
		case "next":
			this.nextItem();
			break;
		case "showlist":
			hideCurrentItem();
			this.itemsList.topIndex = this.lastTopIndex;
			this.itemsList.showObject();
			break;
		case "zoomin":
			this.zoomIn();
			break;
		case "zoomout":
			this.zoomOut();
			break;
		case "showtitle":
			this.titleState = TITLE_STATE_VISIBLE;
			this.updateItemTitle();
			break;
		case "hidetitle":
			this.titleState = TITLE_STATE_HIDDEN;
			this.updateItemTitle();
			break;
		case "save":
			trace("Save as dialog...");
			break;
		default:
			trace("Invalid button id. Verify button ids.");
			break;
		}	
	};
]]></script>
</composition>
</rmml>